By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

setting focus to datagrid subcontrol

P: n/a
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?
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Bonj wrote:
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?

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.