469,352 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

call a javascript function within code behind

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.

Jun 20 '07 #1
4 38468
Hi,

"sajithkahawatta" <sa*************@gmail.comwrote in message
news:11*********************@d30g2000prg.googlegro ups.com...
>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.
Jun 20 '07 #2
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

"sajithkahawatta" <sa*************@gmail.comwrote in message
news:11*********************@d30g2000prg.googlegro ups.com...
>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.

Jun 20 '07 #3
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?
Jun 20 '07 #4
sajithkahawatta wrote:
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);

--
Göran Andersson
_____
http://www.guffa.com
Jun 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Vinod Kumar | last post: by
5 posts views Thread by settyv | last post: by
2 posts views Thread by Mike | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.