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 = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.UniqueID & "').focus(); </SCRIPT>"
RegisterStartupScript("focus", 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("edtAppointmentDate"
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="grdAppointments:_ctl3:edtAppointmentDate" type="text" value="12/25/2002 11:00 AM" id="grdAppointments__ctl3_edtAppointmentDate" class="fEdtText" size="20" style="height:18px;width:150px;" />
==== END_code ====
Any ideas please? Thanks.
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 = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.UniqueID & "').focus(); </SCRIPT>"
RegisterStartupScript("focus", 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("edtAppointmentDate"
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="grdAppointments:_ctl3:edtAppointmentDate" type="text" value="12/25/2002 11:00 AM" id="grdAppointments__ctl3_edtAppointmentDate" class="fEdtText" size="20" style="height:18px;width:150px;" />
==== END_code ====
Any ideas please? Thanks.