focus question

  • Thread starter Thread starter starbuck
  • Start date Start date
S

starbuck

We have some fields with AutoPostBack set to true and when user tabs to the
next field the screen re-calculates correctly however we also lose which
control/text box had focus when the user tabbed.
Is there any way of storing and resting which control should have focus?
We are using asp.net with vb.net for code.

Thanks
 
Here is a user control I wrote in C# to accomplish this. Place it in your toolbox and drag and drop it on any form. Set the properties in your code behind

using System
using System.Web.UI
using System.ComponentModel

namespace Common.CTL.FocusManage

/// <summary
/// clsFocusManager - Allows programmer to set focus to a control and to select the data in th
/// control if desired
/// This web custom control is placed in the Tool Box
/// </summary
[DefaultProperty("Text"),
ToolboxData("<{0}:clsFocusManager runat=server></{0}:clsFocusManager>")

public class clsFocusManager : System.Web.UI.WebControls.WebContro

private string mBoundControlID
private bool mSelectControl

//Property to indicate which control will receive focus
[Bindable(true),
Category("Appearance"),
DefaultValue("")]

public string BoundControlI

ge

return mBoundControlID

se

mBoundControlID = value



//Property to indicate if the data in the control is selected or not
[Bindable(true),
Category("Appearance"),
DefaultValue(false)]

public bool SelectContro

ge

return mSelectControl

se

mSelectControl = value



/// <summary>
/// Render this control to the output parameter specified
/// </summary
/// <param name="output"> The HTML writer to write out to </param
protected override void Render(HtmlTextWriter output

string strScript

if (mBoundControlID != null

if (Page.FindControl(mBoundControlID) != null

if(!Page.IsStartupScriptRegistered("FocusManager")

strScript = "<Script Language=javascript>"
strScript += "document.getElementById('" + mBoundControlID + "').focus();"

if (mSelectControl == true

strScript += "document.getElementById('" + mBoundControlID + "').select();"

strScript += "</script>"
Page.RegisterStartupScript("FocusManager", strScript)
 
Thanks

RCAM said:
Here is a user control I wrote in C# to accomplish this. Place it in your
toolbox and drag and drop it on any form. Set the properties in your code
behind.
using System;
using System.Web.UI;
using System.ComponentModel;

namespace Common.CTL.FocusManager
{
/// <summary>
/// clsFocusManager - Allows programmer to set focus to a control and to select the data in the
/// control if desired.
/// This web custom control is placed in the Tool Box.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:clsFocusManager runat=server></{0}:clsFocusManager>")]

public class clsFocusManager : System.Web.UI.WebControls.WebControl
{
private string mBoundControlID;
private bool mSelectControl;

//Property to indicate which control will receive focus.
[Bindable(true),
Category("Appearance"),
DefaultValue("")]

public string BoundControlID
{
get
{
return mBoundControlID;
}
set
{
mBoundControlID = value;
}
}

//Property to indicate if the data in the control is selected or not.
[Bindable(true),
Category("Appearance"),
DefaultValue(false)]

public bool SelectControl
{
get
{
return mSelectControl;
}
set
{
mSelectControl = value;
}
}

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
string strScript;

if (mBoundControlID != null)
{
if (Page.FindControl(mBoundControlID) != null)
{
if(!Page.IsStartupScriptRegistered("FocusManager"))
{
strScript = "<Script Language=javascript>";
strScript += "document.getElementById('" + mBoundControlID + "').focus();";

if (mSelectControl == true)
{
strScript += "document.getElementById('" + mBoundControlID + "').select();";
}
strScript += "</script>";
Page.RegisterStartupScript("FocusManager", strScript);
}
}
}
}
}
}
 
Here is another idea:

In your base page that all your pages inherit from you can use:
=====================================================
Public InitialFocus As Control

Protected Overridable Sub Page_PreRender(ByVal sender As Object, ByVal e As
EventArgs)
Dim sb As New StringBuilder
sb.Append("<script language='javascript'>")
If Not InitialFocus Is Nothing Then
sb.Append("document.getElementById('" & InitialFocus.ClientID &
"').focus();")
End If
sb.Append("</script>")
End Sub
=====================================================
Then in Page_Load of your .aspx page you simply set the Property like this:
InitialFocus = txtVuid
=====================================================
 
Thanks Joe

Joe Fallon said:
Here is another idea:

In your base page that all your pages inherit from you can use:
=====================================================
Public InitialFocus As Control

Protected Overridable Sub Page_PreRender(ByVal sender As Object, ByVal e As
EventArgs)
Dim sb As New StringBuilder
sb.Append("<script language='javascript'>")
If Not InitialFocus Is Nothing Then
sb.Append("document.getElementById('" & InitialFocus.ClientID &
"').focus();")
End If
sb.Append("</script>")
End Sub
=====================================================
Then in Page_Load of your .aspx page you simply set the Property like this:
InitialFocus = txtVuid
=====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top