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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Focus on DataGrid field after new record inserted

Status
Not open for further replies.

wfweirich

Programmer
Sep 10, 2001
21
US
I have a web page that includes a datagrid. A button at the top of the page allows you to add a new record. When pressed a new record is added to the database(SQL Server), then the page is re-displayed showing the previously existing records and a new one. All entries in the datagrid are editable. There are 3 editable columns. What I would like to do is, after the new record is inserted, when the page returns, I would like the new entry selected for edit, and focus set on the first editable column. I have made numerous attempts using snippets I have found on the web. Still struggling! Your help would be very much appreciated.

wfw
 
there are many version of this setfocus sub. I use the below sub to set a basic focus on a control..

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)

Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"

RegisterStartupScript("focus", s)

End Sub

you will need to use the itemdatabound event of the grid. then use the e.item.findcontrol method to find the control. then just set focus to it.

Setfocus(ControlName)



 
my 2 pennies...

im not certain on using the idb event...id use the edit event...

Sub DataGrid_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
DataGrid1.EditItemIndex = E.Item.ItemIndex
BindGrid()
Dim tbFirst As Textbox = CType(DataGrid1.Items(DataGrid1.EditItemIndex).FindControl("ext"), TextBox)
setFocus(tbFirst)
End Sub

- in addition to the setFocus sub dvan posted above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top