sweetdragon36067 sweetdragon36067 - 1 month ago 8
ASP.NET (C#) Question

How do I modify edititemtemplate unbounded checkboxes before they are rendered(visible) in edit mode?

I have a unique situation. I have added several checkboxes to a gridview and they are populated from a database. They look very good in itemtemplate mode. When I click the Edit button and they are rendered, they loose all settings and appear is unchecked boxes. How do I populate the edititemtemplate checkboxes before they are displayed in edit mode?
Here is the code snippet in the aspx page:

<asp:TemplateField HeaderText="Referral">
<ItemTemplate>
<asp:CheckBox ID="CheckBox7" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox10" runat="server" />
</EditItemTemplate>
</asp:TemplateField>


and here is the code behind for this checkbox that populates these itemtemplate checkboxes:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)


{

//load the referral column from the form3 data column to display the results

TextBox txtReferral = (TextBox)e.Row.FindControl("TextBox82"); //form1
if (txtReferral != null)
{
CheckBox ckbxReferral = (CheckBox)e.Row.FindControl("CheckBox7");
ckbxReferral.Enabled = false;
if (txtReferral.Text.Length > 0)
{
int intReferral = Convert.ToInt32(txtReferral.Text.ToString());
if (intReferral > 0)
{
if (!ckbxReferral.Checked)
{
ckbxReferral.Checked = true;
}
intReferral = 0;
}
}
}
}


I have tried to set the editemtemplate checkboxes, but have had no luck since they are currently null objects. I am a newbie to .net programming, so any help will be greatly appreciated. My ultimate goal is to use the results of the checkboxes to populate two additional child tables from these results.

Answer

You need to keep the id of the checkbox controls same in both itemtemplate and edititemtemplate:

     <asp:TemplateField HeaderText="Referral">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox7" runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:CheckBox ID="CheckBox7" runat="server" />
        </EditItemTemplate>
     </asp:TemplateField>
Comments