[SOLVED] checkbox autopostback without refreshing page asp net

Issue

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

Solution

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.

Answered By – Gopalakrishnan

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *