I have a gridview with checkbox
controls as columns and with Autopostback='true'
for inserting to db. Everyting works fine except when i " change pages " - Postback (?)
My checked checkboxes gets unchecked when I go back to the page.
My Page_Load
have a !IsPostBack
and then i bound my gridview ( gwPlanning )
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridViewPlanning();
}
}
and my code behind :
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();
}
}
any idea what i'm missing?
The solution was just to add:
Checked='<%#Convert.ToBoolean(Eval("....YourColumn...."))%>
<asp:CheckBox ID="CheckBox1" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("....YourColumn...."))%> OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.