asp.net - 如何找出哪个按钮正在提交页面?

标签 asp.net registerclientscriptblock clientscript

我有一个与 ClientScript.RegisterOnSubmitStatement 绑定(bind)的函数,该函数在 UpdatePanel 更新时显示 JQuery UI 对话框(更新需要一段时间)。页面上有 2 个按钮,我想根据单击的按钮在对话框中显示不同的文本。有没有办法做这样的事情:

服务器端

ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", "ShowSplashScreen(this)");

客户端

function ShowSplashScreen(source) { 
// Do stuff depending on the button that was clicked
}

目前,“源”是 DOM 窗口,而不是按钮。

最佳答案

您可以使用__EVENTTARGET来查找发起回发的控件:

/// <summary> 
/// Retrieves the control that caused the postback. 
/// </summary> 
/// <param name="page"></param> 
/// <returns></returns> 
private Control GetControlThatCausedPostBack() 
{ 
    Control ctrl = null; 

    //use the event target to get the control that initiated the postback 
    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 - 如何找出哪个按钮正在提交页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247091/

相关文章:

c# - 将 XML 数据写入 ASP.NET 页面

c# - 将内容复制到流时出现 System.Net.Http.HttpRequestException 错误

c# - 带有 MySQL 的 ASP.NET MVC EF 需要 .dll 及其用法?

c# - 使用 ClientScript 后收到 System.FormatException

javascript - 如何通过mvc4中的提交按钮传递自定义参数

javascript - Yii 注册脚本 : Adding php parameters to javascript

javascript - 如何以编程方式将 javascript 放入 <head> block 中?

c# - ClientScript.RegisterClientScriptBlock?

javascript - asp 按钮和历史返回 onclientclick

c# - 如何在 ASP.NET c# 中执行多个 ClientScript.RegisterStartupScript?