setting focus to datagrid subcontrol

B

Bonj

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?
 
J

John M Deal

Try using txtNameAdd.ClientId instead of the txtNameAdd.UniqueID. I did
a similar thing meant specifically for IE (client requirement, not mine)
and aside from that we pretty much have the same code going on.

Have A Better One!

John M Deal, MCP
Necessity Software
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top