0

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?

Alex
  • 373
  • 1
  • 3
  • 13
  • _why_ are you using WebForms in 2021? – Dai Oct 28 '21 at 16:20
  • "But why ASP.NET environment set last parameter as false?" - you probably have WebForm's built-in form input validation enabled - and it's validating your form as invalid using client-side logic, but your form doesn't have anywhere for the validation error messages to be displayed. Do you have an `` element? Is anything displayed in the Browser Console? – Dai Oct 28 '21 at 16:22
  • In my experience I find that many WebForms have hardcoded dependencies on _ancient, buggy, insecure, etc_ versions of jQuery which can break easily - so that's one thing to consider. – Dai Oct 28 '21 at 16:23
  • Also, are your `.browser` files up-to-date? Are you using _ASP.NET AJAX_ and ` at all? That too can break postbacks. – Dai Oct 28 '21 at 16:23
  • @Dai, thank you for attention. (1) ASP.NET most secure and fast environment I know. All past year I wrote in Blazor, result is 10 times less performance then ASP.NET classic. And a lot of historical sites uses ASP.NET Classic. Ordinary internet business has no enough money rewrite all sites and follow idiotic Microsoft ideas - forbid jQuery, forbid Visual Studio ASP.NET designer, forbid VB.NET, forbid Visual Studio masters to tune ASP.NET controls and so on. And programmer has no enough time and money to learn new stupid MS technology what give us 10% performance than ASP.NET Classic. – Alex Oct 28 '21 at 16:34
  • @Dai, (2) I have no UpdatePanel, ValidationSummary and .browser befinition – Alex Oct 28 '21 at 16:35
  • Blazor is completely different to WebForms (though I agree it's inappropriate for many things) - and WebForms is **not** "secure and fast" compared to ASP.NET Core, I don't know where you're getting that from. Yegods... – Dai Oct 28 '21 at 16:38
  • @Dai, Blazor is not allow me immediately change code and simple press resfesh button in browser, only this future doing Blazor unacceptable in ANY projects. If I can fully recompiled and restart whole project by I changing one letter in source code - I receive most idiotic development environment existing in whole world. Is I can really wait 5 minutes after I changing one letter in source code? Yes, simple follow for modern Microsoft rules and use Blazor ! And About ASP.NET Classic. My site on ASP.NET created in 2002 year is still working fine and I still support it. And NEVER cracks it 20 ys – Alex Oct 28 '21 at 17:06
  • I don't want to get much into this debate. but webforms was and is killer. With MVC, you google for a grid example, and you split that into 20 different grids. They REALLY hurt the adopting rates and momentum of asp.net and fragmented such examples. (too much on your own). Having to use all those 3rd party controls means you just tossed out the reasons to use asp.net (and that's what occurred - VERY slow + poor adopting rates for new developers for asp.net). But new razor and @page does try and reduced the need for controllers. Going this way quite much means little advantage to using MVC – Albert D. Kallal Oct 28 '21 at 17:27
  • So, the new trends? hot code loads, instant view of changes - all an attempt to GET BACK the kind of visual design experience we had with web forms. Without question, we see even use of signalR and razor pages to allow ease of updating only parts of a web page (just like we had with update panels). so, if you look close, more and more the trend in regards to razor pages is to re-gain, and give back to developers the better user experience they had with web forms. As noted, force everyone to get their own grid control, and thus little advantages over MVC exist - no new developers as a result – Albert D. Kallal Oct 28 '21 at 17:32
  • dear @AlbertD.Kallal, no need new trend. At last 20 year all of us listen from MS about great advantages (1) use VB.NET as most genius improvement of idiotic ancient syntax of C#/JS/JAVA. Pay attention than GOOGLE also create KOTLIN language that is 90% VB.NET, and try to replace ancient idiotic C#/JAVA/JAVASCRIPT syntax. (2) Secondary, all of us during 20 years listen advantage of plain languages code instead idiotic "spaghetti" style of code, actually this is ASP.NET MVC style. (3) And all of programmers used Visual Studio Designer and Masters - this is most productive parts of ASP.NET. – Alex Oct 28 '21 at 17:48

2 Answers2

0

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.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Dear @Albert D. Kallal, in most extremely simple case your answer is great. But in my case I have in this special form tag
    and this site use ordinary IIS extension - URL Rewriter with a lot of various rules web config like. So, this is a reason I use PostBackURL. My page name where I wait postback is PositionForm.aspx and I simple set this URL in Button parameters.
    – Alex Oct 28 '21 at 17:20
  • As noted, if you going to have a button stub for that button, then move the post-back url to the code behind and run the code, and then do a Response.Redirect() in that code behind. I can only say that having both a post-back URL and code behind for that one button does not make sense. If you need that button click to jump to a new page, and also code, then as noted, remove the post-back URL, and put that navigation jump into the code behind for that button. Even with your url re-write rules, a Response.Redirect should work. And thus again no real case can be made for the post-back url. – Albert D. Kallal Oct 28 '21 at 17:38
  • dear @Albert D. Kallal, nothing happens in this case. Can you see my project? This is ordinary ASP.NET Classic site. If yes, I can give u my AnyDesk id.We speaking only about postback from button. – Alex Oct 28 '21 at 18:05
  • hum, you could try OnClientClick="return false". I might be miss-reading this question. (that the issue is both a event stub + postback url). I assume you don't want a post back in the current page? The post-back url should re-direct to the other page. But, as noted, you could still use a code behind event for that button and drop use of post-back url. – Albert D. Kallal Oct 28 '21 at 18:34
  • I need postback URL but have no it. And, of course, you are not correct about redirection with IIS Express. IIS express is not equal to IIS with redirect extension. – Alex Oct 28 '21 at 20:24
0

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.

Alex
  • 373
  • 1
  • 3
  • 13