How can I make sure that an event with a text change of a text field is fired before a button click event?

I have text fields and a button.

The button uses the value that is processed by the event modified by the text field.

I do not want the button click event to fire before the value is changed using an event with the text field changed.

void tprice_TextChanged(object sender, EventArgs e) { idtextbox tsender = (idtextbox)sender; decimal value = 0; decimal.TryParse(tsender.Text, out value); if (area_updates.ContainsKey(tsender.id)) { area_updates[tsender.id].price = value; } else { area_updates.Add(tsender.id, new area_update(tsender.id) { price = value }); } Session["area_updates"] = area_updates; } protected void bsave_Click(object sender, EventArgs e) { } 
+5
source share
5 answers

Afaik there is no way to ensure the order of TextChanged events. You should use a different approach.

  • Move the TextChanged logic to the TextChanged event or
  • Make TextBox AutoPostBack=true , but this requires additional AutoPostBack=true

So, I would suggest putting the logic in ButtonClick - and removing the TextChanged event.

+1
source

EDIT:. According to another comment you made, if your text fields are added and removed programmatically, you can also create a custom control using the button and text field and implement this logic, and then programmatically add that user control. This is so that one button and text field will be connected to each other and do not know the others. I am not sure about the context in which you want to do this, so this approach may not be the best.


Use the textboxIsDirty flag that you set and disable in two event handlers.

 private bool tpriceIsDirty = false; void tprice_TextChanged(object sender, EventArgs e) { tpriceIsDirty = true; // Do work } protected void bsave_Click(object sender, EventArgs e) { if (tpriceIsDirty) { tpriceIsDirty = false; // Do work } } 

As suggested in another answer, I would also execute the current logic in the TextChanged method in the Click method. However, you can bind the tpriceIsDirty flag to the bsave.Enabled property to completely disable the button if the text field remains unchanged. This is better in terms of UX. :)

Edit: According to the comment you made, you can also add and remove event handlers on the fly. A variant of this approach may be useful to you.

 void tprice_TextChanged(object sender, EventArgs e) { if (bsave.Click == null) { bsave.Click += bsave_Click; } .... } protected void bsave_Click(object sender, EventArgs e) { bsave.Click = null; } 
+1
source

I personally prefer not to have this code in most event handlers. Indeed, when saving, you could probably get the values ​​for parsing the text and do it there if you need to. In addition, you can mark the properties changed on the objects you are working with and update them as it happens, for example, close or cancel the form, or even by timer if you are worried about data loss. But this is just my opinion.

To solve the problem you have, you can add an event handler for the Leave textbox events. To click the Save button, you must first leave the text box, so perhaps you can make your own parsing logic.

0
source

Yes, it is possible ... just take a look at the solution below. This is basically a trick created using javascript but powerful enough ..

 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script> function EnsureNoSubmit(txt) { //alert('Ensured no submit');//test cases document.getElementById('<%=hdn.ClientID%>').value = false; } function isOkToSubmit() { var needSubmit = document.getElementById('<%=hdn.ClientID%>').value; //if (needSubmit != '') {alert('No Submit now');}else {alert('Ok with Submit');}//test cases return needSubmit == ''; } function MakeSureSubmit() { //alert('no submit revoked');//test cases document.getElementById('<%=hdn.ClientID%>').value = ''; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:HiddenField runat="server" ID="hdn" /> <asp:TextBox AutoPostBack="true" runat="server" ID="txt" onchange="EnsureNoSubmit();" OnTextChanged="txt_TextChanged"></asp:TextBox> <asp:Button Text="Click Me" ID="btnClickMe" OnClientClick="return isOkToSubmit();" OnClick="btnClickMe_Click" runat="server" /> </div> </form> </body> </html> 

And on the code side by side, add only one line

 protected void txt_TextChanged(object sender, EventArgs e) { //Your code is here Page.ClientScript.RegisterStartupScript(this.GetType(),"Anything","MakeSureSubmit();",true); } 

He will work!

0
source
  • Enable the save button. Calls the "Validation" option.
  • Call the focused editor of the DoValidate method.
  • Call the ValidateChildren method.

https://msdn.microsoft.com/de-de/library/ms158374(v=vs.110).aspx

0
source

Source: https://habr.com/ru/post/1243040/


All Articles