OnCheckedChanged event not firing

2019-02-11 12:33发布

问题:

I have a GridView with a column of checkboxes (the rest of the GridView is being populated from a database). I'm using AJAX to perform different functions, and I'm wondering if i'm just not calling the OnCheckedChanged event in the right place. Should it be wrapped in some sort of UpdatePanel? I'm still really new to how all of this works...basically what I'm aiming for is to change a bit value in my database when a checkbox is checked. I know the logic of how to do that, I just don't know if I'm addressing my OnCheckedChanged event the right way.

.CS

        protected void CheckBoxProcess_OnCheckedChanged(Object sender, EventArgs args)
    {
        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;
        OrderBrowser.Text += "CHANGED";
    }


    }

.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

        <asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
            <asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
            <asp:ListItem Value="lName">Last Name</asp:ListItem>
            <asp:ListItem Value="state">State</asp:ListItem>
            <asp:ListItem Value="zip">Zip Code</asp:ListItem>
            <asp:ListItem Value="cwaSource">Source</asp:ListItem>
            <asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
        </asp:DropDownList>
    </div>
    <div>
        <asp:Label runat="server" ID="searchLabel" Text="Search For: " />
        <asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
        <asp:Button ID="searchButton" runat="server" Text="Search" />
    </div>
<div>
<asp:UpdatePanel ID = "up" runat="server">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID = "orderByList"
    EventName="SelectedIndexChanged" />
     <asp:AsyncPostBackTrigger ControlId="searchButton" EventName="Click" />
</Triggers>

<ContentTemplate>
<div align="center">
    <asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName"
    onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
    autogenerateselectbutton = "true" 
    selectedindex="0">
    <SelectedRowStyle BackColor="Azure"
    forecolor="Black"
    font-bold="true" />
    <Columns>
    <asp:TemplateField HeaderText="Processed">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBoxProcess" runat="server" Enabled="true" OnCheckedChanged = "CheckBoxProcess_OnCheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>

    </Columns>
    </asp:GridView>
    </div>
    <asp:TextBox ID="OrderBrowser" columns="100" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true">
    </asp:TextBox>
    </ContentTemplate>
    </asp:UpdatePanel>



</div>
</form>

回答1:

Try turning AutoPostBack on for the checkbox control.

<asp:CheckBox ID="CheckBoxProcess" runat="server" Enabled="true" OnCheckedChanged = "CheckBoxProcess_OnCheckedChanged" AutoPostBack="true" />

This maybe the reason your method isn't being called.



回答2:

Please try to check the value with the DB:

Checked='<%# Convert.ToBoolean(Eval("Processed")) %>'