2

I have a function tied to ClientScript.RegisterOnSubmitStatement that displays a JQuery UI dialog while an UpdatePanel is updating (it takes a while to update). There are 2 buttons on the page, and I'd like to display different text in the dialog depending on which button is clicked. Is there a way to do something like this:

ServerSide

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

ClientSide

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

Currently, the "source" is the DOM window, not the button.

user1004944
  • 51
  • 1
  • 8
  • You could always use a `hidden` html field that contains the button id which you could determine after the form submit. – ServAce85 Nov 23 '11 at 18:16
  • I need it before the form submits because I want the text in the splash screen to change depending on what button was clicked. It needs to happen on the clientside after client validation (which is why it needs to be RegisterOnSubmitStatement), but before the form actually submits because I need the modal splash screen to be displayed during the asyc postback. – user1004944 Nov 23 '11 at 21:24

2 Answers2

3

You can use the __EVENTTARGET to find the control that initiated the postback:

/// <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; 
} 
James Johnson
  • 45,496
  • 8
  • 73
  • 110
0

OK, I figured out a workaround. I am first going to put a short explaination, and then a much longer one for those in the future that may see this page because hobbyists like me that may not know this stuff backwards and forwards can use this to get some help. (Because I live on googling this stuff.)

The functionallity that I was seeking was to have a modal div with different innerHTML depending on what button was clicked. I also only wanted the div to display if the page was valid, and not all buttons on the page cause validation.

The workout was to create a global variable "ButtonClicked." Then, EVERY button on the page has to have javascript assigned to it's onclick attribute that sets the ButtonClicked variable to the button's id. The script assigned to onclick is run BEFORE page validation. Then, using ClientScript.RegisterOnSubmitStatement, I assigned a function to be called after the page has been successfully validated, but just before actually submitting the page. I can then access the "ButtonClicked" event to see which button was just called and then change the innerHTML of the modal div, then display it. (Then do an asyc postback, then remove the modal div after the postback.)

Why can't I just set the innerHTML of the modal div from the functions that the buttons call themselves? Because the innerHTML that I am putting in there depends on the page being valid and I only know if the page is valid when I get the ShowSplashScreen function. You may also ask why I don't just call the page to be validated from within the javascript function that the button calls. This is because doing this causes validation to be called twice, and there is so much information on the page that it takes almost a full second to validate the page and because the clientside validation function itself has some things in it that can ONLY be called once during validation.

So the end result of all this is that function1 and function 2 are both called by their respective buttons when clicked BEFORE validation, and ShowSplashScreen is called by either button AFTER validation (only if the page is valid), and then I access the global variable to see which one was clicked.

So the entire thing would look like this:

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
}

ServerSide

//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()");
}
user1004944
  • 51
  • 1
  • 8