I have webform that uses 1 customized user controls(contain textbox1 and textbox2). When the form loads, I want to put the mouse focus on the user controls textbox1. Is this possible?
Thanks in advance!Again in my case I'm trying to position on a textbox1 inside a user control(name.ascx) that is called by home.aspx page.
When I add the below code to the page_load event of home.aspx, it gave the error.
Dim strbuilder As StringBuilder = New StringBuilder
strbuilder.Append("<script language='javascript'>")
strbuilder.Append(" window.document.getElementById('txtkeywords').focus()")
strbuilder.Append("</script>")
RegisterStartupScript("Focus", strbuilder.ToString)
Error:
Microsoft JScript runtime error: 'window.document.getElementById(...)' is null or not an object
Any help is grealty appreciated
Say that your user control has the ID of "myUserControl".
You would then change your code from:
strbuilder.Append(" window.document.getElementById('txtkeywords').focus()")to
strbuilder.Append(" window.document.getElementById('" & myUserContro.FindControl('txtkeywords').ClientID & "').focus()")
Admittedly, I haven't tested this ... but it should work.
I replaced the code you gave me, but it gave me a Expression Expected error, indicated in between getElementbyID and (. Since I don't know Javascript well, I remove the two "s from your code and it gave me runtime error: Expected ')' on this line of code. How do I make this work?
Thanks for you help.
My apologies, I should have tested the code before I posted it.
Here is the user control:
<%@. Control Language="VB" %>
<script runat="server"
Function GetTextboxID() As String
Return txtkeywords.ClientID
End Function</script>
<p>I am the UserControl</p>
<p>Keywords: <asp:Textbox id="txtkeywords" runat="server" /></p>
The only interesting thing is that I've given the User Control a method called GetTextboxID. This simply returns the ClientID property of its Textbox control. You see, when the page finally renders, the ID of this textbox will no longer be "txtkeywords". It will be the ID of the user controlplus the ID of the textbox, separated by an underscore: myUserControl_txtkeywords.
This is why your code was never working ... the Javascript was looking for a control with the ID txtkeywords, when what it should be looking for was the ID myUserControl_txtkeywords.
And we've now given our User Control a method that returns this extended ID (known as the ClientID).
So, we can now update our ASPX Page so that it calls this function when creating the focus Javascript function:
<%@. Page Language="VB" %>
<%@. Register TagPrefix="my" TagName="UserControl" src="http://pics.10026.com/?src=mycontrol15.ascx" %>
<script runat="server"
Sub Page_Load( sender As Object, eArgs As EventArgs )
Dim focusScript As Text.StringBuilder = New Text.StringBuilder
focusScript.Append("<scr" & "ipt language=""javascript"" type=""text/javascript"">")
focusScript.Append("window.document.getElementById('" &myUserControl.GetTextboxID() & "').focus()")
focusScript.Append("</scr" & "ipt>")
RegisterStartupScript("FocusScript", focusScript.ToString)
End Sub</script>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>Page Header</h3>
<hr />
<my:UserControl id="myUserControl" runat="server" />
<hr />
<h4>Page Footer</h4>
</form>
</body>
</html>
Does that work for you?
Thanks for your help! But, I created a usercontrol and a form and copied your code. When I run it, it doesn't place the focus on textbox. what went wrong?
here are my code:
myControl.ascx
<%@. Control Language="vb" AutoEventWireup="false" Codebehind="myControl.ascx.vb" Inherits="SettingFocus.myControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<script runat="server"
Function GetTextboxID() As StringReturn txtkeywords.ClientID
End Function
</script>
<p>I am the UserControl</p>
<p>Keywords:
<asp:Textbox id="txtkeywords" runat="server" /></p
And the form file DefaultFocus.aspx
<%@. Register TagPrefix="my" TagName="UserControl" src="http://pics.10026.com/?src=myControl.ascx" %>
<%@. Page Language="vb" AutoEventWireup="false" Codebehind="DefaultFocus.aspx.vb" Inherits="SettingFocus.DefaultFocus"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<script runat="server"
Sub Page_Load( sender As Object, eArgs As EventArgs )Dim focusScript As Text.StringBuilder = New Text.StringBuilder
focusScript.Append("<scr" & "ipt language=""javascript"" type=""text/javascript"">")
focusScript.Append("window.document.getElementById('" & myUserControl.GetTextboxID() & "').focus()")
focusScript.Append("</scr" & "ipt>")
RegisterStartupScript("FocusScript", focusScript.ToString)
End Sub
</script
<html
<head
</head
<body
<form runat="server" ID="Form1"
<h3>Page Header</h3
<hr /
<my:UserControl id="myUserControl" runat="server" /
<hr /
<h4>Page Footer</h4
</form
</body
</html>
We'll need to update our code slightly as you're using the codebehind method.
Here is our Control.ascx user control. I've now removed the inline code:
<%@. Control AutoEventWireup="False" Inherits="Test.ControlCodebehind" %>
<p>I am the UserControl</p>
<p>Keywords: <asp:Textbox id="txtkeywords" runat="server" /></p>
The code has now moved into the codebehind:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControlsNamespace Test
Public Class ControlCodebehind
Inherits System.Web.UI.UserControlProtected txtkeywords As Textbox
Function GetTextboxID() As String
Return txtkeywords.ClientID
End FunctionEnd Class
End Namespace
Our page.aspx also has its inline code removed:
<%@. Page AutoEventWireup="False" Inherits="Test.PageCodebehind" %>
<%@. Register TagPrefix="my" TagName="UserControl" src="http://pics.10026.com/?src=control.ascx" %>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>Page Header</h3>
<hr />
<my:UserControl id="myUserControl" runat="server" />
<hr />
<h4>Page Footer</h4>
</form>
</body>
</html>
And the code is moved into its codebehind. I've highlighted the only real change:
Imports System
Imports System.Text
Imports System.Web.UINamespace Test
Public Class PageCodebehind
Inherits System.Web.UI.PageProtected myUserControl As Test.ControlCodebehind
Protected Sub Page_Load( ByVal sender As Object, ByVal eArgs As System.EventArgs ) Handles MyBase.Load
Dim focusScript As StringBuilder = New StringBuilder
focusScript.Append("<scr" & "ipt language=""javascript"" type=""text/javascript"">")
focusScript.Append("window.document.getElementById('" & myUserControl.GetTextboxID() & "').focus()")
focusScript.Append("</scr" & "ipt>")
RegisterStartupScript("FocusScript", focusScript.ToString)End Sub
End Class
End Namespace
As you can see, the only real change is that we've declared our myUserControl not as a UserControl, but as the class name for our user control's codebehind.
Does that help?
NO Luck. I'm getting the same result. I'm wondering if the code works on your pc. If it does then there must be something else other than the source code that is causing the problem. I'm running Visual Studio .NET 2003, with framework 1.1 and IIS 5.
> I'm wondering if the code works on your pc.
It sure does, because I made a test app called DotNetBeginner in IIS, with the code shown. (I too use .NET 1.1 on IIS 5.)
You can download this test app:
www.edition3.com/developers/DotNetBeginner/code.zip
Create a new folder within \wwwroot and unzip the files. Use IIS to configure this new folder as an app. And then request page.aspx.
Let me know if that works.
Not to step on SomeNewKid's efforts, I'm sure his code will work, I haven't tested it, but when I want to simply set focus on a textbox, this is what I do...
Let's say my textbox name is txtEntry.
In the page_load event, I place this code.
Me.SetFocus(Me.txtEntry)
Then, elsewhere, I create this...
Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"
RegisterStartupScript("focus", s)
End Sub
And that's it. You can put the Me.SetFocus(Me.txtEntry) anywhere on your page as well.
Hope this helps as well,
Zath
Hi.. how about if i want to put the coding in a .cs file? i found that it have an error on this line
RegisterStartupScript("focus", s)?
The name 'RegisterStartupScript' does not exist in the class or namespace 'Utilities.HandleUtil.HandlerUtil'
What wrong actually?
Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"
RegisterStartupScript("focus", s)
End Sub
RegisterStartupScript is a method of System.Web.UI.Page.
So RegisterStartupScript really needs to be used within your codebehind ... not within a Utility class.
Although, if you like, the Utility class can generate the script, but the codebehind actually places it on the Page with RegisterStartupScript.
Hi SomeNewKid,
This maybe a dumb question, but since I'm a beginner, please don't mind me asking. How do configure the new folder as an application in IIS?
Thanks
Gwen
Open the Internet Services Manager (under Administrative tools) expand
your website and right-click on webserver, select all tasks->New virtual directory,
give it a name, browse to the direcotry that contains the files, and voila
you have configured your application!
Regards
Fredr!k
I do it aslightly different way to Fredrick2000.
To make it easy, I've created a little tutorial with screenshots, starting here:
www.edition3.com/developers/DotNetBeginner/step1.htm
I hope this helps.
0 comments:
Post a Comment