Search code examples
c#asp.netoncheckedchanged

OnCheckedChanged not firing when clicked


I'm trying to run my OnCheckedChanged inside an itemtemplate, but it is not firing. What I did was I typed the OnCheckChanged in the asp:CheckBox tag and also typed the entire method manually. Would this affect the process??

 <asp:CheckBox runat="server" ID="uoCheckBoxTagtoVehicle" OnCheckedChanged="ChkChanged" AutoPostBack="true" Width="50px"   />

and my event:

protected void ChkChanged(object sender, EventArgs e)
{
    uoHiddenFieldVehicle.Value = "1";
}

Note: I'm using Visual studio 2008


Solution

  • Since your control is inside a GridView (since you said ItemTemplate I assume you do) you can't use your approach to attach the event as you did. Because there will be multiple check boxes once you populate the GridView. Therefore, do the following

    1. In you GridView's DataBinding event find the CheckBox by ID (use FindControl method)
    2. Then attach the event OnCheckedChanged to the method you've written