Adding Focus to new row to a DataGrid using Autogenerated Columns

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

Once a user clicks "add new row", a new row is created in my Datagrid. The
datasource is huge so they have to scroll to the bottom of the page to edit
the data in the new row. I have come across two EE solutions which use
javascript to focus on the textbox in the "editcommandcolumn" mode.
However, these solutions were made for non-autogenerated columns. I NEED
autogenerated columns for my project.

Is it possible to give focus to a textbox created from an autogenerated
column? If not i'll just live with it :)



Code I used in my "add row" click event:
Dim scriptJs As String
Dim index as Integer = DataSet11.Tables(0).Rows.Count-1
DataGrid1.EditItemIndex = index
myComponent.FillDataSet(DataSet11)
DataBind()
Dim focusBox As TextBox
focusBox = DataGrid1.Items(index).Cells(0).Controls(0) <-****
error here**** (TypeCast is invalid)

scriptJs = "<Script Language=JavaScript>" & vbCrLf
scriptJs &= "document.getElementById('" & focusBox.UniqueID &
"').focus();" & vbCrLf
scriptJs &= "document.getElementById('" & focusBox.UniqueID &
"').select();" & vbCrLf
scriptJs &= "<" & "/script>"
If (Not Me.IsStartupScriptRegistered("Startup")) Then
Me.RegisterStartupScript("Startup", scriptJs)
End If

Controls(0) is a LinkButton (for edit command column)
Controls(1) is a LinkButton (for deleting a column)
Controls(2) does not exist, etc

Here is my frontend asp.net code:

<asp:linkbutton id="LinkButton1" runat="server"
onclick="LinkButton1_Add">Create New Row</asp:linkbutton>
<br>
<asp:datagrid id="DBGrid1" runat="server" ondeletecommand="DBGrid1_Delete"
oneditcommand="DBGrid1_Edit"
oncancelcommand="DBGrid1_Cancel" onupdatecommand="DBGrid1_Update"
bordercolor="#DEDFDE" borderstyle="None"
borderwidth="1px" backcolor="White" cellpadding="4"
gridlines="Vertical" forecolor="Black"
datakeyfield="rowId">
<alternatingitemstyle backcolor="White"></alternatingitemstyle>
<itemstyle backcolor="#F7F7DE"></itemstyle>
<headerstyle font-bold="True" backcolor="#CCCC99"></headerstyle>
<columns>
<asp:editcommandcolumn buttontype="LinkButton"
updatetext="Update" canceltext="Cancel"
edittext="Edit"></asp:editcommandcolumn>
<asp:buttoncolumn text="Delete"
commandname="Delete"></asp:buttoncolumn>
</columns>
</asp:datagrid>
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top