5 Replies - 29868 Views - Last Post: 30 April 2008 - 09:04 AM

#1 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 282
  • View blog
  • Posts: 1,489
  • Joined: 07-April 08

Passing C# variable to a javascript confirm box

Posted 28 April 2008 - 08:45 AM

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.

// 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);




Is This A Good Question/Topic? 0
  • +

Replies To: Passing C# variable to a javascript confirm box

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Passing C# variable to a javascript confirm box

Posted 28 April 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.
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.
Was This Post Helpful? 0
  • +
  • -

#3 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 282
  • View blog
  • Posts: 1,489
  • Joined: 07-April 08

Re: Passing C# variable to a javascript confirm box

Posted 28 April 2008 - 10:14 AM

View Postbaavgai, on 28 Apr, 2008 - 09:50 AM, said:

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.
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.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Passing C# variable to a javascript confirm box

Posted 28 April 2008 - 11:53 AM

View Postrgfirefly24, on 28 Apr, 2008 - 01:14 PM, said:

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.

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.
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 282
  • View blog
  • Posts: 1,489
  • Joined: 07-April 08

Re: Passing C# variable to a javascript confirm box

Posted 28 April 2008 - 12:09 PM

View Postbaavgai, on 28 Apr, 2008 - 11:53 AM, said:

View Postrgfirefly24, on 28 Apr, 2008 - 01:14 PM, said:

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.

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.
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 282
  • View blog
  • Posts: 1,489
  • Joined: 07-April 08

Re: Passing C# variable to a javascript confirm box

Posted 30 April 2008 - 09:04 AM

ok so i've settled on using either this:
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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1