I have created a class to set the focus to a teext box. When i move the class to my App_code folder for my application so it can be reused i get the error: "The name 'ClientScript' does not exist in the current context"
The error is being caused by ClientScript.RegisterStartupScript .
I have tried prefixing it with HttpContext.Current but get an error that
ClientScript.RegisterStartupScript is not a class of
HttpContext.Current.
Please help, this is doing my head in!
Cheers, Ryan
My class........
public class
Focus
{
public void SetFocus(Control controlToFocus)
{
StringBuilder scriptFunction =
new StringBuilder();
string scriptClientId;
scriptClientId = controlToFocus.ClientID;
scriptFunction.Append(
"<script language='javascript'>");
scriptFunction.Append(
"document.getElementById('");
scriptFunction.Append(scriptClientId);
scriptFunction.Append(
"').focus();");
scriptFunction.Append(
"</script>");
ClientScript.RegisterStartupScript( // Cause of error
Did you just note that in ASP.NET 2.0 Control has
Focus() method which sets the focus. So you wouldn't need to create the script yourself but just call
controlToFocus.Focus() (perhaps the need to make central function to set focus in that case is actually obsolete)
You need to pass your SetFocus routine a reference to the page. Then call the RegisterStartupScript on that referenced object. I changed line #4 ann line #27.
You would call the function like this:
Focus myFocus = new Focus();
myFocus.SetFocus(this.Button1,this);
public class Focus
{
public void SetFocus(Control controlToFocus, Page webPageInstance)
{
StringBuilder scriptFunction = new StringBuilder();
string scriptClientId;
scriptClientId = controlToFocus.ClientID;
scriptFunction.Append(
"<script language='javascript'>");
scriptFunction.Append(
"document.getElementById('");
scriptFunction.Append(scriptClientId);
scriptFunction.Append(
"').focus();");
scriptFunction.Append(
"</script>");
webPageInstance.ClientScript.RegisterStartupScript(GetType(),"SetFocusScript",scriptFunction.ToString()
);
}
}
there are these built.in variations to set the focus
//Variations to set focus in ASP.NET 2.0
TextBox1.Focus();
Page.SetFocus(TextBox1);
Page.SetFocus(TextBox1.ClientID);
So from this standpoint, setting the focus from a library/separate class seems a little bit needless. But example of ClientScript, say you have a helper class:
public class Helpers
{
public static void ShowAlert(Page currentPage,string message)
{
StringBuilder sb = new StringBuilder();
sb.Append("alert('");
sb.Append(message);
sb.Append("');");
currentPage.ClientScript.RegisterStartupScript(typeof(Helpers),"showalert",sb.ToString(),true);
}
}
No it does not since that code assumes you run it from the page. You can get the currently executing Page also by casting HttpContext.Current.Handler to Page, so making a version of that method to use it, is also a possibility
public class Helpers
{
public static void ShowAlert(Page currentPage, string message)
{
StringBuilder sb = new StringBuilder();
sb.Append("alert('");
sb.Append(message);
sb.Append("');");
currentPage.ClientScript.RegisterStartupScript(typeof(Helpers), "showalert", sb.ToString(), true);
}
public static void ShowAlert(string message)
{
Page currentPage = HttpContext.Current.Handler as Page;
if (currentPage != null)
ShowAlert(currentPage, message);
}
}
Note that the class library would then need to reference System.Web assembly
ryanchopps2
Member
110 Points
22 Posts
How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 03, 2006 01:51 PM|LINK
Hi all,
I have created a class to set the focus to a teext box. When i move the class to my App_code folder for my application so it can be reused i get the error: "The name 'ClientScript' does not exist in the current context"
The error is being caused by ClientScript.RegisterStartupScript . I have tried prefixing it with HttpContext.Current but get an error that ClientScript.RegisterStartupScript is not a class of HttpContext.Current.
Please help, this is doing my head in!
Cheers, Ryan
My class........
public class Focus
{
public void SetFocus(Control controlToFocus){
StringBuilder scriptFunction = new StringBuilder(); string scriptClientId;scriptClientId = controlToFocus.ClientID;
scriptFunction.Append(
"<script language='javascript'>");scriptFunction.Append(
"document.getElementById('");scriptFunction.Append(scriptClientId);
scriptFunction.Append(
"').focus();");scriptFunction.Append(
"</script>");ClientScript.RegisterStartupScript( // Cause of error
GetType(),
"SetFocusScript",scriptFunction.ToString()
);
}
}
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 03, 2006 02:51 PM|LINK
controlToFocus.Page.ClientScript....
Did you just note that in ASP.NET 2.0 Control has Focus() method which sets the focus. So you wouldn't need to create the script yourself but just call controlToFocus.Focus() (perhaps the need to make central function to set focus in that case is actually obsolete)
Teemu Keiski
Finland, EU
James Steele
Participant
875 Points
173 Posts
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 03, 2006 03:03 PM|LINK
Hi Ryan,
You need to pass your SetFocus routine a reference to the page. Then call the RegisterStartupScript on that referenced object. I changed line #4 ann line #27.
You would call the function like this:
James Steele
James Steele
Participant
875 Points
173 Posts
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 03, 2006 03:05 PM|LINK
ryanchopps2
Member
110 Points
22 Posts
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 03, 2006 03:37 PM|LINK
Hi,
Thanks for your reply. Sorry to ask but could you provide an example of your solution? Having a few problems.
Thanks
R.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 03, 2006 05:55 PM|LINK
If you have TextBox on your Page, for example:
<asp:TextBox ID="TextBox1" runat="server" />
there are these built.in variations to set the focus
//Variations to set focus in ASP.NET 2.0
TextBox1.Focus();
Page.SetFocus(TextBox1);
Page.SetFocus(TextBox1.ClientID);
So from this standpoint, setting the focus from a library/separate class seems a little bit needless. But example of ClientScript, say you have a helper class:
public class Helpers
{
public static void ShowAlert(Page currentPage,string message)
{
StringBuilder sb = new StringBuilder();
sb.Append("alert('");
sb.Append(message);
sb.Append("');");
currentPage.ClientScript.RegisterStartupScript(typeof(Helpers),"showalert",sb.ToString(),true);
}
}
You could use it on Page:
Helpers.ShowAlert(this, "Hello");
Teemu Keiski
Finland, EU
ryanchopps2
Member
110 Points
22 Posts
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
May 04, 2006 09:14 AM|LINK
Just writing to say BIG thanks to everyone who helped me out.
All sorted now,
Cheers,
R
parimalaabha
Member
17 Points
19 Posts
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
Aug 04, 2007 03:07 AM|LINK
Hi,
How to handle this situation. I have a textbox inside the user control and this usercontrol is placed in the Page. How to set focus in this case?
Thanks in advance,
Parimal
mng0
Member
7 Points
27 Posts
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
Jan 22, 2008 12:46 PM|LINK
If you want to run the method from another class or library, how do you do that?
"this" does not work.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to use ClientScript.RegisterStartupScript in App_code files? is it possible??
Jan 22, 2008 03:14 PM|LINK
No it does not since that code assumes you run it from the page. You can get the currently executing Page also by casting HttpContext.Current.Handler to Page, so making a version of that method to use it, is also a possibility
public class Helpers { public static void ShowAlert(Page currentPage, string message) { StringBuilder sb = new StringBuilder(); sb.Append("alert('"); sb.Append(message); sb.Append("');"); currentPage.ClientScript.RegisterStartupScript(typeof(Helpers), "showalert", sb.ToString(), true); } public static void ShowAlert(string message) { Page currentPage = HttpContext.Current.Handler as Page; if (currentPage != null) ShowAlert(currentPage, message); } }Note that the class library would then need to reference System.Web assemblyTeemu Keiski
Finland, EU