sourcetip

postback에서 postback의 원인이 되는 컨트롤을 Page_에서 확인하려면 어떻게 해야 합니까?Init 이벤트

fileupload 2023. 6. 7. 23:05
반응형

postback에서 postback의 원인이 되는 컨트롤을 Page_에서 확인하려면 어떻게 해야 합니까?Init 이벤트

postback에서 postback의 원인이 되는 컨트롤을 Page_에서 확인하려면 어떻게 해야 합니까?Init 이벤트.

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

}

감사해요.

이미 포스트 컨트롤을 되돌리는 방법을 제안하는 몇 가지 훌륭한 조언과 방법이 있다는 것을 알았습니다.하지만 저는 포스트백 컨트롤 ID를 검색하는 방법이 있는 다른 웹 페이지(Mahesh 블로그)를 찾았습니다.

확장 수업으로 하는 것을 포함하여 조금 수정하여 여기에 올리겠습니다.바라건대 그것이 그런 방식으로 더 유용하기를 바랍니다.

/// <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):유형 검사:Button그리고.ImageButton을 찾기 위해 변경된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;
}

포스트백의 원인이 된 컨트롤을 확인해야 하는 경우 직접 비교할 수 있습니다.["__EVENTTARGET"]원하는 컨트롤로 이동할 수 있습니다.

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

이것은 당신이 단지 어떤 것으로부터 결과를 비교할 것이라고 가정합니다.GetPostBackControl(...)어쨌든 확장 방법.모든 상황을 처리할 수는 없지만, 효과가 있다면 더 간단합니다.또한 처음부터 신경 쓰지 않았던 컨트롤을 찾기 위해 페이지를 뒤지지 않습니다.

매개 변수에 직접 정보를 입력합니다.

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

또한 위의 코드를 사용하여 모든 컨트롤을 반복하고 그 중 하나로 인해 포스트백이 발생했는지 확인할 수 있습니다.

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;

언급URL : https://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event

반응형