Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / April 2008

Tip: Looking for answers? Try searching our database.

Set Focus after Postback (vb.net newbie)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mel - 16 Apr 2008 14:02 GMT
I have several text boxes and drop-down lists in an AJAX Update
Panel.  All user inputs have the Postback property set to True.  After
I type something in the first input entry and press the "Tab" key how
can I set the focus to the next box after the postback?  Please help!

Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP
Eliyahu Goldin - 16 Apr 2008 14:12 GMT
Use ScriptManager.SetFocus (myControl).

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

>I have several text boxes and drop-down lists in an AJAX Update
> Panel.  All user inputs have the Postback property set to True.  After
> I type something in the first input entry and press the "Tab" key how
> can I set the focus to the next box after the postback?  Please help!
>
> Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP
Mel - 16 Apr 2008 14:41 GMT
On Apr 16, 8:12 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> Use ScriptManager.SetFocus (myControl).
>
[quoted text clipped - 9 lines]
>
> > Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP

THANK YOU!  I wish I would have posted my question yesterday...I spent
hours sifting the web for an answer.  I had Page.SetFocus(MyControl)
instead of ScriptManager.SetFocus (myControl).

Code Snippet:
   Protected Sub txtCustomer_TextChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles txtCustomer.TextChanged
       'customer text box (1st text box on screen)
       If rfvCustomer.IsValid = True Then
           Session("ProjName") = txtCustomer.Text
       Else
           Session("ProjName") = Nothing
       End If
       Session("QRNextControl") = "2"
       SetNextQRControl()
   End Sub
   Protected Sub SetNextQRControl()
       Select Case Session("QRNextControl")
           Case "2"
               ScriptManager1.SetFocus(txtProjAddr1) 'address 1 text
box
           Case "3"
               ScriptManager1.SetFocus(txtProjAddr2) 'address 2 text
box
           Case "4"
               ScriptManager1.SetFocus(txtProjCity) 'city text box
           Case "5"
               ScriptManager1.SetFocus(ddlStAbrv) 'state text box
           Case "6"
               ScriptManager1.SetFocus(txtZip)   'zip text box
           Case "7"
               ScriptManager1.SetFocus(txtCusContact)  'customer
Contact text box
           Case "8"
               ScriptManager1.SetFocus(txtCusPhone)  'customer Phone
text box
           Case "9"
               ScriptManager1.SetFocus(txtProjName)  'project Name
text box
           Case "10"
               ScriptManager1.SetFocus(rblPriceAdj)  'price
Adjustment text box
       End Select
       Session("QRNextControl") = ""
   End Sub
Eliyahu Goldin - 16 Apr 2008 14:45 GMT
>I wish I would have posted my question yesterday...I spent hours sifting
>the web for an answer.

This is the only way of getting experience known to me.

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> On Apr 16, 8:12 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
[quoted text clipped - 58 lines]
>        Session("QRNextControl") = ""
>    End Sub
Mel - 16 Apr 2008 21:52 GMT
On Apr 16, 8:45 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> >I wish I would have posted my question yesterday...I spent hours sifting
> >the web for an answer.
[quoted text clipped - 72 lines]
> >        Session("QRNextControl") = ""
> >    End Sub

It works great.  One small glitch though.
Lets say there are 3 text boxes.  If you type some data in the 1st
text box and then instead of pressing the tab key you click your mouse
in the 3rd text box, after the postback the cursor is sitting in the
2nd text box.  Not the 3rd one.
Eliyahu Goldin - 17 Apr 2008 09:35 GMT
You can handle client side onfocus event to set a hidden text control to the
id of the element that got focus. On server side, you can set focus based on
the passed id as opposed to the session variable.

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> On Apr 16, 8:45 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
[quoted text clipped - 84 lines]
> in the 3rd text box, after the postback the cursor is sitting in the
> 2nd text box.  Not the 3rd one.
Mel - 17 Apr 2008 14:44 GMT
On Apr 17, 3:35 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> You can handle client side onfocus event to set a hidden text control to the
> id of the element that got focus. On server side, you can set focus based on
[quoted text clipped - 97 lines]
> > in the 3rd text box, after the postback the cursor is sitting in the
> > 2nd text box.  Not the 3rd one.

Sorry to be a pain.  I am using a Textbox control.  Will I need to use
a different control instead to get an OnFocus method?
Eliyahu Goldin - 17 Apr 2008 14:57 GMT
No, you can attach a client-side event handler like
myTextBox.Attributes["onfocus"] = "gotFocus(this)";

and in .aspx file:
<input type = "hidden" runat="server" id="inhControlWithFocus" />

and in javascript
function gotFocus(control)
{
myForm.inhControlWithFocus.value = control.Id;
}

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> On Apr 17, 3:35 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
[quoted text clipped - 107 lines]
> Sorry to be a pain.  I am using a Textbox control.  Will I need to use
> a different control instead to get an OnFocus method?
Mel - 17 Apr 2008 17:25 GMT
On Apr 17, 8:57 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> No, you can attach a client-side event handler like
> myTextBox.Attributes["onfocus"] = "gotFocus(this)";
[quoted text clipped - 129 lines]
> > Sorry to be a pain.  I am using a Textbox control.  Will I need to use
> > a different control instead to get an OnFocus method?

The hidden text box always says "undefined".  I am doing something
wrong.

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
   Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Set Focus - AJAX Script Manager and Update Panel</title>
</head>
<script type="text/javascript">
   function gotFocus(control)
   {

document.getElementById("inhControlWithFocus").value=control.Id;
   }
</script>
<body>
   <form id="form1" runat="server">
       <asp:ScriptManager ID="ScriptManager1" runat="server">
       </asp:ScriptManager>
       &nbsp;
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
               <asp:TextBox ID="TextBox1" runat="server"
AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"
                   TabIndex="1"></asp:TextBox><br />
               <br />
               <asp:TextBox ID="TextBox2" runat="server"
AutoPostBack="True" OnTextChanged="TextBox2_TextChanged"
                   TabIndex="2"></asp:TextBox><br />
               <br />
               <asp:TextBox ID="TextBox3" runat="server"
AutoPostBack="True" OnTextChanged="TextBox3_TextChanged"
                   TabIndex="3"></asp:TextBox><br />
               <br />
               <input runat="server" id="inhControlWithFocus" />
           </ContentTemplate>
       </asp:UpdatePanel>
   </form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Partial Class _Default
   Inherits System.Web.UI.Page
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
       TextBox1.Attributes("onfocus") = "gotFocus(this)"
       TextBox2.Attributes("onfocus") = "gotFocus(this)"
       TextBox3.Attributes("onfocus") = "gotFocus(this)"
   End Sub
   Protected Sub SetNextQRControl()
       ScriptManager1.SetFocus(inhControlWithFocus.ID)
   End Sub
   Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e
As System.EventArgs)
       'Session("QRNextControl") = 2
       SetNextQRControl()
   End Sub

   Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e
As System.EventArgs)
       'Session("QRNextControl") = 3
       SetNextQRControl()
   End Sub

   Protected Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e
As System.EventArgs)
       'Session("QRNextControl") = 1
       SetNextQRControl()
   End Sub
End Class

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.