Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid, EditItem, TextBox JavaScript Focus Problem

Status
Not open for further replies.

jkl

Programmer
May 16, 2001
83
US
I have a datagrid with several items. When I click a link to "edit" an item, I want the focus of the page to be on the first textbox for that item, however this is not the case. When I click "edit", the page re-focuses to the top.

Here's what I'm using to try to accomplish this:

==== BEGIN_code ====
Friend Sub setFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String = &quot;<SCRIPT language='javascript'>document.getElementById('&quot; & ctrl.UniqueID & &quot;').focus(); </SCRIPT>&quot;
RegisterStartupScript(&quot;focus&quot;, s)
End Sub
==== END_code ====



I call this function during the itemCreated event for my datagrid:

==== BEGIN_code ====
Sub ItemCreated_Appointments(ByVal Sender As Object, ByVal e As DataGridItemEventArgs)
Select Case e.Item.ItemType

Case ListItemType.EditItem
Dim field1 As TextBox = e.Item.FindControl(&quot;edtAppointmentDate&quot;)
setFocus(field1)

End Select
End Sub
==== END_code ====



The problem with this method, is that the resulting Javascript doesn't work. Here's the Javascript:

==== BEGIN_code ====
<SCRIPT language='javascript'>document.getElementById('edtAppointmentDate').focus(); </SCRIPT>
==== END_code ====



edtAppointmentDate is the ID of the textbox element in the code, but when the page gets rendered the ID is different. Here's the resulting HTML for this textbox:

==== BEGIN_code ====
<input name=&quot;grdAppointments:_ctl3:edtAppointmentDate&quot; type=&quot;text&quot; value=&quot;12/25/2002 11:00 AM&quot; id=&quot;grdAppointments__ctl3_edtAppointmentDate&quot; class=&quot;fEdtText&quot; size=&quot;20&quot; style=&quot;height:18px;width:150px;&quot; />
==== END_code ====


Any ideas please? Thanks.



 
it seems like the id for your textbox is
grdAppointments__ctl3_edtAppointmentDate
instead of
edtAppointmentDate

hth Daren J. Lahey
Just another computer guy...
 
Yes the resultant client-side ID for the control is grdAppointments__ctl3_edtAppointmentDate and not the server-side, pre-compiled id that i gave it of edtAppointmentDate.

But I found another way of accomplishing this. Because the clientID attribute is not available until after all server-side processing, I created a public sub which references the clientID on the control, in-line, after all server-side processing is done.
 
Interesting. I would like to see that code... Daren J. Lahey
Just another computer guy...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top