我有一个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();
}
}
知道我错过了什么吗?
答案 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" />