有没有办法检查"动态"在Gridview中创建Button导致了Postback

时间:2014-09-19 11:03:26

标签: c# asp.net gridview

正如标题所示, 有没有办法检查"动态"在Gridview中创建Button导致了Postback。 由于页面中有多个按钮!

我尝试了以下内容:

String ButtonID = Page.Request.Params["__EVENTTARGET"];
String ButtonID = Request.Form["__EVENTTARGET"];
String ButtonID = Request.Params["__EVENTTARGET"];

但这些都返回Null值。 我需要识别在GrdiView中动态创建的按钮。

2 个答案:

答案 0 :(得分:0)

按钮在单击时创建Postback。输入onClick event时,创建一个属性并将其设置为true。

答案 1 :(得分:0)

您可以使用参考文献下面的功能
On postback, how can I check which control cause postback in Page_Init event

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