Onclick not added until after Postback - asp.net

I have the following:
<asp:Button id="SubmitButton" Text="Submit" runat="server" OnClientClick="Do();"
OnClick="SubmitButton_Click"/>
When the page is first rendered, I go to the view source in my browser and see that the onclick="Do();" is not added to my submit button tag. But when I do a post back, and do the view source again, the onclick is added to the submit.
Why would the onclick not be there on the first request?

At least on my machine, I can see OnClick event on both request.

Related

on every postback page opens in a new tab

I have a weird problem in my page. I have a button called Print. I wanted report should come in a new tab, so i wrote some javascript on button's onClientClick. Which works great with no problem at all.
But problem starts now when user comes back on original page again, now here i have several controls which cause postback. Say for example its a dropdownlist. so whenever user changes dropdown item it causes postback which is fine but everytime it opens a new tab on every postback.
hope I am clear in question...
Any help??
Here is a code:
<asp:Button ID="btnshow" runat="server" Text="Show" Font-Bold="true" ForeColor="Black" Width="90px" OnClick="btnshow_Click" OnClientClick ="document.forms[0].target = '_blank';"/>
The issue is document.forms[0].target='_blank' is setting the target on the form not the individual button so a postback triggered by any control will open in a new tab.
You should use a HyperLink control instead of the Button control. The HyperLink control has a Target property which allows you to specify how the link should be opened.
Below is an example taken from the HyperLink documentation. This will render an anchor tag with target="_blank".
<asp:HyperLink ID="lnkPrint" NavigateUrl="http://www.microsoft.com" Text="Print" Target="_blank" runat="server" />

Asp.net Button Firing Other Button's Event

I have a set of options on an ASP.net page where the user may open up one of two lightboxes - either the "Add" lightbox or the "Edit" lightbox.
Each one has a small form for the user to fill out, and then they can hit a button at the bottom of the lightbox to hit submit.
I have the buttons set up within the lightboxes like so:
<asp:Button Id="btnSubmitAdd" runat="server" Text="Submit" OnClick="btnSubmitAdd_Click" />
... then later in the other lightbox...
<asp:Button Id="btnSubmitEdit" runat="server" Text="Submit" OnClick="btnSubmitEdit_Click" />
When i click the "Add" lightbox's submit button, everything behaves just fine.
When i click the "Edit" lightbox's submit button however, it fires "btnSubmitAdd_Click" instead of its own "...Edit_Click" event!
I have checked and re-checked all of the names and events and everything is set up correctly. Anyone have any ideas why this is happening?
Thanks to #MikeGuthrie for leading me down the correct path!
The issue seems to be with asp.net defaulting buttons to type "submit" which submits the entire form, and apparently that means it simply hit the first button's event before the second.
I added have modified the buttons like so and things are working now:
<asp:Button Id="btnSubmitAdd" runat="server" Text="Submit" OnClick="btnSubmitAdd_Click" UseSubmitBehavior="false"/>
<asp:Button Id="btnSubmitEdit" runat="server" Text="Submit" OnClick="btnSubmitEdit_Click" UseSubmitBehavior="false"/>

disabling asp button onclick prevents event from firing

I'm trying to disable an asp button when it is clicked on the client side.
<asp:Button runat="server" ID="btnSubmit" Text="Save" class="Button" OnClick="btnSubmit_Click" CausesValidation="false"></asp:Button>
<script type="text/javascript">
$("input[type=submit],button").click(function (){$(this).attr("disabled", "disabled");});
</script>
when the button is clicked it does post back to the page_load , but it won't go to the btnSubmit_Click method. If I remove the jquery that disables the button it makes it from the page_load to the btnSubmit_Click method.
It seems when the button is disabled from the jquery it is no longer able to wire up to its event. Anyone have any ideas how I can disable the button client side, and still make it to the specific onclick event method?
This is resolved (Yuriy answered), the issue was that I needed to put the disabled code within the form.submit client side code. If not the disabled button was not firing its click event, yet it was still posting back, just not going to the onclick method.
solution:
$('#Form1').submit(function(){ $("input[type=submit],button").click(function (){$(this).attr("disabled", "disabled");});

asp.net textarea postback before submit button

I have an ASP.NET form with a few controls and a submit button at the bottom, all inside an update panel:
<asp:UpdatePanel runat="server" ID="upContent">
<ContentTemplate>
<asp:TextBox runat="server" ID="tbxMyTextBox" AutoPostBack="true" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return doStuff()" OnClick="btnSubmit_Click" />
</ContentTemplate>
</asp:UpdatePanel>
If I write something in the TextBox and click 'submit' immediately (without clicking out of the TextBox first), the changes are not recorded (as seen in the server-side event handler). However, if I write something in the TextBox, and then change focus to another control, an AutoPostback happens through the UpdatePanel and then clicking 'submit' does recognize these changes. How can I force this content to update right as I click the submit button, while still running both the clientside and serverside events attached to it? Thanks!
Is is possible that your text-box gets cleared due to some on-submit/on-click event handler? I will suggest you do use some tool such as Fiddler to inspect the POST data in request. Or you can put a break-point at your server side code and inspect the request data. Look for particularly Request.Form[tbxMyTextBox.UniqueID] - i.e. look for presence of value for your text-box's name (name property at client side which corresponds to UniqueID property at server side). If the request contains the value typed in the text-box then something is happening on server side but good news is you can always retrieve the value from Request object. If the value is not present in the Request object then it means that client side code is clearing the value before the submit.
If an onclick method returns false, it cancels the action that the button would normally perform. As your button would normally submit your form, but it appears not to be submitting, your client side javascript code in doStuff is probably returning false.

asp.net button doesn't cause post back when clicked repeatedly

If I click a button very fast after page load, the post back won't be fired. The following example illustrates this (you have to click the button twice and very fast):
<form id="form1" runat="server">
<div>
<span id="submitText"></span>
<br />
<asp:Button ID="btnSubmitTest" runat="server" Text="Button"
OnClientClick="document.getElementById('submitText').innerText='you should not see this after postback';" />
</div>
</form>
The submitText, the button set's before firing, should not be visible after the postback. But if you click at the button very fast, it happens, that the event doesn't fire.
I think, that the page isn't loaded completely at this moment. Or is there any other reason for that behaviour? How do you handle this?
You will loose all javascript dom updates on postback. What you have explained sounds correct... you see the client click event fire just before page posts back and the dom is reset.
Try the same exercise by populating a textbox. In that instance the value will maintain as the textbox relies on viewstate. You could put a runat="server" on the span, but I believe you would have to manually assign the value to viewstate as I don't believe htmlgenericcontrols automatically utilize viewstate.

Resources