Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法检查Gridview中“动态”创建的按钮是否导致回发_C#_Asp.net_Gridview - Fatal编程技术网

C# 有没有办法检查Gridview中“动态”创建的按钮是否导致回发

C# 有没有办法检查Gridview中“动态”创建的按钮是否导致回发,c#,asp.net,gridview,C#,Asp.net,Gridview,正如标题所示, 是否有办法检查Gridview中动态创建的按钮是否导致回发。 因为页面中有多个按钮 我尝试了以下方法: String ButtonID = Page.Request.Params["__EVENTTARGET"]; String ButtonID = Request.Form["__EVENTTARGET"]; String ButtonID = Request.Params["__EVENTTARGET"]; 但这些都返回空值。 我需要识别在GrdiView中动态创建的按钮。

正如标题所示, 是否有办法检查Gridview中动态创建的按钮是否导致回发。 因为页面中有多个按钮

我尝试了以下方法:

String ButtonID = Page.Request.Params["__EVENTTARGET"];
String ButtonID = Request.Form["__EVENTTARGET"];
String ButtonID = Request.Params["__EVENTTARGET"];
但这些都返回空值。
我需要识别在GrdiView中动态创建的按钮。

按钮在单击时创建回发。当您输入onClick事件时,创建一个属性并将其设置为true。

您可以使用下面的函数引用

/// <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 Button || foundControl is ImageButton)) continue;

            control = foundControl;
            break;
        }
    }

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