954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Issue with Javascript embedded in C#

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("");
strJavaScript += "\n";
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
}
}
}
}

Aswathy
Newbie Poster
13 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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
Junior Poster
131 posts since Jan 2007
Reputation Points: 68
Solved Threads: 9
 

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

Aswathy
Newbie Poster
13 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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?'\);"

sedgey
Junior Poster
131 posts since Jan 2007
Reputation Points: 68
Solved Threads: 9
 

Thanks a million......... It works!!!!!!! :)

Aswathy
Newbie Poster
13 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You