Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. remove autopostback from button

remove autopostback from button

Scheduled Pinned Locked Moved ASP.NET
csharpquestionhtmlasp-netgraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tiziacaia
    wrote on last edited by
    #1

    how can I remove autopostback from a button? i want change the document location cliccking on the button but my location don't change. I don't understand why! somebody can help me? happy new year to all and thanks a lot for help! -------------->here my code of html: <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %> WebForm1 function change(){ alert("qui"); document.location.href="ciao.htm"; }

    -------------->and here the codebehind: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebApplication2 { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Button1.Attributes.Remove("AutoPostBack"); Button1.Attributes.Add("OnClick","change();"); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }

    M 1 Reply Last reply
    0
    • T tiziacaia

      how can I remove autopostback from a button? i want change the document location cliccking on the button but my location don't change. I don't understand why! somebody can help me? happy new year to all and thanks a lot for help! -------------->here my code of html: <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %> WebForm1 function change(){ alert("qui"); document.location.href="ciao.htm"; }

      -------------->and here the codebehind: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebApplication2 { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Button1.Attributes.Remove("AutoPostBack"); Button1.Attributes.Add("OnClick","change();"); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, Because you use a WebControls.Button control, then at the client side it is rendered as a submit button, so you can't cancel the submit action as you are doing now to go to another page. In this case, there are a couple of ways to do that: + You simply cancel the submit action by adding the onsubmit="return false;" for the form element. This will prevent the page from going back to the server even you use other controls that can cause the page to submit.

      <form id="Form1" onsubmit="return false;" method="post" runat="server">

      + You simply cancel the submit action caused by the button by adding "return false;" after the change() function, the sample code is something like this:

      Button1.Attributes.Add("onclick","change(); return false;");

      + You simply use an html button control instead of a WebControls.Button control:

      <INPUT type="button" value="Change" onclick="change();">

      T 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, Because you use a WebControls.Button control, then at the client side it is rendered as a submit button, so you can't cancel the submit action as you are doing now to go to another page. In this case, there are a couple of ways to do that: + You simply cancel the submit action by adding the onsubmit="return false;" for the form element. This will prevent the page from going back to the server even you use other controls that can cause the page to submit.

        <form id="Form1" onsubmit="return false;" method="post" runat="server">

        + You simply cancel the submit action caused by the button by adding "return false;" after the change() function, the sample code is something like this:

        Button1.Attributes.Add("onclick","change(); return false;");

        + You simply use an html button control instead of a WebControls.Button control:

        <INPUT type="button" value="Change" onclick="change();">

        T Offline
        T Offline
        tiziacaia
        wrote on last edited by
        #3

        thanks thanks thanks a lot! I choose the second way.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups