By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Handling Return Confirm()

P: 7
Hi All,
I am tring to figure out how to handle input from the confirm dialog. I am using a gridview on which the user can check some checkboxes and then clicks <asp:button> to submit.
I check a condition in the csharp code and based on a condition , I need to display a confirm dialog to the user. So when I do the check on the condition (Server Side) I use RegisterStartupScript to generate the Client Side script for the confirm prompt.

Expand|Select|Wrap|Line Numbers
  1.             if (resultRow != null)
  2.             {
  3.                 if (!Page.IsClientScriptBlockRegistered("InfoMessage"))
  4.                 {
  5.                     string strNumScript = "<script> confirm('Item has already been assigned. Do you wish to continue? ');</script>";
  6.                     Page.ClientScript.RegisterStartupScript(GetType(), "InfoMessage", strNumScript);
  7.                 }
  8.             }
  9.  
I have seen lots of examples where the script is added as an attribute to a control. I am confused by these examples and most seem to be more straight forward examples than what I am trying to do.

Can I use the input from the confirm to control execution in the csharp code. Am I on the wrong track here?

Regards
Oct 23 '07 #1
Share this Question
Share on Google+
9 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

So you want the return value (true or false) from the confirm to be passed or become available in your C# code?
Oct 23 '07 #2

P: 7
Hello, thanks for your reply.
yes I wish to access the true / false value.

I am still trying to figure it out. I am trying to set a hidden field with the result. So far I've come up with ....

Expand|Select|Wrap|Line Numbers
  1. if (shippingRowExists == true)
  2.                         {
  3.                             if (!Page.IsClientScriptBlockRegistered("BoxNumberInfoMessage"))
  4.                             {
  5.                                 string strNumScript = @"<script language=""javascript"" type=""text/javascript"">document.aspnetform.hidUserConfirm.value = return confirm('Item has already been assigned. Do you wish to reassign? ');</script>";
  6.                                 Page.ClientScript.RegisterStartupScript(GetType(), "BoxNumberInfoMessage", strNumScript);
  7.                             }
  8.                         }
which renders as

[HTML]<script language="javascript" type="text/javascript">document.aspnetform.hidUserConfirm.val ue = return confirm('Item has already been assigned. Do you wish to reassign? ');</script>
<script type="text/javascript">[/HTML]

Its still not working quite right. The confirm is not opening.
Oct 23 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Remove "return" from your code. Also make sure that the code runs after the page has loaded.
Oct 23 '07 #4

P: 7
Hello again, you are right about dropping the return. The confirm is now appearing. So thanks for that.

So I am setting the client side hidden field with the outcome from the confirm. The 'RegisterStartupScript' renders the javascript at the bottom of the page. My difficulty is where to catch the user input. I am not sure where in the C sharp the code is when waiting for the user input

I am beginning to think I am not going to be able to achieve what I set out to do using this method. Interestingly (and probably no surprise to most people) the javascript cannot set a server side hidden field - and the server side canot see the ordinary html hidden field. I am still curious if it is possible to capture the user response?

As Homer would put it ..... doh!
Oct 24 '07 #5

acoder
Expert Mod 15k+
P: 16,027
So I am setting the client side hidden field with the outcome from the confirm. The 'RegisterStartupScript' renders the javascript at the bottom of the page. My difficulty is where to catch the user input. I am not sure where in the C sharp the code is when waiting for the user input
The code will set the value of the hidden field. What do you mean "where to catch the user input"? When you submit the form, the hidden form value will be available to your server-side code.
Oct 24 '07 #6

P: 7
I click the assignment button - the page is posted back.
The row items in the gridview are checked for previous assignment. If already assigned, the confirm prompt code is executed - then the confirm is rendered. User clicks and the page finishes loading. It is all part of the same postback. Is this a crazy plan.

I did investigate if there is an event that gets raised when the page completes loading - I was hoping to do the check on the hidden field here but this didn't come good either.
Oct 24 '07 #7

acoder
Expert Mod 15k+
P: 16,027
I click the assignment button - the page is posted back.
The row items in the gridview are checked for previous assignment. If already assigned, the confirm prompt code is executed - then the confirm is rendered. User clicks and the page finishes loading. It is all part of the same postback. Is this a crazy plan.
No, it's not a crazy plan.

You probably want to avoid repeated postbacks. For this, you can use Ajax.

You have two choices: either check the assignment on the first page (before the assignment button is clicked) and show up a confirm there if there has been a previous assignment.

The second choice is to show the confirm as you are doing and then use Ajax to run the server-code.

I think the first option makes more sense.
Oct 24 '07 #8

P: 7
I went with option 1. I amended the prompt to the user to also confirm assignment and swapped the Assignment button with a Confirm Assignment, Cancel button. Not the most elegant soln perhaps but it does whats needed.

Thanks for all your very good feedback.
Oct 25 '07 #9

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad to see that you got it working. Post again if you have any more questions.
Oct 25 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.