簡體   English   中英

如何確保在按鈕的單擊事件之前觸發文本框的textchanged事件?

[英]How can I make sure the textchanged event of a textbox is fired before the click event of a button?

我有文本框和按鈕。

該按鈕使用由textbox textchanged事件操縱的值。

我不希望在文本框更改事件更改值之前觸發按鈕單擊事件。

 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) {

    }

Afaik沒有辦法確保事件順序TextChanged - > ButtonClick 你應該使用不同的方法。

  1. TextChanged邏輯移動到ButtonClick事件或
  2. 使TextBox AutoPostBack=true ,但這需要額外的回發

所以我建議將邏輯放入ButtonClick - 並刪除TextChanged事件。

編輯:根據您的另一個評論,如果以編程方式添加和刪除文本框,您還可以使用按鈕和文本框創建自定義用戶控件,並實現此邏輯,然后以編程方式添加該用戶控件。 這是一個按鈕,文本框將相互關聯,不知道其他人。 我不確定你想要這樣做的背景,所以這種方法可能不是最好的。


使用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
    }
}

正如另一個答案所示,我也會在Click方法中使用TextChanged方法中的當前邏輯。 但是,如果文本框保持不變,您可以將tpriceIsDirty標志綁定到bsave.Enabled屬性以完全禁用該按鈕。 從用戶體驗的角度來看,它更好。 :)

編輯:根據您所做的評論,您還可以動態添加和刪除事件處理程序。 這種方法的變體可能對您有益。

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;
}

是的,這是可能的 ...只是看看下面的解決方案..它基本上a trick created using 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>

而在背后的代碼上只添加一行

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

它會工作!!

  1. 啟用“保存”按鈕的“CauseValidation”選項。
  2. 調用焦點編輯器的DoValidate方法。
  3. 調用表單的ValidateChildren方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM