datagrid中想实现这样的效果:
根据某一字段列的值动态改变按钮的文本,比如:
点击按钮列,自动更新某列原为0的值为1,并将按钮列的文本改为“置0”,
再按下,自动更新某列原为1的值为0,并将按钮列的文本改为“置1”,
最终通过namingcontainer,实现! 方法如下 :
<asp:datagrid id=”datagrid1″ runat=”server” autogeneratecolumns=”false”> <columns> <asp:boundcolumn datafield=”honoreeid” headertext=”id”></asp:boundcolumn> <asp:boundcolumn datafield=”status” headertext=”状态”> <headerstyle width=”300px”></headerstyle> </asp:boundcolumn> <asp:templatecolumn headertext=”状态是否为0″> <headerstyle horizontalalign=”center” width=”10%”></headerstyle> <itemstyle horizontalalign=”center”></itemstyle> <itemtemplate> <asp:label id=”lb” runat=”server” visible=”false” text=<%# ((databinder.eval(container, “dataitem.status”, “{0}”))==”0″)?”是”:”<font color=red>否</font>” %>> </asp:label> <asp:button id=”changestate” runat=”server” text=<%# ((databinder.eval(container, “dataitem.status”, “{0}”))==”0″)?”转为1″:”转为0″ %>> </asp:button> </itemtemplate> </asp:templatecolumn> </columns> </asp:datagrid> |
后台:
protected system.web.ui.webcontrols.datagrid datagrid1; public int keyid { get { object o =viewstate [“keyid“]; if(o!=null) { return int.parse(viewstate [“keyid“].tostring()); } else { return 0; } } set { viewstate [“keyid“] = value; } } public int rowstate { get { return int.parse(viewstate [“rowstate“].tostring()); } set { viewstate [“rowstate“] = value; } } private void page_load(object sender, system.eventargs e) { if(ispostback) {return ; } getdata(); } private void getdata() { //sqlconnection con = new sqlconnection(configurationsettings.appsettings[“dsnpubs”]); sqlconnection con = new sqlconnection(system.configuration.configurationsettings.appsettings[“mblog“]); sqlcommand cmd; con.open(); cmd = new sqlcommand(“select * from dbo.honoree“, con); datagrid1.datasource = cmd.executereader(); datagrid1.databind(); con.close(); } private bool updatedata(int id,int oldstate) { sqlconnection con = new sqlconnection(system.configuration.configurationsettings.appsettings[“mblog“]); sqlcommand cmd; con.open(); try { string strsql=“update honoree set status={0} where honoreeid={1}“; strsql=string.format(strsql,(oldstate==0?1:0).tostring(),id.tostring()); cmd = new sqlcommand(strsql, con); cmd.executenonquery(); cmd.dispose(); return true; } catch { return false; } finally { con.close(); con.dispose(); } return false; } web 窗体设计器生成的代码 private void datagrid1_itemcreated(object sender, system.web.ui.webcontrols.datagriditemeventargs e) { if(e.item.itemtype==listitemtype.item || e.item.itemtype == listitemtype.alternatingitem) { button b=(button)e.item.findcontrol(“changestate“); if(b!=null) { b.click+=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 = (button)sender; datagrid dg = (datagrid)but.namingcontainer.namingcontainer; //此处是关键!!即找到包含按钮的命名容器的上层命名容器 if(dg == null) return; datagriditem di =(datagriditem)but.namingcontainer; tablecell key= (tablecell)di.cells[0]; tablecell state= (tablecell)di.cells[1]; keyid=(key==null)?0:int.parse(key.text); rowstate=(state==null)?0:int.parse(state.text); response.write(updatedata(this.keyid,this.rowstate).tostring()); getdata(); } |