Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Setting Focus on stubborn Textbox

Author  Topic 

slmgt
Starting Member

37 Posts

Posted - 2010-10-15 : 16:54:04
[code]<%@ Page Language="VB" MasterPageFile="~/masterPages/Login.master" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="toolbarContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="featuredContent" Runat="Server">

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="navContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="mainContent" Runat="Server">
<h1>Welcome</h1>
<p>Please enter your UserID and password.</p>
<p>
<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="1.2em"
ForeColor="#333333" Height="125px" Width="376px" DestinationPageUrl="~/employees/Default.aspx">
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="1em" ForeColor="White" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TextBoxStyle Font-Size="1em" Font-Strikeout="False" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="1em" ForeColor="#284775" />
<LayoutTemplate>
<table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse">
<tr>
<td>
<table border="0" cellpadding="0" style="width: 376px; height: 125px">
<tr>
<td align="center" colspan="2" style="font-weight: bold; font-size: 1em; color: white;
background-color: #5d7b9d">
Log In</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">UserID:</asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server" Font-Size="1em" Font-Strikeout="False"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>[/code]

I tried numerous ways to set focus on that textbox, including using the Login.master, however in the masterpage, I do not know where to put any of the code as it does not have TextBox ID or anything else I think is relevant. Other pages have cursors starting in the box (focus set), but these pre-date me therefore I am unaware how they got that way (and looking at them has not netted me any new knowledge).

Sachin.Nand

2937 Posts

Posted - 2010-10-16 : 04:07:40
I m not to sure.You can do something like this

Login1.Controls.FindControl("Yourtextbox")
& then cast it to a textbox & set the focus property to true.

PBUH

Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-18 : 16:54:46
quote:
Originally posted by Sachin.Nand

I m not to sure.You can do something like this

Login1.Controls.FindControl("Yourtextbox")
& then cast it to a textbox & set the focus property to true.

PBUH





I'm not sure I understand. I thought it was already a textbox... And what focus property?

<html>
<head>
<title>sample focus</title>
<script language="javascript">
function focusBox()
{
var box = document.getElementById('myControl');
box.focus();
}
</script>
</head>
<body onLoad="focusBox();">
<input type="text" id="myControl" />
</body>
</html>


The code above works per my own testing, but does not do anything for my ASP page. login.aspx is a page whose masterpage is located at: ~/masterPages/Login.master

Login.master has no reference [to my knowledge] to the UserName text box as it is basically a template. It has a form id = form1 portion, but no reference to the UserName box.

I have looked at these suggestions: http://forums.asp.net/t/1098151.aspx
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-10-19 : 01:07:18
Try this...

document.getElementById("<%=myControl.ClientID%>").focus();
Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-19 : 14:58:42
I tried posting it before and after the code. Since after seems more appropriate, here is the error and exact code used:
Compiler Error Message: BC30451: Name 'UserName' is not declared.

Source Error:



Line 35: <td>
Line 36: <asp:TextBox ID="UserName" runat="server" Font-Size="1em" Font-Strikeout="False"></asp:TextBox>
Line 37: document.getElementById("<%=UserName.ClientID%>").focus();


Go to Top of Page

nathan.bekker
Starting Member

11 Posts

Posted - 2010-10-19 : 15:35:05
Hi ,

Do you want the focus to be set to the TextBox when the page loads?
If so use this in your code behind:
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = Login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub

Hope this works for you.
Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-19 : 16:54:01
quote:
Originally posted by nathan.bekker

Hi ,

Do you want the focus to be set to the TextBox when the page loads?
If so use this in your code behind:
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = Login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub

Hope this works for you.



I put that code exactly as is in a login.aspx.vb page with no other code. It had no effect. I also tried putting it in the login.master.vb and it gave me an error. I then tried adding a "CodeFile" to login.aspx but even with the CodeFile set to login.aspx.vb and Inherits="Login2" (which was what I made the class named in login.aspx.vb), it did nothing. I don't doubt it works, I just do not know how to implement it (I am not a .NET programmer).
Go to Top of Page

nathan.bekker
Starting Member

11 Posts

Posted - 2010-10-19 : 17:04:01
Are you using Visual Studio?
Go to Top of Page

nathan.bekker
Starting Member

11 Posts

Posted - 2010-10-19 : 17:17:57
This is what your login.aspx.vb should look like.
Partial Class login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = Login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub
End Class

Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-19 : 17:20:58
I am using Dreamweaver, but it appears to edit correctly.

But now I get this error:
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

Source Error:



Line 1: Partial Class login
Line 2: Inherits System.Web.UI.Page
Line 3: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



My header in login.aspx:
<%@ Page Language="VB" CodeFile="login.aspx.vb" MasterPageFile="~/masterPages/Login.master" Title="" Inherits="login" %>


Code in login.aspx.vb:
Partial Class login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = Login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub
End Class
Go to Top of Page

nathan.bekker
Starting Member

11 Posts

Posted - 2010-10-19 : 17:32:37
From the Header in your login.aspx Remove the CodeFile and inherits.
Then paste this right after the header and before your asp:content controls

<script type="text/VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub
</script>

Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-19 : 17:45:18
quote:
Originally posted by nathan.bekker

From the Header in your login.aspx Remove the CodeFile and inherits.
Then paste this right after the header and before your asp:content controls

<script type="text/VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub
</script>





I had no success with that even though it looks like it should work. My top several lines of login.aspx:
<%@ Page Language="VB" MasterPageFile="~/masterPages/Login.master" Title=""%>
<script type="text/VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txt As TextBox = login1.FindControl("UserName")
If (Not txt Is Nothing) Then
txt.Focus()
End If
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="toolbarContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="featuredContent" Runat="Server">


Thanks a lot for your help thus far!!!
Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-20 : 16:01:47
I am writing to correct my previous post. When I tested the code yesterday, it did not appear to work. However today, it is working 100% perfectly. Thank you very much for the solution, nathan.bekker - especially that it is simple, I understand it and it was easy to implement.
Go to Top of Page

nathan.bekker
Starting Member

11 Posts

Posted - 2010-10-20 : 17:33:19
You are welcome.
Any Time
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-21 : 08:13:26
Did I say something different?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=151647#595925

PBUH

Go to Top of Page

slmgt
Starting Member

37 Posts

Posted - 2010-10-21 : 14:50:42
quote:
Originally posted by Sachin.Nand

Did I say something different?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=151647#595925

PBUH





I did not know how to do it your way, nor did I have any idea on how to implement it. With things I understand a little more, your guidance would have been fine, but ASP/VB I simply do not understand, hence the request for a more detailed explanation. Thank you, too!
Go to Top of Page
   

- Advertisement -