簡體   English   中英

在回發時,如何檢查哪個控件導致Page_Init事件中的回發

[英]On postback, how can I check which control cause postback in Page_Init event

在回發時,如何檢查哪個控件導致Page_Init事件中的回發。

protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?

}

謝謝

我看到已經有一些很好的建議和方法建議如何獲得后期控制。 但是我找到了另一個網頁( Mahesh博客 ),其中包含一個檢索回發控制ID的方法。

我將在這里發布一些修改,包括使其成為擴展類。 希望它以這種方式更有用。

/// <summary>
/// Gets the ID of the post back control.
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    string controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = page.FindControl(controlId);
            }
            else
            {
                foundControl = page.FindControl(ctl);
            }

            if (!(foundControl is IButtonControl)) continue;

            control = foundControl;
            break;
        }
    }

    return control == null ? String.Empty : control.ID;
}

更新(2016-07-22):鍵入檢查ButtonImageButton更改為查找IButtonControl以允許識別來自第三方控件的回發。

這里有一些代碼可能適合你(來自Ryan Farley的博客

public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}

直接在表單參數或

string controlName = this.Request.Params.Get("__EVENTTARGET");

編輯 :檢查控件是否導致回發(手動):

// input Image with name="imageName"
if (this.Request["imageName"+".x"] != null) ...;//caused postBack

// Other input with name="name"
if (this.Request["name"] != null) ...;//caused postBack

您還可以遍歷所有控件並檢查其中一個是否使用上面的代碼導致了postBack。

如果你需要檢查哪個控件導致了回發,那么你可以直接將["__EVENTTARGET"]與你感興趣的控件進行比較:

if (specialControl.UniqueID == Page.Request.Params["__EVENTTARGET"])
{
    /*do special stuff*/
}

這假設您只是要比較任何GetPostBackControl(...)擴展方法的結果。 它可能無法處理每種情況,但如果它有效則更簡單。 另外,您不會在頁面上搜索您不關心的控件。

if (Request.Params["__EVENTTARGET"] != null)
{
  if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID"))
  {
    DoWhateverYouWant();
  }
}

假設它是服務器控件,您可以使用Request["ButtonName"]

要查看是否單擊了特定按鈕: if (Request["ButtonName"] != null)

除了以前的答案,要使用Request.Params["__EVENTTARGET"]你必須設置選項:

buttonName.UseSubmitBehavior = false;

要獲得確切的控件名稱,請使用:

    string controlName = Page.FindControl(Page.Request.Params["__EVENTTARGET"]).ID;

暫無
暫無

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

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