首页  ·  知识 ·  云计算
GridView的RowDataBound和DataGrid的ItemDataBound邦定后触发事件
aiouluosi  http://www.cnblogs.com/aiouluosi/  综合  编辑:dezai  图片来源:网络
GridView的RowDataBound邦定后触发事件 1 protected void lbtnSetVIP_Click(object sender, EventArgs e)2 

GridView的RowDataBound邦定后触发事件

 1 protected void lbtnSetVIP_Click(object sender, EventArgs e)
 2     {
 3         Label lbvip = new Label();
 4         Label lbid = new Label();
 5         LinkButton lbtnSetVIP = (LinkButton)sender;
 6         lbvip = (Label)lbtnSetVIP.Parent.FindControl("lbvip");
 7         lbid = (Label)lbtnSetVIP.Parent.FindControl("lbid");
 8
 9         int isvip = Convert.ToInt32(lbvip.Text);
10         string update_member = string.Empty;
11         if (isvip == 0)
12         {
13             update_member = "update member set isvip = 1 where id =" + lbid.Text;
14             db.Execute(update_member);
15             GridView1.DataSourceID = SqlDataSource1.ID;
16             GridView1.DataBind();
17         }
18         else
19         {
20             update_member = "update member set isvip = 0 where id =" + lbid.Text;
21             db.Execute(update_member);
22             GridView1.DataSourceID = SqlDataSource1.ID;
23             GridView1.DataBind();
24         }
25     }
26
27     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
28     {
29         if (e.Row.RowType == DataControlRowType.DataRow)
30         {    
31
32             LinkButton lbtnSetVIP = new LinkButton();
33             Label lbvip = new Label();
34             lbvip = (Label)e.Row.FindControl("lbvip");
35             lbtnSetVIP = (LinkButton)e.Row.FindControl("lbtnSetVIP");
36
37             int isv = Convert.ToInt32(lbvip.Text);
38             if (isv == 0)
39             {
40                 lbtnSetVIP.Text = "设置为VIP用户";
41                 lbtnSetVIP.ForeColor = System.Drawing.Color.Green;
42                 lbtnSetVIP.Attributes.Add("onclick", "javascript:return confirm('确定设置此用户吗?')");
43             }
44             else
45             {
46                 lbtnSetVIP.Text = "取消VIP用户";
47                 lbtnSetVIP.ForeColor = System.Drawing.Color.Red;
48                 lbtnSetVIP.Attributes.Add("onclick", "javascript:return confirm('确定取消此用户吗?')");
49             }
50         }       DataGrid的ItemDataBound邦定后触发事件
protected void dgArticleClass_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) || (e.Item.ItemType == ListItemType.EditItem))
        {
            e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#DFEAFD';";
            e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
            TableCell cell = e.Item.Cells[6];
            LinkButton button = (LinkButton)cell.Controls[1];
            button.Attributes.Add("onclick", "return confirm('确定要删除这个栏目及其子栏目吗?');");
            TableCell cell2 = e.Item.Cells[7];
            LinkButton button2 = (LinkButton)cell2.Controls[0];
            button2.Attributes.Add("onclick", "return confirm('确定要清空这个栏目及其子栏目的软件吗?');");
        }
    }
当控件在DataView或DataGrid里时:
      btn.Attributes.Add("onclick","javascript:return confirm('确定删除吗?')");
在控件里需要用到别的控件来存状态以便操作
      例如:Button,label 控件  
       Button lbtnSetVIP = (LinkButton)sender;  //sender是object对象之一
      找到它>>
       label   lb  =  (label)e.Row.FindCcontrol("lbXX");  //lbXX是ID号
    
      然后此Button 控件对象通过lXX找到该控件 (找到它的父级控件)
     
       label  lbvip  =  new  label();
       lbvip =  (label)lbtnSetVIP .Parent.FindControl("lbXX");

      label  lbid = new label();
      实例化label 在这里是为了存这一项的主键以便操作:
      >> update_member = "update member set isvip = 1 where id =" + lbid.Text;   

本文作者:aiouluosi 来源:http://www.cnblogs.com/aiouluosi/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读