我有这个复选框,我需要将其设置为 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/