0

OnCheckedChanged event is firing on every postback of different control. How can I prevent it?

Like I want onCheckedChanged should work only with the checkbox. How to prevent other controls to fire this event on postback?

aspx:

<asp:TemplateField HeaderText="Show on Order"
    HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="center" 
    ItemStyle-Width="15%">
    <ItemTemplate>
        <asp:CheckBox ID="chkShowOnOrder" runat="server" AutoPostBack="true"
            Checked='<%# Bind("Show_Order") %>' 
            OnCheckedChanged="chkShowOnOrder_CheckedChanged" 
            ToolTip='<%# Bind("Document_ID") %>'>
        </asp:CheckBox>
    </ItemTemplate>
</asp:TemplateField>

C#

protected void chkShowOnOrder_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    int docID = Convert.ToInt32(checkbox.ToolTip);
    TableRow tr = (TableRow)checkbox.Parent.Parent;

    /*Some other code below.*/
}

Same question is asked in this post but i did not get it. Please Help.

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • The event will fire on every postback if the checkbox has changed, you can't prevent that. If you want changing the checkbox to cause a postback then set `AutoPostBack="true"` on the checkbox. – Ben Robinson Sep 23 '14 at 10:23
  • The event is firing as expected with the checkbox but it is also firing with every postback of different control. like click on Linkbutton will also call the onCheckedChanged event of Checkbox. – Muahmmad Haris Khan Sep 23 '14 at 10:27
  • Clicking on a Linkbutton will not fire the CheckChanged even of a checkbox (unless you have also wired it up to the linkbutton click event or similar), however if the checkbox has changed since the last postback, the event will fire in the next postback, irrespective of which control causes the postback. – Ben Robinson Sep 23 '14 at 10:32
  • @BenRobinson Sir you should check this post: http://social.msdn.microsoft.com/Forums/vstudio/en-US/fa68a24e-43c7-4d0d-b500-62efd5285b54/oncheckedchanged-event-fired-for-all-the-events-in-the-webpage?forum=csharpgeneral – Muahmmad Haris Khan Sep 23 '14 at 10:34
  • CheckBox box2 = (CheckBox)GridView1.Rows[rowIndex].FindControl("chkShowOnOrder "); use find control – Genish Parvadia Sep 23 '14 at 12:51

2 Answers2

0

As per Radio button checked changed event fires twice you can verify that which control is causing the CheckedChanged event to fire as follows

protected void chkShowOnOrder_CheckedChanged(object sender, EventArgs e)
{ 
    CheckBox cb= sender as CheckBox ;
    if (cb != null && cb.ID=="chkShowOnOrder")
    {
       //Your code here
    }
}
Community
  • 1
  • 1
captainsac
  • 2,484
  • 3
  • 27
  • 48
  • i have also tried this approach but this solutions has one performance bug. means if i have 25 checkboxes on my page then this block of code will run 25 times. even the postback call was not from the original checkbox. but it will call this block 25 times. i have run it on debug mode for checking its performance. – Muahmmad Haris Khan Sep 23 '14 at 10:31
0

You can check the value of

Request.Form["__EVENTTARGET"]

To know which control has fired the event. Hence can put a check on using that value.

Priyank
  • 1,353
  • 9
  • 13