【问题标题】:Way to find out which button is submitting page?找出哪个按钮正在提交页面的方法?
【发布时间】:2011-11-23 18:13:20
【问题描述】:

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

服务器端

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

客户端

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

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

【问题讨论】:

  • 您始终可以使用hidden html 字段,其中包含您可以在提交表单后确定的按钮 ID。
  • 在表单提交之前我需要它,因为我希望启动屏幕中的文本根据点击的按钮而改变。它需要在客户端验证之后发生在客户端(这就是它需要 RegisterOnSubmitStatement 的原因),但在表单实际提交之前,因为我需要在 asyc 回发期间显示模态启动屏幕。

标签: asp.net registerclientscriptblock clientscript


【解决方案1】:

您可以使用__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; 
} 

【讨论】:

  • @rick schott:这是我不久前在回发后尝试为动态控件保留选项卡位置时学到的一个漂亮的小技巧 :)
  • 我需要这个客户端,而不是服务器端
【解决方案2】:

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

我正在寻求的功能是根据单击的按钮有一个具有不同 innerHTML 的模态 div。我也只想在页面有效时显示 div,而不是页面上的所有按钮都会导致验证。

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

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

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

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

HTML

<!-- This is a simplified version of the HTML that <asp:Button> is going to 
     output in the actual HTML -->
<input type="submit" id="Button1" value="Load Page" onclick="function1(this)">
<input type="submit" id="Button2" value="Save Page" onclick="function2(this)">

JavaScript

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()");
}

【讨论】:

    猜你喜欢
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多