By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Setting focus on control in webform

P: n/a
I have a webform and found some code where I understand some of it, but not
all
The code is as follows
public static void SetFocus(Control control)
{
StringBuilder sb = new StringBuilder();

sb.Append("\r\n<script language='JavaScript'>\r\n");
sb.Append("<!--\r\n");
sb.Append("function SetFocus()\r\n");
sb.Append("{\r\n");
sb.Append("\tdocument.");

Control p = control.Parent;
while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;

sb.Append(p.ClientID);
sb.Append("['");
sb.Append(control.UniqueID);
sb.Append("'].focus();\r\n");
sb.Append("}\r\n");
sb.Append("window.onload = SetFocus;\r\n");
sb.Append("// -->\r\n");
sb.Append("</script>");

control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
}
The code results in adding Javascript to your page as follows:
<script language='JavaScript'>
<!--
function SetFocus()
{
document.Form1['TextBox1'].focus();
}
window.onload = SetFocus;
// -->
</script>
But you don't need to care about that because all you need to do to use it
is call your static method from your code behind class:
PageUtility.SetFocus(TextBox1);I have converted it to VB. What I don't
understand is the last one sentence with the first word PageUtilityWhat is
it for?regardsreidarT
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I found the solution myself. PageUtility means only Me
Sorry for the inconvenience.
regards
reidarT
"reidarT" <re****@eivon.no> skrev i melding
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have a webform and found some code where I understand some of it, but not
all
The code is as follows
public static void SetFocus(Control control)
{
StringBuilder sb = new StringBuilder();

sb.Append("\r\n<script language='JavaScript'>\r\n");
sb.Append("<!--\r\n");
sb.Append("function SetFocus()\r\n");
sb.Append("{\r\n");
sb.Append("\tdocument.");

Control p = control.Parent;
while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;

sb.Append(p.ClientID);
sb.Append("['");
sb.Append(control.UniqueID);
sb.Append("'].focus();\r\n");
sb.Append("}\r\n");
sb.Append("window.onload = SetFocus;\r\n");
sb.Append("// -->\r\n");
sb.Append("</script>");

control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
}
The code results in adding Javascript to your page as follows:
<script language='JavaScript'>
<!--
function SetFocus()
{
document.Form1['TextBox1'].focus();
}
window.onload = SetFocus;
// -->
</script>
But you don't need to care about that because all you need to do to use it
is call your static method from your code behind class:
PageUtility.SetFocus(TextBox1);I have converted it to VB. What I don't
understand is the last one sentence with the first word PageUtilityWhat is
it for?regardsreidarT

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.