Sunday, March 11, 2012

Loosing the OnClick event of a Button when OnTextChanged of a TextBox fires

Try to specify asynchronous post back in the asp:UpdatePanel and it should work fine.
Try to change some of codes as the following.
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
????<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
????<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
<Triggers>
????<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName="TextChanged" />
????<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>

</asp:UpdatePanel>
</div
Wish the above can help you.

I've tried your suggestion, but it did not work. When I enter a text into the TextBox and press the Button, only the TextChanged event will fire. I have to press the Button again to get the click event after the text is changed. That sounds not very satisfying, since the user will not understand that behaviour. To me it sounds like a bug...


Sven


Hello.

well, i think this is the expected behavior because when the textbox looses its focus, the framework will automatically start a postback beacuse you've set the autopostback property to true. if you set it to false, then your page will only be submitted when you click the button and i think you'llget both events on the server.


Yes, when I remove AutoPostBack="true" it does work and in some way I can agree that is the expected behaviour when AutoPostBack="true" (using a traditional webpage without Ajax). What I expected with Ajax, is that the TextBox_Changed is fired and after that the button click should be fired. Is there no event queue in the webbrowser that handles these events? Is that not the job of the ScriptManager to detect events caused by the user while a AsyncPostBack is running?

For the user it is a bug, since he clicked the button after a text_changed and nothing will happen. When this is a feature within Ajax, than it is a pitfall for software developers. I removed the AutoPostBack="true" since I cannot be sure that the button click event will be called.

Sven


I had the same issue with onTextChanged event and bottonClick event. After searching for days for a solution, I came up with this and it worked for me. Your code needs to know that the button was clicked when the ontextchanged event was triggered. Since only one event runs at a time, you need to trigger the button routine manually in your code.

place a hidden input in your page:

<

inputtype="hidden"id="SubmitFlag"runat="server"/>

Add this script to the page:

<

scripttype="text/javascript">

function

submitform() {

document.getElementById(

"SubmitFlag").value ='1'

document.forms[0].submit(); }

</script>

add this code to the page_load event.

cmdSave.Attributes.Add(

"onClick","submitform();")

In the OnTextCanged event handler, place this code to check the status of the flag.

If SubmitFlag.Value.Equals("1")Then

SubmitFlag.Value =

""

submitChanges()

EndIf

this code would check if the submit button was clicked (SubmitFlag = 1) then execute your submit routine submitChanges() which is the same routine that is called from the button click event.

I hope this would help.

-z

No comments:

Post a Comment