Asp.net 找出哪个按钮正在提交页面的方法?

Asp.net 找出哪个按钮正在提交页面的方法?,asp.net,registerclientscriptblock,clientscript,Asp.net,Registerclientscriptblock,Clientscript,我有一个与ClientScript.RegisterOnSubmitStatement关联的函数,它在UpdatePanel更新时显示一个JQuery UI对话框(更新需要一段时间)。页面上有两个按钮,我希望根据单击的按钮在对话框中显示不同的文本。有没有办法做到这一点: 服务器端 ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", "ShowSplashScreen(this)"); //Use

我有一个与ClientScript.RegisterOnSubmitStatement关联的函数,它在UpdatePanel更新时显示一个JQuery UI对话框(更新需要一段时间)。页面上有两个按钮,我希望根据单击的按钮在对话框中显示不同的文本。有没有办法做到这一点:

服务器端

ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", "ShowSplashScreen(this)");
//Use this code to set a function to be called after the page has 
//been successfully validated. If a button does not cause validation, 
//then that button will always call the function set here
//
//You should also check to see if the script has already been registered 
//for speed purposes, but I'm just demonstrating particular 
//functionality here.
protected void Page_Load(object sender, EventArgs e)
{
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen",
                                           "ShowSplashScreen()");
}
客户端

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

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

您可以使用
\u 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; 
} 
//
///检索导致回发的控件。
///  
///  
///  
私有控件GetControlThatCausedPostBack()
{ 
控件ctrl=null;
//使用事件目标获取发起回发的控件
string ctrlName=Page.Request.Params.Get(“\uu EVENTTARGET”);
如果(!String.IsNullOrEmpty(ctrlName))
ctrl=Page.FindControl(ctrlName);
//将控件返回给调用方法
返回ctrl;
} 

好的,我想出了一个解决办法。我首先要做一个简短的解释,然后是一个更长的解释,将来可能会看到这一页,因为像我这样的业余爱好者,可能不知道这些东西的前后,可以使用它来获得一些帮助。(因为我靠谷歌搜索这些东西为生。)

我所寻求的功能是根据所单击的按钮,使用具有不同innerHTML的模式div。我还只想让div显示页面是否有效,而不是页面上的所有按钮都会导致验证

解决方案是创建一个全局变量“ButtonClick”。然后,页面上的每个按钮都必须将javascript分配给它的onclick属性,该属性将ButtonClick变量设置为按钮的id。分配给onclick的脚本在页面验证之前运行。然后,使用ClientScript.RegisterOnSubmitStatement,我分配了一个函数,该函数将在页面成功验证之后,但就在实际提交页面之前调用。然后,我可以访问“ButtonClicked”事件,查看刚才调用了哪个按钮,然后更改model div的innerHTML,然后显示它。(然后执行asyc回发,然后在回发后删除modal div。)

为什么我不能从按钮自己调用的函数中设置model div的innerHTML?因为我放进去的innerHTML取决于页面是否有效,我只有在获得ShowSplashScreen函数时才知道页面是否有效。您可能还会问,为什么我不直接从按钮调用的javascript函数中调用要验证的页面。这是因为这样做会导致验证被调用两次,而且页面上的信息太多,验证页面几乎需要整整一秒钟的时间,并且因为客户端验证函数本身有一些东西,在验证期间只能调用一次

因此,所有这些的最终结果是,在验证之前单击函数1和函数2时,它们各自的按钮都会调用函数1和函数2,而在验证之后,ShowSplashScreen会由任一按钮调用(仅当页面有效时),然后我访问全局变量以查看单击了哪一个

所以整个事情看起来是这样的:

HTML


您可以始终使用
隐藏的
html字段,该字段包含可在表单提交后确定的按钮id。我需要在表单提交前使用该字段,因为我希望根据单击的按钮更改初始屏幕中的文本。它需要在客户端验证之后发生(这就是为什么它需要注册为SubmitStatement),但在表单实际提交之前,因为我需要在asyc回发期间显示模式启动屏幕。@rick schott:这是我在回发后尝试保留动态控件的选项卡位置时学会的一个巧妙的小技巧:)我需要这个客户端,而不是服务器端
var ButtonClicked = ""; //Needs to be global

//It is not necessary to have a different function for each button, 
//I just needed to for the needs of my page. If Button2 calls function1 
//instead of function2, ButtonClicked is still set to "Button2". 
//It is very important that EVERY button on the page calls something 
//that sets ButtonClicked or you will get an bug if the user ever 
//clicks a button that sets ButtonClicked, and then clicks a button 
//that does not set ButtonClicked (the final function will still 
//think that the first button had just been clicked)

function function1(source){
    ButtonClicked = source.id;
    //whatever else Client Script that needs to be run from this button
}

function function2(source){
    ButtonClicked = source.id;
    //whatever else Clinet Script that needs to be run from this button
}

function ShowSplashScreen(){
    if(ButtonClicked == "Button1")
        //Use JQuery to access the dialog <div> and set the innerHTML
        //to whatever
    else if(ButtonClicked == "Button2")
        //Use JQuery to access the dialog <div> and set the innerHTML 
        //to something else
}
//Use this code to set a function to be called after the page has 
//been successfully validated. If a button does not cause validation, 
//then that button will always call the function set here
//
//You should also check to see if the script has already been registered 
//for speed purposes, but I'm just demonstrating particular 
//functionality here.
protected void Page_Load(object sender, EventArgs e)
{
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen",
                                           "ShowSplashScreen()");
}