Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复选框自动回发而不刷新网页asp net_C#_Jquery_Asp.net - Fatal编程技术网

C# 复选框自动回发而不刷新网页asp net

C# 复选框自动回发而不刷新网页asp net,c#,jquery,asp.net,C#,Jquery,Asp.net,我有一个复选框,我需要是AutoPostBack=“True”,这样我就可以触发OnCheckedChanged=“chkCompany\u OnCheckedChanged”。问题是,我不希望页面被刷新和重定向,我希望用户留在原地不动 ASPX: 您应该使用UpdatePanel控件来执行此操作 <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:

我有一个复选框,我需要是
AutoPostBack=“True”
,这样我就可以触发
OnCheckedChanged=“chkCompany\u OnCheckedChanged”
。问题是,我不希望页面被刷新和重定向,我希望用户留在原地不动

ASPX:


您应该使用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>


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

如果更新面板不工作,您可以使用javascript来完成此操作



函数CheckedChanged()
{
if((document.getElementById('chkCompany')。选中))
`{`
document.getElementById('txtname')。Visible=false;
}
`否则`
{
document.getElementById('txtname')。Visible=false;
`}`
}
-----------------------------------------------------------------------------

您必须使用UpdatePanel控件,并将代码放在UpdatePanel控件中。我试过了,但没有成功。除非你创建了一篇文章并向我展示了具体的方法?如果你所做的只是在选中复选框时更改另一个元素的可见性,我建议使用JavaScription这不是我所做的全部,我以后需要实现更多的代码,我已经有了一个jQuery解决方案,但我不能使用它!如果您使用c#代码并且AutoPostback=true,那么它肯定会在页面上发回。。。要么你必须使用javascriptIt,它说:'Element'CheckBox'不是一个已知的元素。如果网站中存在编译错误,或者Web.config文件丢失,则可能会发生这种情况。这只是一条警告消息,而不是错误。您可以跳过警告。不,不是这样,我的整个应用程序在尝试调试时冻结为白色。很抱歉,我错过了标记。更新了答案。我的答案中包含了脚本管理器。ScriptManager控件管理启用AJAX的ASP.NET网页的客户端脚本。默认情况下,ScriptManager控件向页面注册Microsoft Ajax库的脚本。这使客户端脚本能够使用类型系统扩展,并支持部分页面呈现和Web服务调用等功能。更多详情:
protected void chkCompany_OnCheckedChanged(object sender, EventArgs e)
    {
        if (chkCompany.Checked)
        {
            txtName.Visible = false;
        }

        else
        {
            txtName.Visible = true;
        }
    }
 <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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <script type=text/javascript>
        function CheckedChanged()
    {
       if((document.getElementById('chkCompany').checked))
`{`


      document.getElementById('txtname').Visible=false;
    }

`else`   

    {

     document.getElementById('txtname').Visible=false;

`}`

    }
    </script>
</head>
<body>


     <asp:CheckBox OnCheckedChanged="CheckedChanged" AutoPostBack="false" CssClass="chkCompany" ClientIDMode="Static" ID="chkCompany" runat="server" />
    <asp:TextBox ID="txtname" runat="server"/>

</body>
</html>
-----------------------------------------------------------------------------