带回发的Checkbox OnCheckedChange事件?

时间:2018-04-13 11:31:12

标签: c# asp.net gridview checkbox oncheckedchanged

我有一个gridview,其中checkbox个控件作为列,Autopostback='true'用于插入db。  除非我“更改页面” - 回发(?)

,否则一切正常

当我返回页面时,我的选中复选框将被取消选中。

我的Page_Load有一个!IsPostBack然后我绑定了我的gridview( gwPlanning

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridViewPlanning();
    }
}

我的代码

protected void myCheckBox_OnCheckedChange(object sender, EventArgs e)
{

    CheckBox myCheckBox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)myCheckBox.NamingContainer;

    string ThisWeekMinus2 = row.Cells[1].Text;
    bool status = myCheckBox.Checked;

    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("UPDATE [BI_Planning].[dbo].[tblPlanning] SET [This Week - 2] = @IsActive WHERE ActivityID = @ID  ", con);
        cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Value = myCheckBox.Checked;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ThisWeekMinus2;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }        

}

知道我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

解决方案只是添加:

<强>选中='&LT;%#Convert.ToBoolean(EVAL( “.... .... YourColumn”))%&GT;

<asp:CheckBox ID="CheckBox1" AutoPostBack="true"  Checked='<%#Convert.ToBoolean(Eval("....YourColumn...."))%> OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />