Control Focus and DataList

J

Jerry Camel

Once the datalist is in edit mode and the text boxes and all happily appear,
how can I force the textbox to be the control with focus, so the user can
start editing without having to click the textbox first? Thanks.

Jerry
 
J

Jay Douglas

In the past, I have put the following code in the ItemDataBound event of the
text box.

Where TextBoxToFocusId in the text box id in the datagrid

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
TextBox txt = e.Item.FindControl("TextBoxToFocusId");
RegisterClientScriptBlock("DataGridFocus",
string.Format("<script
language=\"javascript\">document.getElementById('{0}').focus();</script>",
txt.ClientID));
}
}
 
J

Jerry Camel

Here's the problem... The Datalist control renders the code and assigns IDs
and names to all the controls. So you don't know the name of the control
that needs the focus until the html is rendered. And the name will be
different for every line in the datalist.

For instance, I named the textbox "txtName", but the ID when rendered is
something like "dlEntities__ctl4_txtName" and if I click a different record
to edit, the name would be different. How can you tell what control to set
the focus to?

Thanks.
 
J

Jay Douglas

Per the example below, you on the ItemDataBound event, you can use:

TextBox txt = (TextBox) e.Item.FindControl("txtName");

then txt.ClientID will return the full control name, ie:
dlEntities__ctl4_txtName
 
J

Jerry Camel

Figured it out right after I sent that note. Thanks for the info - much
appreciated.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top