简体   繁体   English

AsyncPostBackTrigger用于复选框控件未触发

[英]AsyncPostBackTrigger for Checkbox control not firing

Context of the problem: 问题的上下文:

I have a checkbox and a button. 我有一个复选框和一个按钮。 The button is disabled and colored grey upon the page load. 在页面加载时,该按钮被禁用并显示为灰色。 I want the checkbox to enable and re-color the button if checked, and revert the change if unchecked. 我希望复选框启用并重新为按钮添加颜色(如果已选中),并还原更改(如果未选中)。 Think of it as a User Agreement page, where the user accepts the terms and needs to click on the checkbox to proceed. 可以将其视为“用户协议”页面,该页面上的用户接受条款并需要单击复选框以继续。

There is a grid view and other components above the page that would break upon a postback, so searching online I found that I could use an UpdatePanel. 页面上方有网格视图和其他组件,这些视图会在回发时中断,因此在线搜索后发现可以使用UpdatePanel。 Thought this would be more simple than writing jQuery for the checkbox, but it isn't working 认为这比为复选框编写jQuery更简单,但无法正常工作

Code: 码:

ASPX ASPX

<asp:UpdatePanel ID ="upCheckbox" runat="server">
    <ContentTemplate>
        <asp:CheckBox ID="checkLabel" runat="server" OnCheckedChanged="checkLabel_CheckedChanged"/><asp:Label ID="AknowledgementLabel" runat="server" Text="Info is correct & Enable button"></asp:Label>
        <br /><br />
        <asp:Button ID="btnSubmit" Text="Submit Application" CssClass="jqbutton" OnClick="btnSubmit_Click" runat="server"/>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="checkLabel" EventName="CheckedChanged"/>
    </Triggers>
</asp:UpdatePanel>

Code Behind: 背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    btnSubmit.Enabled = false;
    btnSubmit.ForeColor = System.Drawing.Color.Gray;
}

protected void checkLabel_CheckedChanged(object sender, EventArgs e)
{
    if (checkLabel.Checked)
    {
        btnSubmit.Enabled = true;
        btnSubmit.ForeColor = System.Drawing.ColorTranslator.FromHtml("#336699");
    }
    else
    {
        btnSubmit.Enabled = false;
        btnSubmit.ForeColor = System.Drawing.Color.Gray;
    }
}

Problem: 问题:

The checkbox does not work and the submit button remains disabled. 复选框不起作用,并且提交按钮保持禁用状态。 Not sure if I am using Update Panels for the appropriate purpose or if am missing something. 不知道我是出于适当目的使用更新面板还是丢失了某些东西。 Is it possible to accomplish this the way that I am trying to, or should I move on to jQuery? 是否可以按照我尝试的方式完成此操作,还是应该继续使用jQuery?

You are missing AutoPostback="true" in your CheckBox control. 您在CheckBox控件中缺少AutoPostback="true" By default it is set to false . 默认情况下,它设置为false With autopostback missing your event won't reach server side. 缺少自动回传功能,您的活动将无法到达服务器端。

<asp:UpdatePanel ID ="upCheckbox" runat="server">
    <ContentTemplate>
        <asp:CheckBox ID="checkLabel" runat="server" AutoPostback="true" OnCheckedChanged="checkLabel_CheckedChanged"/>
        <asp:Label ID="AknowledgementLabel" runat="server" Text="Info is correct & Enable button"></asp:Label>
        <br /><br />
        <asp:Button ID="btnSubmit" Text="Submit Application" CssClass="jqbutton" OnClick="btnSubmit_Click" runat="server"/>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="checkLabel" EventName="CheckedChanged"/>
    </Triggers>
</asp:UpdatePanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM