Share via


Prevent full page refresh during postback

Question

Tuesday, January 26, 2010 3:53 PM

I have 2 checkboxes (yes/no) that when selected cause the entire page to refresh during postback mode.  How can I elimiate this?

here is my current coding for the 2 checkboxes.

ProtectedSub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChangedIf CheckBox1.Checked = True Then

CheckBox2.Enabled =False

Else

CheckBox1.Enabled =

True

CheckBox2.Enabled =

True

End If

End Sub

Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChangedIf CheckBox2.Checked = True Then

CheckBox1.Enabled =

False

Else

CheckBox2.Enabled =

True

CheckBox1.Enabled =

True

End If

End Sub

 

Thank you ~ Stephen

All replies (6)

Friday, January 29, 2010 8:12 AM ✅Answered

Hi,

You can put the checboxes inside the Update panel.

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
      
    <asp:ScriptManager ID="scrManager" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="upPnl" runat="server" ChildrenAsTriggers="true" > 
        <ContentTemplate>
            <asp:CheckBox ID="chk1" runat="server" AutoPostBack="true" Text="Yes"
                oncheckedchanged="chk1_CheckedChanged">
            </asp:CheckBox>
            <asp:CheckBox ID="chk2" runat="server" AutoPostBack="true" Text="No"
                oncheckedchanged="chk2_CheckedChanged">
            </asp:CheckBox>
          </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Hope this helps.

Thanks,

Sumit


Sunday, January 31, 2010 10:23 PM ✅Answered

With JavaScript, that will not cause the postback if you need.

You can define the following script after "body".

<input type="checkbox" id="Check1" onchange="check1_onchanged" />

<input type="checkbox" id="Check2" onchange="check2_onchanged" />
<script type="text/javascript">

function check1_onchanged(){
var check1=$get("Check1");
var check2=$get("Check2");
if(check1.checked){
  check2.disabled=true;
}
else{
  check1.disabled=fase;
  check2.disabled=fase;

}
}

function check2_onchanged(){
if(check2.checked)
   check1.disabled=true;
else
{
   check2.disabled=false;
   check1.disabled=false;
}
}

</script>

 


Tuesday, January 26, 2010 4:39 PM

Put the checkboxes in an updatepanel, they won't post the entire page.


Tuesday, January 26, 2010 5:37 PM

You can use JavaScript , Find my code here : http://forums.asp.net/t/1515421.aspx


Friday, January 29, 2010 7:35 AM

Put the checkbox in updatepanel so that it prevent the full postback

http://www.asp.net/ajax/documentation/live/overview/UpdatePanelOverview.aspx 


Friday, January 29, 2010 8:07 AM

using the update panel is the best option