Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access javascript function in code behind 1

Status
Not open for further replies.
Apr 11, 2002
193
IN
Hi All,

I have a server side button and i have written a if condition on the click event. In the else part i want to access a javascript function which is written in the HTML markup section. I want to pass a server side textbox id to the javascript function and the javascript function should setfocus on that textbox.

Thanks,
Manish
 
You can use Page.RegisterStartUpScript (or ClientScript.RegisterStartUpScript depending on which version of the framework you are using) to call the function (or you could simply create it here if it isn't being used anywhere else).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Assuming you have the client script configured for use by your page, what you'd do is something like this:

Code:
//page load
myButton.Attributes.Add( 
   "onclick", "return jsFunction(" + myTextBox.ClientId + ");" );

or something similar. The basic idea is that you reference the ClientId of the TextBox and have a function that returns false if you want to cancel PostBack.
 
Hi,

Thanks for the reply. Its true that adding attributes to the button will execute the js function, but it will get executed as soon as i click on the button. What i want is, i have a if else condition in the click event of the button. When it goes to the else part then only the js function should get executed.

Thanks,
Manish
 
Assuming you want this all to happen client-side then why not have your evaluation condition be part of the function that click calls?

e.g.

Code:
//conditionally focus on a passed control based on some criteria
function jsFunction(controlToFocusOnConditionally)
{
   if( /*you don't want to set focus on the control*/ )
   {
        return true;
   }
   else
   {
        controlToFocusOnConditionally.focus();
        return false;
   }
}
 
Yet another server vs. client mess: you cannot "run" any client-side code in your server-side code. In terms of what the user sees, the purpose of the server-side code is to generate a response stream.

You can certainly have conditional code server-side. You can have each condition affect the final response given to the user. The response stream can definitely include JavaScript code.

ca8msm gave you the answer: register a startup-script.

Code:
Page.RegisterStartUpScript()

Research it!

Thomas D. Greer
 
Is he talking about the server or client-side click event, though? If he's talking about client-side then Page.RegisterStartupScript won't be of much use, he'll have to hook into the client-side button click event (say if he wants to set focus to a required field).

Also, on a sidenote if you want to set the focus on a control in Framework 2.0, you wouldn't have to use Page.ClientScript.RegisterStartupScript(), you could just use Control.Focus().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top