TextBox.Focus not working in FormView

J

J055

Hi

The client script does not get created when calling the Focus method on the
TextBox. No errors occur. What am I doing wrong please?

protected void fv_Load(object sender, EventArgs e)
{
FormView fv = (FormView)sender;
TextBox tbFirstName = (TextBox)fv.FindControl("tbFirstName");
tbFirstName.Focus();
}

Thanks
Andrew
 
M

Mark Rae

The client script does not get created when calling the Focus method on
the TextBox. No errors occur. What am I doing wrong please?

protected void fv_Load(object sender, EventArgs e)
{
FormView fv = (FormView)sender;
TextBox tbFirstName = (TextBox)fv.FindControl("tbFirstName");
tbFirstName.Focus();
}

What happens if you rem out the first two lines within the method...?

Alternatively, what happens if you put the third line of the method in
Page_Load()...?
 
S

Steven Cheng[MSFT]

Hi Andrew,

As for setting control focus inside a FormView control, I suggest you the
"PreRender" event, because this is the last event in control's server-side
lifecycle that can use to change control state. Also, in those earlier
event(such as Load), the FormView's content may haven't been fully
configured so our changes on them may bave been overwritten by some other
internal code. Also for "focus" setting, it is also a global setting on
page, maybe some other controls on page will acquire the focus also.
Therefore, I think you can put the code into FormView.PreRender event
handler as below:

================
protected void FormView1_PreRender(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox txt = FormView1.FindControl("nameTextBox") as TextBox;

if (txt != null)
{

Page.SetFocus(txt);
}
}
}

=======================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top