|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
专业性的服务。有的ASP商提供垂直型的应用服务,针对某一特定行业提供应用服务。datagrid|静态|模板数据源布局同http://blog.csdn.net/lovecherry/archive/2005/03/24/328455.aspx
表dep:depid(标识主键),depname(学院名字)
表stu:stuid(标识主键),stuname(先生名字),studepid(学院id=表dep.depid)
前台:
<%@Pagelanguage="c#"Codebehind="WebForm30.aspx.cs"AutoEventWireup="false"Inherits="csdn.WebForm30"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WebForm30</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<linkhref="CSS.css"rel="stylesheet"type="text/css">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
</form>
</body>
</HTML>
背景:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespacecsdn
{
///<summary>
///WebForm30的择要申明。
///</summary>
publicclassWebForm30:System.Web.UI.Page
{
DataGridDataGrid1=newDataGrid();
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处安排用户代码以初始化页面
CreateDataGrid();
}
protectedvoidCreateDataGrid()
{
DataGrid1.AutoGenerateColumns=false;
DataGrid1.CssClass="border";
DataGrid1.BorderWidth=0;
DataGrid1.CellSpacing=1;
DataGrid1.CellPadding=5;
DataGrid1.ItemStyle.CssClass="item";
DataGrid1.HeaderStyle.CssClass="header";
DataGrid1.DataKeyField="stuid";
//以上设定DataGrid的款式
TemplateColumntm=newTemplateColumn();
tm.ItemTemplate=newColumnTemplate1();
tm.HeaderText="姓名";
DataGrid1.Columns.Add(tm);
//创建第一个模板列
TemplateColumntm2=newTemplateColumn();
tm2.ItemTemplate=newColumnTemplate2();
tm2.HeaderText="学院";
DataGrid1.Columns.Add(tm2);
//创建第二个模板列
ButtonColumnbc=newButtonColumn();
bc.ButtonType=ButtonColumnType.PushButton;
bc.CommandName="del";
bc.Text="删除";
DataGrid1.Columns.Add(bc);
//创建删除按钮列
SetBind();
//添补数据
Page.Controls[1].Controls.Add(DataGrid1);
//给页面的form到场这个DataGrid1
}
protectedvoidSetBind()
{
SqlConnectionconn=newSqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapterda=newSqlDataAdapter("select*fromstu,depwherestu.studepid=dep.depid",conn);
DataSetds=newDataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
privatevoidDataGrid1_ItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
{
//和下面毗连给出的例子中的代码一样,给下拉框绑定命据,而且选择默许的
SqlConnectionconn=newSqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapterda=newSqlDataAdapter("select*fromdep",conn);
DataSetds=newDataSet();
da.Fill(ds,"table1");
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
DropDownListddl=(DropDownList)e.Item.FindControl("dep");
ddl.DataSource=ds.Tables["table1"];
ddl.DataTextField="depname";
ddl.DataValueField="depid";
ddl.DataBind();
ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;
}
}
privatevoidDataGrid1_ItemCommand(objectsource,System.Web.UI.WebControls.DataGridCommandEventArgse)
{
if(e.CommandName=="del")
{
SqlConnectionconn=newSqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]</p>想法是和程序员的想法不一样的.至于为什么.大家去想一想.跟心理学有关的 |
|