Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,399 Programmers. There are 1,446 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Passing C# variable to a javascript confirm box

 
Reply to this topicStart new topic

Passing C# variable to a javascript confirm box

rgfirefly24
post 28 Apr, 2008 - 08:45 AM
Post #1


D.I.C Head

Group Icon
Joined: 7 Apr, 2008
Posts: 135



not sure if posting it here is the right spot but lets hope so.

I have an asp.net application where i need a confirmation box to pop up upon someone clicking a submit button. This confirmation box needs to have a variable passed to it.

the thing is the variable isnt getting passed and also the C# code behind the button isnt getting executed.

CODE

// btnSubmit.Attributes.Add("onclick", "return ConfirmationWindow();");

      //  Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "key", "if(!confirm('Phone number is . Do you want to continue?'))return false;");


        //String scriptText = "return confirm('Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?')";
        
        //ClientScript.RegisterOnSubmitStatement(this.GetType(),
        //    "ConfirmSubmit", scriptText);

        //Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "confirm", scriptText);

User is offlineProfile CardPM
Go to the top of the page
+Quote Post


baavgai
post 28 Apr, 2008 - 09:50 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,078

I'm not real sure I follow the problem. What do you expect to happen and what's actually happening. Which commented out code are you using?

This code should work fine. It's what I could piece together from what you've given.
csharp

protected void Page_Load(object sender, EventArgs e) {
String scriptText = "return confirm('Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?')";
this.btnSubmit.Attributes.Add("onclick",
"java script:return confirm("
+ "'Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?'"
+ ")"
);
}

// if they choose no in the confirm box, they will never get here
// otherwise, this will be processed
protected void btnSubmit_Click(object sender, EventArgs e) {
this.labMessage.Text = "Confirmed: " + this.txtSavePhoneNum.Text;
}


Hope this helps.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

rgfirefly24
post 28 Apr, 2008 - 10:14 AM
Post #3


D.I.C Head

Group Icon
Joined: 7 Apr, 2008
Posts: 135

QUOTE(baavgai @ 28 Apr, 2008 - 09:50 AM) *

I'm not real sure I follow the problem. What do you expect to happen and what's actually happening. Which commented out code are you using?

This code should work fine. It's what I could piece together from what you've given.
csharp

protected void Page_Load(object sender, EventArgs e) {
String scriptText = "return confirm('Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?')";
this.btnSubmit.Attributes.Add("onclick",
"java script:return confirm("
+ "'Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?'"
+ ")"
);
}

// if they choose no in the confirm box, they will never get here
// otherwise, this will be processed
protected void btnSubmit_Click(object sender, EventArgs e) {
this.labMessage.Text = "Confirmed: " + this.txtSavePhoneNum.Text;
}


Hope this helps.


Thanks, mate. I forgot to show which one i wanted to use lol. Anyway here is what i want to happen.. User clicks submit... pop up comes up with confirmation message + phone number. user hits ok to confirm phone number is correct or else clicks cancel if it is not.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

baavgai
post 28 Apr, 2008 - 11:53 AM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,078

QUOTE(rgfirefly24 @ 28 Apr, 2008 - 01:14 PM) *

User clicks submit... pop up comes up with confirmation message + phone number. user hits ok to confirm phone number is correct or else clicks cancel if it is not.


The code offered should do just that, actually. Remember that ASP.NET pages only really do most things via postback. That "btnSubmit_Click" method will never be called until the form on the page is actually submitted. Now, the javascript confirm forces our submit button to preform the onclick action before it does the submit action. If the onclick action is false, it never submits.

Of course, it will just leave the sorry thing lying limp on the page. If you want the postback to always fire and somehow read the confirm results in the button event...

This should do it for you. Note you'll need to stick a hidden field on the form.

csharp

protected void Page_Load(object sender, EventArgs e) {
if (!Page.ClientScript.IsClientScriptBlockRegistered("SaveConfirmBlock")) {
string script = "<script>function SaveConfirm() { "
+ " document.getElementById('hfConfirm').value = "
+ " confirm("
+ " 'Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?'"
+ " );"
+ " return true"
+ " }</script>";
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SaveConfirmBlock", script);
}

this.btnSubmit.Attributes.Add("onclick", "java script:SaveConfirm();" );
}
protected void btnSubmit_Click(object sender, EventArgs e) {
this.labMessage.Text = "Confirmed: " + hfConfirm.Value + "<br/>" + this.txtSavePhoneNum.Text;
}


Also, these cute little incode javascript adds are for people who are afraid of HTML. I think it's much easier to read if you just put them in your page structure.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

rgfirefly24
post 28 Apr, 2008 - 12:09 PM
Post #5


D.I.C Head

Group Icon
Joined: 7 Apr, 2008
Posts: 135

QUOTE(baavgai @ 28 Apr, 2008 - 11:53 AM) *

QUOTE(rgfirefly24 @ 28 Apr, 2008 - 01:14 PM) *

User clicks submit... pop up comes up with confirmation message + phone number. user hits ok to confirm phone number is correct or else clicks cancel if it is not.


The code offered should do just that, actually. Remember that ASP.NET pages only really do most things via postback. That "btnSubmit_Click" method will never be called until the form on the page is actually submitted. Now, the javascript confirm forces our submit button to preform the onclick action before it does the submit action. If the onclick action is false, it never submits.

Of course, it will just leave the sorry thing lying limp on the page. If you want the postback to always fire and somehow read the confirm results in the button event...

This should do it for you. Note you'll need to stick a hidden field on the form.

csharp

protected void Page_Load(object sender, EventArgs e) {
if (!Page.ClientScript.IsClientScriptBlockRegistered("SaveConfirmBlock")) {
string script = "<script>function SaveConfirm() { "
+ " document.getElementById('hfConfirm').value = "
+ " confirm("
+ " 'Phone number entered is ' + document.getElementById('txtSavePhoneNum').value + '. Is this correct?'"
+ " );"
+ " return true"
+ " }</script>";
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SaveConfirmBlock", script);
}

this.btnSubmit.Attributes.Add("onclick", "java script:SaveConfirm();" );
}
protected void btnSubmit_Click(object sender, EventArgs e) {
this.labMessage.Text = "Confirmed: " + hfConfirm.Value + "<br/>" + this.txtSavePhoneNum.Text;
}


Also, these cute little incode javascript adds are for people who are afraid of HTML. I think it's much easier to read if you just put them in your page structure.


i know but we are strickly stuck to doing C#(client rules) We even had to do a full CSS layout instead of using tables. which probably wasnt as bad as they had made it out to be.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

rgfirefly24
post 30 Apr, 2008 - 09:04 AM
Post #6


D.I.C Head

Group Icon
Joined: 7 Apr, 2008
Posts: 135

ok so i've settled on using either this:
CODE
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "key", "if(!confirm('Please confirm your phone number is correct: " + Session["avariable"] + " Do you want to continue?'))return false;");


or the AJAX toolkit confirmbuttonextension.

My problem is that the the variable is getting created within the button submit actionevent. I'm not sure if its even possible to show the value of that variable in the confirm box since technically the confirm box happens before the actionevent is fired correct?

i'm assuming i'm going to have to move all my sql code and such outside the button submit actionevent, would i be correct in this assumption?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 06:10AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month