views:

73

answers:

3

Hi all,

I have explicitly added __doPostBack() on Button onclientClick event .

<asp:Button ID="Button1" runat="server" Text="Button" 
         OnClientClick="__doPostBack('Button1','')"/> 

When I am clicking the button the Page_Load is calling twice. But if I am adding below code inside page load ,page load is calling only once on button click.

Button1.Attributes.Add("onClientClick", "__doPostBack('Button1','')");

Again if i add with return false it is giving me it calling only once page load on click

 <asp:Button ID="Button1" runat="server" Text="Button" 
         OnClientClick="__doPostBack('Button1','');return false;"/>

and return true is giving me again twice page load ,but adding return true or false in attribute.add code is giving the same result ,only one page load call.

Button1.Attributes.Add("onClientClick", "__doPostBack('Button1','');return true;");

I am not able to understand what is going on exactly when I tried to add __doPostBack in different way. Please help. Thanks

A: 

Hi Ashar, a Button already to a PostBack, so why do you need to call it from client side?

Anyway, the page_load in your case is called twice I think because one time is done by the OnClientClick, the second time server side.

A: 

You could try viewing the rendered output (i.e. access your ASPX page in your browser and view page source) and seeing what the resulting HTML/Javascript looks like. Perhaps the __doPostBack is being called twice.

happyCuddleTime
A: 

By placing the OnClientClick, then the asp.net render the onlick function on client size with both your code and a doPostBack.

So its called 2 times because one its called by self, and one because you added.

Aristos