欢迎光临
我们一直在努力

DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关

建站超值云服务器,限时71元/月

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 == nullreturn;
            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();
        }

       

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关
分享到: 更多 (0)