簡體   English   中英

MasterPage按鈕單擊事件不會在第一次單擊時觸發,而在第二次單擊時觸發

[英]MasterPage button click event doesn't fire on the first click but on the second click

嗨,謝謝您的幫助。 這是我的問題:

  • 我有一個網頁,分為一個母版頁和一個主頁。
  • 這兩個頁面中的任何內容都沒有動態設置(沒有代碼隱藏),所有內容均在.aspx頁面上設置。
  • 在母版頁上,我有一個“保存”按鈕。
  • 在主頁上,我有幾個文本框。
  • 主頁上的文本框具有一個事件(void SumAll(...)),當焦點離開這些復選框中的任何一個時,將觸發該事件。
  • 在母版頁上,我有一個單擊事件,它連接到“保存”按鈕。

這是發生了什么:

  • 我對主頁上的任何一個文本框進行了更改,但焦點並沒有使該文本框離開(沒有SumAll()事件立即觸發)。
  • 然后,我單擊母版頁中的“保存”按鈕。

我希望MasterPage的“保存”按鈕單擊事件能夠觸發,但它永遠不會觸發! 觸發主頁面textchange(SumAll())事件,但不會觸發MasterPage click事件。

如果我再次單擊MasterPage的“保存”按鈕,則MasterPage單擊事件將成功觸發,但是,當然不會發生Maintext更改事件。 當我只單擊MasterPage的“保存”按鈕而不在主頁上進行任何更改時,情況也是如此。

這是我的代碼示例。 主頁textchanged事件:

protected void SumTheAmounts(object sender, EventArgs e)
    {
        Decimal lease = 0.00M;
        Decimal securityDeposit = 0.00M;
        Decimal miscellanious = 0.00M;
        Decimal sumTotal = 0.00M;

        Decimal.TryParse(txtMiscellaneousAmount.Text, out miscellanious);
        Decimal.TryParse(txtSecurityDepositAmount.Text, out securityDeposit);
        Decimal.TryParse(txtLeaseAmount.Text, out lease);

        sumTotal = miscellanious + securityDeposit + lease;
        txtAmount.Text = sumTotal.ToString();

        // Set focus to the next textbox
        TextBox tb = (TextBox)sender;
        switch (tb.ID)
        {
            case "txtLeaseAmount":
                txtSecurityDepositAmount.Focus();
                break;
            case "txtSecurityDepositAmount":
                txtMiscellaneousAmount.Focus();
                break;
            case "txtMiscellaneousAmount":
                txtMiscellaneousAmount.Focus(); // Send back to the same. .
                break;
            default:
                break;
        }
    }

和我的MasterPage的“保存”按鈕單擊事件:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        // Save every little thing...
        switch (ContentPageLoaded)
        {
            case "CheckRequest":
                ((CheckRequest)ContentMain.Page).UpdateMe();
                break;
            case "PicturesLoad":
                ((PicturesLoad)ContentMain.Page).UpdateMe();
                break;
            default:
                break;
        }
    }

標記:

<asp:Panel ID="pnlSave" CssClass="ButtonClass" runat="server">
   <asp:Button ID="btnSave" style="width:160px; font-size:small; position:relative; color:Blue; top: 0px; left: 0px;" runat="server" Text="Save" onclick="btnSave_Click" CausesValidation="False" /> 
</asp:Panel>

PageLoad:

if (!IsPostBack) 
{ 
   Button saveButton = (Button)Master.FindControl("btnSave"); 
   branchName = (string)Session["BranchName"]; 
   branchLegalData = new Classes.BranchLegalData(); 
   if ((branchName != null) && (branchName.Length > 0)) 
   { 
      FillThisPageData(); 
   } 
} 
else { } 
Master.ContentPageLoaded = "CheckRequest"; 

任何幫助都非常感謝! 謝謝

這是黑暗中的鏡頭,但是請嘗試為所有文本框設置autopostback =“ false”。 我懷疑可能發生的情況是,當您將注意力集中在文本框上時,您將觸發兩次回發,然后單擊一次。

僅當將autopostback設置為true時,才會發生這種情況。

感謝大家!

在文本框上設置autopostback =“ false”可行! 我認為發生的事情是,使用autopostback =“ true”時,文本框將觸發它自己的文本更改事件,並且由於某種原因,之后不會觸發MasterPage按鈕單擊事件? 有人可以告訴我為什么觸發文本框事件而不觸發按鈕事件嗎???

無論如何,此問題已得到解決,我非常感謝您的投入...你們真棒!

史蒂夫

暫無
暫無

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

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