views:

74

answers:

3

I am trying to create a customized messagebox using javascript but not getting too far.

    string txtConfirmMessage = "On " + DateTime.Now + ", ";
    txtConfirmMessage = txtConfirmMessage + sUserID;
    txtConfirmMessage = txtConfirmMessage + " added this record. Do you want to continue? ";
    btnSubmit.OnClientClick = @"return confirm('" + txtConfirmMessage + "' Do you want to continue);";  

I need to display a custom message involving the UserID, the date, and the value that the user has enetered. When I click the Submit button, the messagebox does not pop up.

Update:
I know this code is executed at the Page_Load event. However, is it possible to display in this alert box, a value that the user has selected (that action occurs after the page load event) ?

Any ideas to resolve this issue?

+2  A: 

The javascript that you have is not valid, as the "confirm" method taks a single parameter, but you are giving it two bits.

Try this modified version.

string messageFormat = "On {0}, {1} added this record.  Do you want to continue?";
string formattedMessage = string.format(messageFormat, DateTime.Now, sUserID);
btnSubmit.OnClientClick = "return confirm('" + formattedMessage + "');";
Mitchel Sellers
I know this code is executed at the Page_Load event. However, is it possible to display in this alert box, a value that the user has selected (that action occurs after the page load event) ?
user279521
You could use a hidden field and retrieve that value with javascript
citronas
+2  A: 

You just have an extra string after the one you're passing to confirm(), change it to this:

btnSubmit.OnClientClick = @"return confirm('" + txtConfirmMessage + "');";
Nick Craver
+1  A: 

About the update of the question, you could assign an OnClientClick event handler to the button as late as PreRender and the value will be persisted in the ViewState.

bugventure