How to avoid Page_Load() on button click?

后端 未结 6 1722
鱼传尺愫
鱼传尺愫 2021-01-18 03:01

I have two buttons, preview and Save. With help of preview button user can view the data based on the format and then can save.

But when preview is clicked, one text

相关标签:
6条回答
  • 2021-01-18 03:28

    you can put your code in

    Page_Init()
    {
       //put your code here
    }
    

    instead of

    Page_Load()
    {
       //code
    }
    
    0 讨论(0)
  • 2021-01-18 03:28

    Try adding this to the buttons properties in the aspx page.

    OnClientClick="return false;"
    
    0 讨论(0)
  • 2021-01-18 03:33
    form1.Action = Request.RawUrl; 
    

    Write this code on page load then page is not post back on button click

    0 讨论(0)
  • 2021-01-18 03:40

    From what I am understanding the preview button is causing a postback and you do not want that, try this on your preview button:

    <asp:button runat="server".... OnClientClick="return false;" />
    

    similarly this also works:

    YourButton.Attributes.Add("onclick", return false");
    

    Edit:

    it seems the answer to the user's question was simple change in the HTML mark up of the preview button

    CausesValidation="False"
    
    0 讨论(0)
  • 2021-01-18 03:40

    I had the same problem and the solution above of "CausesValidation="False"" and even adding "UseSubmitBehavior="False"" DID NOT work - it still called "Page_Load" method.

    What worked for me was adding the following line up front in Page_Load method.

    if (IsPostBack) return; 
    

    I am mentioning this if it helps someone (I meant to comment above but StackOverflow did not allow me to comment because I am a new user - hence a new reply).

    0 讨论(0)
  • 2021-01-18 03:43

    Use Page.IsPostback() in your aspx code (server-side). Like this:

    private void Page_Load()
    {
        if (!IsPostBack)
        {
            // the code that only needs to run once goes here
        }
    }
    

    This code will only run the first time the page is loaded and avoids stepping on user-entered changes to the form.

    0 讨论(0)
提交回复
热议问题