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

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.

Related

OnTextChanged loses focus when AutoPostBack is true

I have a ASP.Net webform that has several textboxes. Some of the textboxes have an OnTextChanged event with AutoPostBack set to true.
When the user enters some text and leaves the textbox, I want some code to run. This part works fine.
The problem is that if a user enters some text, then clicks or tabs to another textbox, the OnTextChanged of the current textbox event fires fine but the textbox that the user clicked on does not keep focus. This causes problems because the user thinks they are on the next textbox but they aren't. And no object seems to have the focus.
Is there anything that can be done to make the next object keep focus while the OnTextChanged event of the current textbox fires?
One option is to use <asp:UpdatePanel> Control.
Facts about using it:
The postback request would be made via AJAX.
It would not recreate the whole page HTML.
When the UpdatePanel updates, it replaces the DIV innerHTML, that would make the textbox lose focus (bad point).
To maintain the focus, you would have to avoid the UpdatePanel from updating when the textbox posts back.
You can avoid the update by setting the UpdateMode and ChildrenAsTriggers properties.
Here is an example:
<asp:UpdatePanel ID="uppTextboxes" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txb1" runat="server" AutoPostBack="true" OnTextChanged="txb1_OnTextChanged" />
<asp:TextBox ID="txb2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Since the whole page is recreated on postback on serverside and the browser will recreate all html on clientside, you have to tell ASP.NET that your TextBox needs focus.
Use Page.SetFocus:
Page.SetFocus(IdOfControl);
However, i would prefer not to postback at all if i can. Can't you use a button that the user has to click after he has entered all necessary data?

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.

AsyncFileUpload and GridView

I've a little problem.
I have in the same page an AsyncFileUpload and a gridview.
How do I update this gridview immediatly after it has been done upload with AsyncFileUpload?
Thanks
I have a solution that i am using, maybe there are better ones but at least it's working:
On your aspx page create a div like:
<div style="visibility: hidden;">
<asp:Button ID="btnHidden" runat="server" OnClick="btnHidden_Click" Text="Button" />
</div>
On your AsyncFileUpload's OnUploadedComplete event write a code that stores the needed informations about the uploaded file in Session variables (since AsyncFileUpload dont have a viewstate).
On your AsyncFileUpload's OnClientUploadComplete place a Javascript function that click's the hidden button and in the codebehind the hidden button's click method is binding your gridview and clearing the session. Thats all! :)
ps.: wrap around your gridview with an updatepanel and your page wont blink

Onclick not added until after Postback

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.

Reverse order of operations for OnClick and OnClientClick?

I have some simple javascript that I'd like to run when a button is clicked, but I also want some postback action to occur on the server. The logical code for this looks like this:
<asp:Button ID="btnOK" runat="server" Text="Save Changes" OnClientClick="UpdateParent();" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="window.close();" />
<script language="javascript" type="text/javascript">
function UpdateParent()
{
window.opener.document.location.reload(true); // or should we postback instead?
window.close();
}
</script>
Basically, a popup window should refresh its parent and then close itself. However... if I call window.close(), the postback does not occur and the button handler is not called. But obviously OnClientClick is called before the postback happens. Am I going to have to emit this javascript in the button handler and run it when the page loads after postback? If so, what is the proper way to do this these days for ASP.NET 2.0?
It's a shame that the code above doesn't work as it's elegantly simple and straightforward.
You have to do the postback before closing the window. Also you want to do the postback before refreshing the parent window, as I guess that the reason to refresh the window is to display the information that you are about to save.
Use the RegisterStartupScript in the ClientScript object to run the code after postback:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.reload(true);window.close();", true);
However, if the parent page is a result of a postback, this would cause a dialog window in the browser informing the user that a post request is needed to reload the page. To avoid this you would have to do something like calling a function in the parent page that could do a postback to update the page.

Resources