User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 391,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,581 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 2156 | Replies: 4
Reply
Join Date: Feb 2007
Posts: 13
Reputation: Aswathy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Aswathy Aswathy is offline Offline
Newbie Poster

Help Issue with Javascript embedded in C#

  #1  
Feb 8th, 2007
Hi,

I have a button click event handler in which i have embedded a javascript code for generating a confirmation box.
Based on the selection in the confirmation box a change is to be made in a hidden field having id hdnField.

In the same btnSubmit_Click() i have a conditional statement written.
Inside the conditional statement i have written a code to make some changes to the page displayed.

All was working fine until I embedde the JavaScript code.
Now after i wrote the script i am facing a problem.
The changes made in the page (which is written inside the conditional statement) is getting displayed only on the second click on the submit button .

Could some one help me to sort this out.
I am pasting the code here.


privatevoid btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Write("<script language=javascript>alert('Clicked on button submit')</script>");
strJavaScript += "\n<script language='javascript' id='confirmJS'>\n";

strJavaScript += " if(confirm( 'Do you want to continue ? ')) {\n";

strJavaScript += "var objForm = document.forms['Form1'];\n";

strJavaScript += "var objFormField = objForm.elements['hdnField'];\n";

strJavaScript += " objFormField.value='1';}\n";
strJavaScript +="</script>";
this.RegisterStartupScript("confirmJS",strJavaScript);
Response.Write(hdnField.Value.ToString());
if(hdnField.Value.Equals(1.ToString()))
{
btnSubmit.Enabled = false;
if(PlaceHolder2.HasControls())
{
foreach(Control c in PlaceHolder2.Controls)
{

/* Got some code here which deals with changes to controls inplace Holder
}
}
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Location: Japan
Posts: 70
Reputation: sedgey is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
sedgey sedgey is offline Offline
Junior Poster in Training

Re: Issue with Javascript embedded in C#

  #2  
Feb 8th, 2007
basically you cant create a function from a button click and have it execute on the client, unless you are using Ajax or something like that, because the page_onload event has already fired and therefore you are only writting the function to the page. "The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised."
sedgey: so little daylight, too much caffeine
MCSD MCAD MCSE
http://www.thefriendlybuilder.co.uk,
http://www.sheetmusicdirect.com
Reply With Quote  
Join Date: Feb 2007
Posts: 13
Reputation: Aswathy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Aswathy Aswathy is offline Offline
Newbie Poster

Re: Issue with Javascript embedded in C#

  #3  
Feb 8th, 2007
Hi,

I am anot using AJAX .

So how do i introduce a confirmation box on click of the submit button so that the user can hink over whether to submit or not.

Thanks,
Aswathy
Reply With Quote  
Join Date: Jan 2007
Location: Japan
Posts: 70
Reputation: sedgey is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
sedgey sedgey is offline Offline
Junior Poster in Training

Re: Issue with Javascript embedded in C#

  #4  
Feb 8th, 2007
in Page_Load why not do something like btnYourButton.Attributes.Add("OnClick","javascript:alert\('hello'\)");

or better would be to use a "return; confirm\('do you really want to do this?'\);"
Last edited by sedgey : Feb 8th, 2007 at 10:24 pm.
sedgey: so little daylight, too much caffeine
MCSD MCAD MCSE
http://www.thefriendlybuilder.co.uk,
http://www.sheetmusicdirect.com
Reply With Quote  
Join Date: Feb 2007
Posts: 13
Reputation: Aswathy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Aswathy Aswathy is offline Offline
Newbie Poster

Solved - Issue with Javascript embedded in C#

  #5  
Feb 8th, 2007
Thanks a million......... It works!!!!!!!
Last edited by Aswathy : Feb 8th, 2007 at 10:51 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 9:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC