c# - 复选框自动回发而不刷新页面 asp net

标签 c# jquery asp.net

我有这个复选框,我需要将其设置为 AutoPostBack="True",这样我才能触发 OnCheckedChanged="chkCompany_OnCheckedChanged"。问题是我不希望页面被刷新和重定向,我希望用户原地不动。

ASPX:

<asp:CheckBox OnCheckedChanged="chkCompany_OnCheckedChanged" AutoPostBack="True" CssClass="chkCompany" ClientIDMode="Static" ID="chkCompany" runat="server" />

C#:

protected void chkCompany_OnCheckedChanged(object sender, EventArgs e)
    {
        if (chkCompany.Checked)
        {
            txtName.Visible = false;
        }

        else
        {
            txtName.Visible = true;
        }
    }

最佳答案

您应该使用 UpdatePanel 控件来执行此操作

 <asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                <asp:CheckBox OnCheckedChanged="chkCompany_OnCheckedChanged" AutoPostBack="True" CssClass="chkCompany" ClientIDMode="Static" ID="chkCompany" runat="server" />
            </ContentTemplate>
        </asp:UpdatePanel>

将您的代码保存在更新面板中。

关于c# - 复选框自动回发而不刷新页面 asp net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718727/

相关文章:

c# - 从调用中获取响应

c# - 扩展任务管理器 Windows 8

c# - Newtonsoft Json 将字典反序列化为 DataContractJsonSerializer 中的键/值列表

jquery - 使用vue js或jquery替换元素中的对象

javascript - 为什么 jquery serialize() 无法解析用户定义的属性?

c# - 我正在使用反射来获取 ASP.NET 中的属性名称和值需要一些优化建议

asp.net - 使用 JavaScript 验证文件大小

c# - CollectionAssert.AreEquivalent 与 Assert.Equals()

php - JQuery Datatables click事件不适用于除1之外的页面

ASP.NET 为什么 session 超时,sessionstate超时设置