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

How to place cursor on the selected DataGrid’s BoundColumn

Status
Not open for further replies.

Katya85S

Programmer
Jul 19, 2004
190
How to place cursor on the selected DataGrid’s BoundColumn:
<asp:BoundColumn DataField="NewEntry" Visible="false" />
Users clicks on an EditCommandColumn button:
<asp:EditCommandColumn EditText="Edit" ButtonType="PushButton" UpdateText="Submit" CancelText="Cancel" />
This makes that BoundColumn text field visible for user’s entry:
Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
myDataGrid.EditItemIndex = e.Item.ItemIndex
myDataGrid.Columns(5).Visible = True
BindData()
End Sub
 
You'll have to use javascript (specifically the "focus" method). You can create and register the javascript server-side using the RegisterClientScriptBlock or RegisterStartUpScript method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Also, when the item is created inside the datagrid it will receive a new name (something like ctrl01_txtWhatever). You'll need to set the focus method of the javascript using whatever the control name is.

Run your page, then view the source to determine what the name is, then write the javascript.
 
I'm sorry i didn't point it out. I do use javascript to set up focus.
Private Sub SetFocus(ByVal ctrl As Control)
Dim focusScript As String = "<script language='javascript'>" & _
"document.getElementById('" + ctrl.ClientID + "').focus();</script>"
Page.RegisterStartupScript("FocusScript", focusScript)
End Sub
Etc., I can place focus on dataGrid itself by:
SetFocus(myDataGrid)
But I have problem (don't know how to) place focus on the particular cell of the selected datagrid's record.
 
jshurst, thank you.
I didn't receive your advice, when submitted my reply. Now I see what I should look for. I'll check out the source code for ctrl01_txtWhatever.
Thank you!
 
Np, good luck. Let us know if you need any other help.
 
I do...
Source returns diferent control name for each row. Etc., at the first row the name, according to teh source, is 'myDataGrid:_ctl1:_ctl5', at the second - 'myDataGrid:_ctl2:_ctl5'.. and so on... Thus i cannot hardcode it.
I believe control's name could be found from that e As DataGridCommandEventArgs, that is passed to Sub dg_Edit (when user clicks the Edit button on one of the datagrid records). The focus should be set to Columns(5) cell of the e argument. I've tried: SetFocus(e.Item.Cells(5)), but it throws an error 'Document.GetElementDyID(...) is null or not an object'. So i guess i should specify teh cell somewhat differently. Don't know how to, though:
Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
myDataGrid.EditItemIndex = e.Item.ItemIndex
myDataGrid.Columns(5).Visible = True
BindData()
SetFocus(????????)
End Sub
Any ideas?
Tank you all in advance.
 
In the dg_edit code, try using a findcontrol() to find the control you want to set focus to, then pass that to your javascript
 
I found an answer in Scott’s Mitchell article It looks like the column, instead of BoundColumn should be TemplateColumn
<asp:TemplateColumn HeaderText="Question">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox id="txtDesc" runat="server" Width="95%"
Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</EditItemTemplate>
</asp:TemplateColumn>

And then server-side dgEdit sub will be:
Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

myDataGrid.EditItemIndex = e.Item.ItemIndex
BindData()
' Create a reference to the TextBox
Dim tb As TextBox
tb = dgAuction.Items(e.Item.ItemIndex).Cells(5).FindControl("txtDesc
")
SetFocus(tb)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top