我有两个按钮:

<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" />

如何在pageLoad上确定这两个问题中的哪一个导致了回发?

推荐答案

您可以使用此方法获取导致回发的控件:

/// <summary>
/// Retrieves the control that caused the postback.
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
private Control GetControlThatCausedPostBack(Page page)
{
    //initialize a control and set it to null
    Control ctrl = null;

    //get the event target name and find the control
    string ctrlName = page.Request.Params.Get("__EVENTTARGET");
    if (!String.IsNullOrEmpty(ctrlName))
        ctrl = page.FindControl(ctrlName);

    //return the control to the calling method
    return ctrl;
}

Asp.net相关问答推荐

VS2012 中无法识别的标签前缀或设备过滤器asp

asp.net - 会话 - 多个浏览器选项卡 - 不同的会话?

httpCompression 和 urlCompression 有什么区别?

如何异步渲染局部视图

IIS 中 ASP.net 应用程序的单独应用程序池

取消选中时 ASP.NET CheckBox 不会触发 CheckedChanged 事件

静态字段与会话变量

Automapper - 映射器已初始化错误

是否可以使用 Membership API 更改用户名

如何从 asp:Repeater 循环遍历项目模板中的项目?

会话变量保存在哪里?

如何使用 asp.net 获取 html Select 的选定值

如何在 IIS 7.5 上使用 ASP.NET 表单身份验证保护静态文件?

asp.net 有控制台日志(log)吗?

您使用哪个 .NET Memcached 客户端,EnyimMemcached 与 BeITMemcached?

如何response.write bytearray?

回发后运行javascript函数

RegisterStartupScript 不适用于 ScriptManager、Updatepanel.这是为什么?

如何识别字符串是否包含 unicode 字符?

ModelClientValidationRule 冲突