1

I'm working on ASP.NET Web Forms application. I have an UpdatePanel inside which I have a TextBox and a Button :

<asp:UpdatePanel ID="MyID" 
       UpdateMode="Always"
           runat="server">
<Triggers>
  <asp:AsyncPostBackTrigger ControlID="btnId"/>
</Triggers>
<ContentTemplate>
  <asp:Label id="LblId" Text="The total value is:" runat="server" />
  <asp:TextBox ID="txtId" Text="" readonly="true" runat="server"></asp:TextBox>
  <asp:button id="btnId" Text="Calculate" runat="server" onclick="btnId_Click"/>
</ContentTemplate>

on my code behind I have a handler :

protected void btnId_Click(object sender, EventArgs e)
  {
     txtId.Text = ..
  }

The problem that I have is that when I click the Calculate button it first goes through some(maybe all init events) of the page like Page_Load which I was able to check with the debugger so before my btnId_Click is executed My code firs got through :

protected void Page_Load(..)
{
...
if (Page.IsPostBack)
  {
   ...
  }
}

which is troublesome since I have some logic there which I don't want to be executed when this button is clicked instead, perfectly I would want only the code inside btnId_Click handler to be executed and that to be all. How can achieve something like this?

Leron
  • 9,546
  • 35
  • 156
  • 257
  • So why do you put logic into `Page_Load` that does not belong there? If the page posted back you should use events in 99% of all cases. What do you do in `if (Page.IsPostBack)`? – Tim Schmelter Sep 04 '14 at 12:15
  • @TimSchmelter Do you really think that's the problem? – Leron Sep 04 '14 at 12:22
  • @Levron: if you're in a postback that indicates that the user has triggered an event which you should handle. Put the logic there instead of the `Page_Load`. Then you also avoid this issue. – Tim Schmelter Sep 04 '14 at 12:24
  • Yeap, the problem is that `Page_Load` is executed no matter what, and AFTER that the actual event where my code is as you suggested. – Leron Sep 04 '14 at 12:29
  • No, the problem seems not to be that `Page_Load` is executed but that you've added code to it which gets executed even if you don't want that. That's why i've recommended to remove the code there and add it to the appropriate event handlers. So again: what are you doing in `if (Page.IsPostBack){}`? – Tim Schmelter Sep 04 '14 at 12:34
  • @TimSchmelter For the moment I catch calls from `__doPostBack()` functions like this: `if (Request.Form["__EVENTTARGET"] == "MainContent_btnAddBase") { btnAddBase_Click(this, new EventArgs()); }` but I guess at some point I might add something else. However this is for now. – Leron Sep 04 '14 at 12:39
  • Instead of fiddling around with `Request.Form["__EVENTTARGET"]` use the event handler `btnAddBase`. What is `MainContent`? – Tim Schmelter Sep 04 '14 at 12:44

2 Answers2

2

It looks like you want to avoid a postback. This link explains the details from Microsoft. http://msdn.microsoft.com/en-us/library/ms178208(v=vs.90).aspx

In short you could post to a WebMethod function but that will not have access to code behind global variables or session state. Microsoft says you access your server method without a full postback by doing this.

from the documentation;

Creating a Server Callback Method

In server code, you must create a method that implements the RaiseCallbackEvent method and a method that implements the GetCallbackResult method. The RaiseCallbackEvent method takes a single string argument instead of the usual two arguments that are typically used with event handlers. A portion of the method might look like the following example:

public void RaiseCallbackEvent(String eventArgument)
{
    // Processes a callback event on the server using the event 
    // argument from the client.
}
Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
0

You can't disable that. But you can check what control caused the postback and then run the Page_Load logic based on that.

Use this method:

/// <summary>
/// Retrieves the control that caused the postback.
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
private Control GetControlThatCausedPostBack(Page page)
{
    //initialize a control and set it to null
    Control ctrl = null;

    //get the event target name and find the control
    string ctrlName = page.Request.Params.Get("__EVENTTARGET");
    if (!String.IsNullOrEmpty(ctrlName))
        ctrl = page.FindControl(ctrlName);

    //return the control to the calling method
    return ctrl;
}

The code is from this answer: Which control caused the postback?

You can then edit your Page_Load event to do the following:

protected void Page_Load(..)
{
    if(GetControlThatCausedPostBack(this).Equals(btnId)
    {
        ...
        if (Page.IsPostBack)
          {
           ...
          }
    }
}
Community
  • 1
  • 1
FirstHorizon
  • 118
  • 1
  • 11