Is it OK to use __doPostBack()?
Asked Answered
U

5

17

Is it OK to use __doPostBack() or it is not recommended because it is generated from ASP.Net and we are not sure if they changed it in a next version of ASP.Net.

Unskillful answered 27/3, 2011 at 11:7 Comment(0)
H
18

I would advice against it, since it's internal stuff of ASP.NET and was never meant to be used directly.

Instead, what I'm doing when I need to "manually" trigger PostBack is adding hidden "server side" button with the proper OnClick:

<asp:Button id="btnDummy" runat="server" OnClick="Foo" style="display: none;" />

Then the JS is:

document.getElementById("<%=btnDummy.ClientID%>").click();

This way I don't care how post back happens, I just trigger the natural flow of events.

Hellhole answered 27/3, 2011 at 11:18 Comment(0)
I
9

You should not call it directly. You should generate the javascript call by using functions in Page.ClientScript such as:

This will ensure that it's always compatible.

Interpleader answered 27/3, 2011 at 11:10 Comment(0)
I
1

They probably won't change it, but why call it directly?
I think it's a better strategy to trigger the event (a button click for example) and let the control trigger the postback.

I you do need to trigger the postback directly it's recommended to use the Page.ClientScript functions tenfour described.

Interne answered 27/3, 2011 at 11:13 Comment(0)
L
1

I think its perfectly fine to use directly, and have used it without fail, its just a javascript function after all.

Lieb answered 27/3, 2011 at 11:13 Comment(1)
Yes, it's "just a javascript function", but the problem is that there is no guarantee that it will be present in a next version.Averroism
R
0

We use it all over the place and I can't imagine it would ever be stripped out of ASP.NET. I think the fake/hidden button method is just as hokie if not worse. If you use the fake button approach, then you get no option to pass in the __EVENTARGUMENT. I like using __EVENTARGUMENT to pass my data to the server better than creating hidden fields, because it would be more difficult for a hacker to compromise than simply posting back some hidden field to my page. I also don't like the idea of creating fields and controls on the page if they are not even going to be displayed. I am sure that the fake button approach is probably easier for a newbie coder to understand. That being said I am searching for a more elegant way to approach this, but still find myself calling

    __doPostBack('%=UpdatePanel.ClientID%>','MyData') 

in some cases.

Recollected answered 23/8, 2017 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.