各位好,我想在GridView 模板中添加一个CheckBox 不管我是勾上还是取消都不发生事件,我在 CS 里面这么写
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
            CheckBox cb = e.Row.FindControl("CheckBox3") as CheckBox;
            if (cb != null)
            {
                if (cb.Checked == true)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('123');", true);
                }
            }    }
但是只有第一次才加载的时候调用,然后我有写了
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {    }
在这个事件中写了 可是都没有反应,我已经把 checkbox 的AutoPostBack="true" 了还是不行 不执行时间 请教一下 谁会的 ?

解决方案 »

  1.   

    用个viewstate储存checkbox对应行.
      

  2.   

    为什么要写在 RowDataBound 中呢?有什么道理?你了解过RowDataBound何时触发吗?把你的RowDataBound 删除掉,在模板中声明事件处理方法,例如:
    <asp:CheckBox ID="CheckBox3" runat="server" OnCheckedChanged="CheckBox3_CheckedChanged" AutoPostBack="true" />protected void CheckBox3_CheckedChanged(object sender, EventArgs e) 
        { 
                CheckBox cb = sender as CheckBox; 
                if (cb != null) 
                { 
                    if (cb.Checked == true) 
                    { 
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('123');", true); 
                    } 
        } 不要动不动就手写代码!尤其是你写不明白逻辑,就尽量少写代码,尤其不要多写 RowDataBound 这种。
      

  3.   

    实际上,代码中也根本不用判断“if (cb != null)”。你要响应哪一个控件的事件就把代码写到那里,除此以外,设计部分在设计代码(窗口)中定义,而不要手写一堆类似 RowDataBound 中的代码。这种代码对你这样的初学者来说写的越多越容易出错。