Groups | Blog | Home
all groups > asp.net datagrid control > november 2004 >

asp.net datagrid control : setting focus to datagrid subcontrol


Bonj
11/13/2004 3:30:52 PM
Hi
I've got an ASP.NET datagrid control, and in the footer, there is a series
of controls for typing in new values to add an item.
e.g.
<asp:DataGrid runat="Server".... >
<Columns>
...
<asp:TemplateColumn HeaderText="Name">
...
<FooterTemplate>
<asp:TextBox ID="txtNameAdd" Runat="server" Width="100%"
Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
</FooterTemplate>
</asp:TemplateColumn>
<asp:DataGrid>

I'm using the following C# code to try to cause this to be selected, but it
is not working.
private void dg1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
Control txtNameAdd = e.Item.FindControl("txtNameAdd");
if(txtNameAdd == null) Debug.WriteLine("txtNameAdd is null");
else
{
string js = string.Format("<script
language=\"javascript\">document.getElementById('{0}').select();
document.getElementById('{0}').focus();</script>",txtNameAdd.UniqueID);
Debug.WriteLine(js);
this.RegisterStartupScript("FocusNameAdd", js);
}
}

Any ideas as to what I can do to get it working?

i-mich NO[at]SPAM online.microsoft.com
11/17/2004 6:52:02 AM
Hi Bonj,

You might like to try using the smartNavigation property on the aspx page instead of RegisterStartupScript

When a page is requested by an Internet Explorer 5.5 browser, or later, smart navigation enhances the user's experience of the page by performing the
following:

-eliminating the flash caused by navigation.
-persisting the scroll position when moving from page to page.
-persisting element focus between navigations.
-retaining only the last page state in the browser's history.

Smart navigation is best used with ASP.NET pages that require frequent postbacks but with visual content that does not change dramatically on return. --
Consider this carefully when deciding whether to set this property to true.

Hope that helps,

Michelle Hlaing
Microsoft Support Professional

***Disclaimer: This posting is provided "as is" with no warranties and confers no rights.***
--------------------
[quoted text, click to view]

AddThis Social Bookmark Button