call a javascript function within code behind

S

sajithkahawatta

i want to call a java script function from code behind.

in page1.aspx page i placed script'

<script language="javascript">
function SetSelected()
{
infoTextBox.select();
}
</script>

i want to highlight text in infotextbox in some cases. so i want to
call this function within code behind.(within if statment).
how can i call this function .i tried with RegisterClientScriptBlock()
but i could not do it .but i think it can be used. can u tell me how a
java script function is called.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

sajithkahawatta said:
i want to call a java script function from code behind.

in page1.aspx page i placed script'

<script language="javascript">
function SetSelected()
{
infoTextBox.select();
}
</script>

i want to highlight text in infotextbox in some cases. so i want to
call this function within code behind.(within if statment).

You cannot execute this method in the code behind, the above method runs in
the client, the "code behind" runs in the server.
Remember that in the server side there is no UI, so you cannot do a Select.

What you can do is trigger the execution of the function from the code
behind, so when the client gets the page back your action will be performed.
 
K

Kevin Spencer

string s = "document.getElementById('" + infoTextBox.ClientID + ".select();"
Page.ClientScriptBlock.RegisterStartupScript(Page.GetType(),
infoTextBox.ClientID, s);

By putting the JavaScript execution code outside of a function declaration,
it is called as soon as it is parsed.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
S

sajithkahawatta

thnks for reply.
dear Kevin Spencer ,
i'm new for asp.net.
i used your code and it give me an error "System.Web.UI.Page' does
not contain a definition for 'ClientScriptBlock''

and i used
Page.RegisterStartupScript(Page.GetType(), infoTextBox.ClientID, s);

then it give a error "No overload for method 'RegisterStartupScript'
takes '3' arguments"

what is the reason?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

sajithkahawatta said:
thnks for reply.
dear Kevin Spencer ,
i'm new for asp.net.
i used your code and it give me an error "System.Web.UI.Page' does
not contain a definition for 'ClientScriptBlock''

and i used
Page.RegisterStartupScript(Page.GetType(), infoTextBox.ClientID, s);

then it give a error "No overload for method 'RegisterStartupScript'
takes '3' arguments"

what is the reason?

The Page.RegisterStartupScript method (used in framework 1.x) only has
two parameters:

Page.RegisterStartupScript("focusInfo", s);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top