DataGrid中想实现这样的效果:
根据某一字段列的值动态改变按钮的文本,比如:
点击按钮列,自动更新某列原为0的值为1,并将按钮列的文本改为“置0”,
再按下,自动更新某列原为1的值为0,并将按钮列的文本改为“置1”,
最终通过NamingContainer,实现! 方法如下 :
<asp:DataGrid id&#61;"DataGrid1" runat&#61;"server" AutoGenerateColumns&#61;"False"> <Columns> <asp:BoundColumn DataField&#61;"HonoreeID" HeaderText&#61;"ID">asp:BoundColumn> <asp:BoundColumn DataField&#61;"status" HeaderText&#61;"状态"> <HeaderStyle Width&#61;"300px">HeaderStyle> asp:BoundColumn> <asp:TemplateColumn HeaderText&#61;"状态是否为0"> <HeaderStyle HorizontalAlign&#61;"Center" Width&#61;"10%">HeaderStyle> <ItemStyle HorizontalAlign&#61;"Center">ItemStyle> <ItemTemplate> <asp:Label id&#61;"lb" runat&#61;"server" Visible&#61;"false" Text&#61;&#39;<%# ((DataBinder.Eval(Container, "DataItem.status", "{0}"))&#61;&#61;"0")?"是":"color&#61;red>否font>" %>&#39;> asp:Label> <asp:Button ID&#61;"changeState" Runat&#61;"server" Text&#61;&#39;<%# ((DataBinder.Eval(Container, "DataItem.status", "{0}"))&#61;&#61;"0")?"转为1":"转为0" %>&#39;> asp:Button> ItemTemplate> asp:TemplateColumn> Columns> asp:DataGrid> |
后台&#xff1a;
protected System.Web.UI.WebControls.DataGrid DataGrid1; public int KeyID ...{ get ...{ object o &#61;ViewState ["KeyID"]; if(o!&#61;null) ...{ return int.Parse(ViewState ["KeyID"].ToString()); } else ...{ return 0; } } set ...{ ViewState ["KeyID"] &#61; value; } } public int RowState ...{ get ...{ return int.Parse(ViewState ["RowState"].ToString()); } set ...{ ViewState ["RowState"] &#61; value; } } private void Page_Load(object sender, System.EventArgs e) ...{ if(IsPostBack) ...{return ; } getData(); } private void getData() ...{ //SqlConnection con &#61; new SqlConnection(ConfigurationSettings.AppSettings["DsnPubs"]); SqlConnection con &#61; new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Mblog"]); SqlCommand cmd; con.Open(); cmd &#61; new SqlCommand("select * from dbo.Honoree", con); DataGrid1.DataSource &#61; cmd.ExecuteReader(); DataGrid1.DataBind(); con.Close(); } private bool UpdateData(int ID,int OldState) ...{ SqlConnection con &#61; new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Mblog"]); SqlCommand cmd; con.Open(); try ...{ string strSql&#61;"Update Honoree set Status&#61;{0} where HonoreeID&#61;{1}"; strSql&#61;string.Format(strSql,(OldState&#61;&#61;0?1:0).ToString(),ID.ToString()); cmd &#61; new SqlCommand(strSql, con); cmd.ExecuteNonQuery(); cmd.Dispose(); return true; } catch ...{ return false; } finally ...{ con.Close(); con.Dispose(); } return false; } Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) ...{ // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /**//// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() ...{ this.DataGrid1.ItemCreated &#43;&#61; new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated); this.DataGrid1.ItemDataBound &#43;&#61; new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); this.Load &#43;&#61; new System.EventHandler(this.Page_Load); } #endregion private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ...{ if(e.Item.ItemType&#61;&#61;ListItemType.Item || e.Item.ItemType &#61;&#61; ListItemType.AlternatingItem) ...{ Button b&#61;(Button)e.Item.FindControl("changeState"); if(b!&#61;null) ...{ b.Click&#43;&#61;new EventHandler(b_Click); } } } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ...{ } private void b_Click(object sender, EventArgs e) ...{ Button but &#61; (Button)sender; DataGrid dg &#61; (DataGrid)but.NamingContainer.NamingContainer; //此处是关键&#xff01;&#xff01;即找到包含按钮的命名容器的上层命名容器 if(dg &#61;&#61; null) return; DataGridItem di &#61;(DataGridItem)but.NamingContainer; TableCell key&#61; (TableCell)di.Cells[0]; TableCell state&#61; (TableCell)di.Cells[1]; KeyID&#61;(key&#61;&#61;null)?0:int.Parse(key.Text); RowState&#61;(state&#61;&#61;null)?0:int.Parse(state.Text); Response.Write(UpdateData(this.KeyID,this.RowState).ToString()); getData(); } |