Home > Enterprise >  ASP.NET Classic don't create button postback with default options of WebForm_PostBackOptions
ASP.NET Classic don't create button postback with default options of WebForm_PostBackOptions

Time:10-29

I have don't understand what going wrong. I have ASP:BUTTON

 <asp:Button runat="server" id="btnQuestion" PostBackUrl="~/PositionForm.aspx"  

OnClick="btnQuestion_Click"></asp:Button>

And have event to processing postback

 protected void btnQuestion_Click(object sender, EventArgs e){}

ASP.NET environment create this event

<input type="submit" name="ctl00$MainContent$btnQuestion" value="Задати питання" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btnQuestion&quot;, &quot;&quot;, false, &quot;&quot;, &quot;../../PositionForm.aspx&quot;, false, false))" id="MainContent_btnQuestion" >

Unfortunately I don't see postback, even I manually perform in browser

WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$btnProposal", "", false, "", "../../PositionForm.aspx", false, false))

I see postback only if I change past argument to true. Those parameters is

  (eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)

so if I manually changing clientSubmit argument from false to true I can receive postback. But why ASP.NET environment set last parameter as false? Or something going wrong in anything else place?

CodePudding user response:

It makes ZERO sense to have both a button click event, or a postback url.

I mean, post-back url means, assumes, suggests, and simple means that button click is to navigate to whole new page. And that of course means that the other new page you navigate to will have post-back = false for the first page load.

However, you can't (should not hang) on both features to the one button. If that button click is to operate on the current page, then of course you need to remove the post-back url.

So, the general operation of that button should not have a post-back url, and does not need one.

So, what is post-back url for then?

TWO BIG HUGE WHOPPER reasons.

First, of course you can thus setup a button to navigate/jump to a new page, and do so without code behind. So that's kind of handy.

but the REALLY big whopper of the reason and feature?

WHEN you use post-back url to jump to another page? YOU ALSO get use of page.Previous!!!!

That means on the new page, you can pick any value, any control from the previous page. Needless to say, this can save you a BOATLOAD of parameters, session() values and who knows what else you might want or have used to pass values.

So even a selected row in a gridview text boxes, the WHOLE shebang of the previous page can be used in the target page.

But ONLY on the first page load!!

Still, this is a HUGE deal.

But, if that button is NOT just jump to another page? Then remove the post-back url, you don't need it, don't want it, and it makes no sense to use post-back URL to the same page you are on. In fact, doing as such quite much re-loads the whole page, and the first post-back will be false.

So, if you do use a post-back url feature of the button, then in the target page, as noted, it is FANTASIC to use, since ALL of the previous controls and their values can be picked up and used in that target page.

So, if I set post-back url, then in the TARGET page, I can now pick up any control from the calling page like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        ' get value of text box 1 from pevious page

        Dim txt1 As TextBox
        txt1 = Page.PreviousPage.FindControl("TextBox1")
        Debug.Print("text box 1 from previous page = " & txt1.Text)


    End If
End Sub

Remember, you ONLY can do the above on he first page load (is postback = false).

However, without question this can save you a BOAT LOAD of parameters or use of session() to pass a boat load of values from the calling page.

In summary:

setting post-back URL of a button is ONLY of use to navigate to a new page.

setting post-back URL means you don't have to write or have a button event
behind, and it MAKES NO SENSE to have a event stub, since clicking the
button is assumed to jump/navigate to a NEW web page.

setting and use of post-back gets you that fantastic magic trick of being
able to use page.Previous to pick up any and all controls from that
previous page.

Bottom line:

If you are going to set use post-back URL feature, then do NOT attempt to have a code behind or event, since that purpose and use assumes the button click is going to navigate to a new page. any other use, or even trying to set post-back url to the same current page you are on does not make sense, nor does having a button event code sub for that button make any sense in this case either.

If you wanting to have a simple click event for that button, then remove the post-back url setting of that button. You don't need nor want as such in the case of a button click to run code behind in the given web page you have.

CodePudding user response:

So, my finally solution to solve this case. This is not site for children or for demo, this is real ASP.NET site with thousand or more buttons in each page.

Postback Parameters in real case

Any my attempt to increase maxRequestLength or other ASP.NET tuning has been unsuccessful. __DoPostBack don't working even I manually perform it.

__DoPostBack

I can not understand exact reason - JS timeout or something other restriction, but my finally solution look as

  <asp:Button runat="server" id="btnQuestion" ClientIDMode="Static" CausesValidation="false" PostBackUrl="~/PositionForm.aspx" Text="Question" ></asp:Button> 

and than parameters needed for me (not all 1000 long hidden fields, of course, but some of hidden fields I need in postback)

 $("#btnQuestion").attr("onclick", "$.ajax({type:'POST',url:'/PositionForm.aspx', data: { '__EVENTTARGET': 'btnQuestion'}});");

of course, in this case server event not link automatically. IsPostback event need to separate and handle by own code.

  • Related