Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox autopostback without refreshing page asp net

Tags:

jquery

c#

asp.net

I have this checkbox that I need to be AutoPostBack="True" so that I can trigger OnCheckedChanged="chkCompany_OnCheckedChanged". The problem is that I dont want the page to be refreshed and redirected, I want the user to stay put exactly where they are.

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;
        }
    }
like image 405
Malphai Avatar asked Dec 20 '25 06:12

Malphai


2 Answers

You should use UpdatePanel control to do this

 <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>

Keep your code inside update pannel.

like image 59
Gopalakrishnan Avatar answered Dec 21 '25 19:12

Gopalakrishnan


you can use javascript to do this,if Update panel does not work.


<!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>
-----------------------------------------------------------------------------
like image 25
Pradeep Gokul Avatar answered Dec 21 '25 19:12

Pradeep Gokul