When the button is clicked, the onCheckedChanged checkbox fires twice

I have a GridView with TemplateField with a checkbox. My goal is to capture the onclick event using autopostback and set the database flag. My only problem is that the event fires twice. The first time the checkbox (In Sender Parameter) contains the click value, so I set it based on the click. The second time the sender parameter has a checkbox which is always checked = false. I am happy to welcome suggestions for other questions that are appropriate for solving this problem, but my goal is to set a database flag based on the user checking the checkbox. I am targeting .NET Framework 2.0.

Here is the related code:

<div style="margin-left : 1em;margin-right:1em;">
    <asp:GridView ID="RouteGridView" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ROUTE_NUMBER" 
            ForeColor="#333333" GridLines="None" style="width:100%;" 
        onselectedindexchanged="RouteGridView_SelectedIndexChanged" 
        AllowSorting="True" onpageindexchanging="RouteGridView_PageIndexChanging" 
        onsorting="RouteGridView_Sorting" >
            <Columns>
<%-- Column one --%>
<asp:TemplateField HeaderText="Route" SortExpression="ROUTE_NUMBER">
    <ItemTemplate>
        <asp:LinkButton ID="HyperLink1" runat="server" CommandName="Select" CommandArgument='<%#Eval("ROUTE_NUMBER")%>'  
                            Text='<%# Eval("ROUTE_NUMBER") %>' ></asp:LinkButton>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<%-- Column 2 this is where the problem CheckBox is--%>
<asp:TemplateField HeaderText="Read?" 
    SortExpression="READ_FLAG">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox1" runat="server" 
            OnCheckedChanged="ChangeReadFlag"  
            AutoPostBack="true"
            Checked='<%# (string)DataBinder.Eval(Container.DataItem, "READ_FLAG") == "1" %>' Enabled='<%# isSelectedRow(Container)  %>' />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<%-- more columns --%
<%-- more columns --%>
</Columns>
</asp:GridView>

      

Here is the event handler from code:

protected void ChangeReadFlag(object sender, EventArgs e)
{
    if (RouteGridView.SelectedIndex != -1)
    {
        CheckBox cb = ((CheckBox)sender);
        DataKey key = RouteGridView.SelectedDataKey;

        //... do stuff here ...
    }
}

      

+1


source to share


5 answers


Is the checked state of a checkbox set to false during a page load event?



+1


source


There may be several reasons for this behavior. In my case this event was logged twice: once automatically as part of the checkbox definition
<asp:CheckBox ID="CheckBox1" runat="server" **OnCheckedChanged="ChangeReadFlag"** AutoPostBack="true" Checked='<%# (string)DataBinder.Eval(Container.DataItem, "READ_FLAG") == "1" %>' Enabled='<%# isSelectedRow(Container) %>' />

and the second time, explicit registration somewhere in the code, usually in the OnInit method:
CheckBox1.CheckedChanged += new EventHandler(ChangeReadFlag);



To fix this, you must remove the second registration either from the code behind or from your control.

+1


source


You want to capture the parity of the GridView; identify the command (sender) that triggers the postback and then you can update your database.

0


source


One reason (there may be others) the problem is that the event is being logged twice.

Check the code in the code behind, inside "InitializeComponent". This will cause the event to be logged twice and therefore fire twice.

You need to delete it in one place to fix the problem.

Ram.

0


source


Another potential problem is where the data binding is happening - I don't see the DataSource in your code from the front, so I think you are code-binding.

If you are doing data binding after postback and after the first onChange event is fired, it is likely that the data binding event is relaying the status of the checkbox and thus causing the event to fire again.

0


source







All Articles