I am using c# and VS 2008. My GridView has several cells but only one cell ("Active", boolean) with a checkbox TemplateField is not updating in Edit mode when Update button is clicked. I have specified 2 primary keys as DataKeyNames in GridView1 properties. I am using rowupdating event to pass the desired checkbox (bit)value to sqldatasource. Is there another event I am missing in order to make the update happen? Again, all other cells update just fine.
I did a lot of research before posting here and I still couldn't see where my mistake is. I only included pertinent parts of the code (gridview properties, the one field in question
ASPX
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
AutoGenerateEditButton="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
DataSourceID="sqldatasource1"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
DataKeyNames="ID,CertID" ondatabound="GridView1_DataBound"
onrowcreated="GridView1_RowCreated"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Active" SortExpression="Active">
<EditItemTemplate>
<asp:CheckBox ID="cbActiveEdit" runat="server"
Text='<%# Convert.ToBoolean(Eval("Active").ToString().Equals("Yes")) %>'
Checked='<%# Convert.ToBoolean(Eval("Active").ToString().Equals("Yes")) %>'
Enabled="True"
AutoPostBack="false"></asp:CheckBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblActive" runat="server" Text='<%# Convert.ToBoolean(Eval("Active").ToString().Equals("Yes")) %>' Enabled="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
SelectCommand="SelectRecords"
SelectCommandType="StoredProcedure"
UpdateCommand="UpdateRecords"
UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddlvwRecords" Name="ID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="CertID" Type="Int32" />
<asp:Parameter Name="CertNumber" Type="String" />
<asp:Parameter Name="DueBy" Type="DateTime" />
<asp:Parameter Name="Completed" Type="DateTime" />
<asp:Parameter Name="Expires" Type="DateTime" />
<asp:Parameter Name="Renewed" Type="DateTime" />
<asp:Parameter Name="Comments" Type="String" />
<asp:Parameter Name="PeriodEffective" Type="String" />
<asp:Parameter Name="CeuEarned" Type="String" />
<asp:Parameter Name="Active" Type="Boolean" />
</UpdateParameters>
</asp:SqlDataSource>
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
//Get the new values to Update
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
CheckBox cb = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("cbActiveEdit");
string strActiveStatus;
if (cb != null && cb.Checked == true)
{
strActiveStatus = "1";
}
else
{
strActiveStatus = "0";
}
sqldatasource1.UpdateParameters["Active"].DefaultValue = GetStatus(strActiveStatus).ToString();//GetStatus is returning “true” or “false”
sqldatasource1.Update();
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
GridView1.DataBind();
}//end try
catch (SqlException ex)
{
//handle error here
}
}
protected bool GetStatus(string str)
{
if (str == "1")
{
return true;
}
else
{
return false;
}
}
Aucun commentaire:
Enregistrer un commentaire