HighTechTalks DotNet Forums  

call a javascript function within code behind

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss call a javascript function within code behind in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
sajithkahawatta
 
Posts: n/a

Default call a javascript function within code behind - 06-20-2007 , 07:14 AM






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.

thankx


Reply With Quote
  #2  
Old   
=?Utf-8?B?U3JpcmFtIFN1cmFwdXJlZGR5?=
 
Posts: n/a

Default RE: call a javascript function within code behind - 06-20-2007 , 07:31 AM






There are several ways to do it.One approach is add the javascript in hidden
field like this..
<asp:HiddenField ID="test" runat="server" />
<script type="text/javascript">
if(document.all["test"].value == "1")
{
//whtever the script
document.all["test"].value ="";
}
</script>

and in the if condition set the "test" field to 1.
Hope this helps.


"sajithkahawatta" wrote:

Quote:
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.

thankx



Reply With Quote
  #3  
Old   
David R. Longnecker
 
Posts: n/a

Default Re: call a javascript function within code behind - 06-20-2007 , 09:01 AM



There are a few options depending on what you're looking for. I'm assuming
you're using standard HTML controls since you're highlighting with JavaScript.

Give this a try:


Your ASPX page looks similar to:

<form id="form1" runat="server">
<div>
<input id="infoTextBox" name="infoTextBox" runat="server" />
<input id="infoHighlight" runat="server" type="button" value="Highlight"
onfocus="return infoHighlight_onfocus()" />
</div>
</form>

Your script in the <head> block would look like:

<script language="javascript" type="text/javascript">
// <!CDATA[

function infoHighlight_onfocus() {
document.forms[0].elements["infoTextBox"].select();
}

// ]]>
</script>


If you're using .NET Server Controls, you could modify this to be:

<form id="form1" runat="server">
<div>
<asp:TextBox ID="infoTextBox" runat="server" />
<asp:Button ID="infoHighlight" runat="server" Text="Highlight" onclick="infoHighlight_Click"
/>
</div>
</form>

And in the codebehind (default.aspx.cs), a quick method that you could reuse
for any control:

private void SelectControl(Control ctl)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script type='text/javascript'>");
sb.Append(String.Format("document.getElementById(' {0}').select();",
ctl.ID));
sb.Append("</script>");
this.Page.RegisterStartupScript("setFocus", sb.ToString());
}

And to call it, the onClick event of your button:

protected void infoHighlight_Click(object sender, EventArgs e)
{
SelectControl(infoTextBox);
}

HTH.

-dl


--
David R. Longnecker
http://blog.tiredstudent.com

s> i want to call a java script function from code behind.
s>
s> in page1.aspx page i placed script'
s>
s> <script language="javascript">
s> function SetSelected()
s> {
s> infoTextBox.select();
s> }
s> </script>
s>
s> i want to highlight text in infotextbox in some cases. so i want to
s> call this function within code behind.(within if statment).
s> how can i call this function .i tried with
s> RegisterClientScriptBlock()
s> but i could not do it .but i think it can be used. can u tell me how
s> a
s> java script function is called.
s> thankx
s>



Reply With Quote
  #4  
Old   
sajithkahawatta
 
Posts: n/a

Default Re: call a javascript function within code behind - 06-21-2007 , 12:27 AM



thanx everyone for reply.
dear David R. Longnecker i used your code and it is
worked.
thanx again


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.