I have textboxes and a button.
我有文本框和一个按钮。
The button uses a value which is manipulated by the textbox textchanged event.
该按钮使用由textbox textchanged事件操作的值。
I don't want the button click event to be fired before the value is changed by the textbox changed event.
我不希望在文本框更改事件之前触发按钮单击事件。
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) {
}
4 个解决方案
#1
1
Afaik there is no way to ensure the event order TextChanged
->ButtonClick
. You should use a different approach.
无法确保事件顺序TextChanged->按钮单击。你应该使用不同的方法。
- Move the
TextChanged
logic into theButtonClick
event or - 将TextChanged逻辑移动到ButtonClick事件or
- Make the
TextBox
AutoPostBack=true
, but this requires an additional postback - 使文本框AutoPostBack=true,但这需要额外的回发
So i would suggest to put the logic into the ButtonClick
- and remove the TextChanged
event.
因此,我建议将逻辑放入ButtonClick—并删除TextChanged事件。
#2
1
EDIT: As per another comment you made, if your textboxes are added and removed programatically you could also create a custom user control with your button and textbox, and implement this logic, then programatically add that user control. This is so one button and textbox will be related to each other and not know of others. I'm not sure of the context in which you want to do this, so this approach may not be the best.
编辑:根据你的另一个评论,如果你的文本框被添加和删除,你也可以用你的按钮和文本框创建一个自定义的用户控件,并实现这个逻辑,然后程序添加那个用户控件。因此,一个按钮和一个文本框将相互关联,而不知道其他的。我不确定您想在什么环境下进行此操作,因此这种方法可能不是最好的。
Use a textboxIsDirty flag, which you set and unset in the two event handlers.
使用textboxIsDirty标志,在两个事件处理程序中设置和取消该标志。
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 do the current logic you have in the TextChanged method in the Click method. However, you can bind the tpriceIsDirty flag to the bsave.Enabled property to disable the button altogether if the textbox remains unchanged. Its nicer from a UX perspective. :)
正如在另一个答案中所建议的,我还将在Click方法中的TextChanged方法中执行当前逻辑。但是,您可以将tpriceIsDirty标志绑定到bsave。如果文本框保持不变,则启用属性禁用按钮。从用户体验的角度来看,它更好。:)
Edit: as per a comment you made, you can also add and remove event handlers on the fly. A variation of this approach may be beneficial 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;
}
#3
0
Yes it's possible ... just have a look on below solution .. It's basically a trick created using javascript
but enough powerful ..
是的,这是可能的…看看下面的解决方案。这基本上是一个使用javascript创建的技巧,但是足够强大。
<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 code behind 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);
}
It will work!!
它将工作! !
#4
0
- Enable the Save button's CausesValidation option.
- 启用Save按钮的CausesValidation选项。
- Call the focused editor's DoValidate method.
- 调用焦点编辑器的DoValidate方法。
- Call the form's ValidateChildren method.
- 调用表单的ValidateChildren方法。
https://msdn.microsoft.com/de-de/library/ms158374(v=vs.110).aspx
https://msdn.microsoft.com/de-de/library/ms158374(v = vs.110). aspx
#1
1
Afaik there is no way to ensure the event order TextChanged
->ButtonClick
. You should use a different approach.
无法确保事件顺序TextChanged->按钮单击。你应该使用不同的方法。
- Move the
TextChanged
logic into theButtonClick
event or - 将TextChanged逻辑移动到ButtonClick事件or
- Make the
TextBox
AutoPostBack=true
, but this requires an additional postback - 使文本框AutoPostBack=true,但这需要额外的回发
So i would suggest to put the logic into the ButtonClick
- and remove the TextChanged
event.
因此,我建议将逻辑放入ButtonClick—并删除TextChanged事件。
#2
1
EDIT: As per another comment you made, if your textboxes are added and removed programatically you could also create a custom user control with your button and textbox, and implement this logic, then programatically add that user control. This is so one button and textbox will be related to each other and not know of others. I'm not sure of the context in which you want to do this, so this approach may not be the best.
编辑:根据你的另一个评论,如果你的文本框被添加和删除,你也可以用你的按钮和文本框创建一个自定义的用户控件,并实现这个逻辑,然后程序添加那个用户控件。因此,一个按钮和一个文本框将相互关联,而不知道其他的。我不确定您想在什么环境下进行此操作,因此这种方法可能不是最好的。
Use a textboxIsDirty flag, which you set and unset in the two event handlers.
使用textboxIsDirty标志,在两个事件处理程序中设置和取消该标志。
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 do the current logic you have in the TextChanged method in the Click method. However, you can bind the tpriceIsDirty flag to the bsave.Enabled property to disable the button altogether if the textbox remains unchanged. Its nicer from a UX perspective. :)
正如在另一个答案中所建议的,我还将在Click方法中的TextChanged方法中执行当前逻辑。但是,您可以将tpriceIsDirty标志绑定到bsave。如果文本框保持不变,则启用属性禁用按钮。从用户体验的角度来看,它更好。:)
Edit: as per a comment you made, you can also add and remove event handlers on the fly. A variation of this approach may be beneficial 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;
}
#3
0
Yes it's possible ... just have a look on below solution .. It's basically a trick created using javascript
but enough powerful ..
是的,这是可能的…看看下面的解决方案。这基本上是一个使用javascript创建的技巧,但是足够强大。
<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 code behind 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);
}
It will work!!
它将工作! !
#4
0
- Enable the Save button's CausesValidation option.
- 启用Save按钮的CausesValidation选项。
- Call the focused editor's DoValidate method.
- 调用焦点编辑器的DoValidate方法。
- Call the form's ValidateChildren method.
- 调用表单的ValidateChildren方法。
https://msdn.microsoft.com/de-de/library/ms158374(v=vs.110).aspx
https://msdn.microsoft.com/de-de/library/ms158374(v = vs.110). aspx