我有一个与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
}
目前,“source”是DOM窗口,而不是按钮。
答案 0 :(得分:3)
您可以使用__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;
}
答案 1 :(得分:0)
好的,我想出了一个解决方法。我首先要做一个简短的解释,然后是一个更长的解释,为将来可能会看到这个页面的人,因为像我这样可能不知道这些东西向后和向前的爱好者可以用它来获得一些帮助。 (因为我一直在谷歌搜索这些东西。)
我正在寻找的功能是根据点击的按钮有一个不同innerHTML的模态div。如果页面有效,我也只想显示div,而不是页面上的所有按钮都会导致验证。
锻炼是创建一个全局变量“ButtonClicked”。然后,页面上的每个按钮都必须将javascript分配给它的onclick属性,该属性将ButtonClicked变量设置为按钮的id。分配给onclick的脚本在页面验证之前运行。然后,使用ClientScript.RegisterOnSubmitStatement,我在成功验证页面之后,但在实际提交页面之前分配了一个要调用的函数。然后,我可以访问“ButtonClicked”事件以查看刚刚调用的按钮,然后更改模态div的innerHTML,然后显示它。 (然后做一个asyc回发,然后在回发后删除模态div。)
为什么我不能只从按钮自称的函数中设置模态div的innerHTML?因为我放在那里的innerHTML取决于页面是否有效,我只知道当我获得ShowSplashScreen函数时页面是否有效。您也可以问为什么我不只是在按钮调用的javascript函数中调用页面进行验证。这是因为这样做会导致验证被调用两次,并且页面上有太多信息需要几乎整整一秒来验证页面,因为客户端验证函数本身有一些东西只能被调用一次在验证期间。
所以这一切的最终结果是,在验证之前单击时,function1和function 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()");
}