set focus in a datagrid

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
I'm trying to set the focus to a textbox in a datagrid when the user starts editing the grid.

I copied some code from 4Guys but it's not happening for me

Code:
' Create a reference to the TextBox
        Dim descTB As TextBox
        descTB = LayerGrid.Items(e.Item.ItemIndex).Cells(2).Controls(0)
        Me.txtStatus.Text = descTB.ClientID.ToString
        'Set the script to focus and select the TextBox
        RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
             vbTab & "Form1." & descTB.ClientID & ".focus();" & _
             vbCrLf & vbTab & "Form1." & descTB.ClientID & ".select();" & _
             vbCrLf & "<" & "/script>")

The output to the text box is in the form:
LayerGrid__ctl3__ctl0


But the source says this:
<input name="LayerGrid:_ctl3:_ctl0"

And I get no focus.

Granted the 4Guys code must have been for a templated grid because they know the name of the textbox that pops up for editing

descTB = dgPopularFAQs.Items(e.Item.ItemIndex).Cells(2).FindControl("txtDesc")

but I don't think I know that in my case. I'm just using bound columns.

Any suggestions?
 
and I used something like this:

Code:
 value = "<script language=""JavaScript"">" & vbCrLf & _
           vbTab & "document.Form1." & ctrlClientID & ".focus();" & _
                   vbCrLf & "<" & "/script>"
        RegisterStartupScript("focus", value)
 
I have the "value" built first, wonder if that would make a difference.

But do a search for RegisterStartupScript , u might find more examples/
 
Still no love on this one.

I know that I have the correct control referenced because I can inject a value into the textbox if I want.

Code:
Sub OrphanGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Orphangrid.EditCommand
        Orphangrid.EditItemIndex = e.Item.ItemIndex
        FillOrphanGrid()
        ' Create a reference to the TextBox
        Dim descTB As TextBox
        descTB = Orphangrid.Items(e.Item.ItemIndex).Cells(2).Controls(0)
        descTB.Text = "9"
        Me.txtStatus.Text = descTB.ClientID.ToString
        'Set the script to focus and select the TextBox
        RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
             vbTab &  "Form1." & descTB.ClientID & ".focus();")  
    End Sub

And I get the value 9 in the textbox yet the focus is not on it.
Strange. It seems like ClientID is not what I should be using. I have confirmed that my Form is called Form1.
 
Yeah.

I'm sort of narrowing in on the issue I believe.

In the view source the control is refered to as:

Orphangrid:_ctl2:_ctl0

If I try to hard code this I get a Javascript error about missing a ';'.
Maybe this is caused by the colons somehow.


Yet the ClientID return from the code is:

Orphangrid__ctl2__ctl0

If I have this in the Javascript I get an error about it not being found on the page.


My previous code had some oversights. Here is what I have now:

Code:
strJava = "<script language=""JavaScript"">" & "Form1." & descTB.ClientID & ".focus();" & "<" & "/script>"
RegisterStartupScript("focus", strJava)
 
I finally got it fixed by switching to a templated edit column in the data grid and finding the control by name. Not sure why the other method doesn't work, but I guess templated columns are better anyway.
 
Well, solving one problem generates yet another.

I switched over to Templated columns like so:

Code:
<asp:TemplateColumn HeaderText="GID">
<ItemTemplate>	<%# DataBinder.Eval(Container.DataItem, "GID") %></ItemTemplate>
<EditItemTemplate>
<asp:textbox runat="server" id="OrphanGIDEdit" Width="30px"></asp:textbox>
</EditItemTemplate>   
</asp:TemplateColumn>

Which is nice because now the focus emit Javascript code does work:

Code:
Sub OrphanGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Orphangrid.EditCommand
        Orphangrid.EditItemIndex = e.Item.ItemIndex
        FillOrphanGrid()
        ' Set focus to the edit text box
        Dim descTB As TextBox
        descTB = Orphangrid.Items(e.Item.ItemIndex).Cells(2).FindControl("OrphanGIDEdit")
        RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
               vbTab & "Form1." & descTB.ClientID & ".focus();" & _
               vbCrLf & vbTab & "Form1." & descTB.ClientID & ".select();" & _
               vbCrLf & "<" & "/script>")
    End Sub


The only problem now is that when the user clicks on the edit button and this textbox pops up it is empty! Before when I was using Bound Columns I and I couldn't get the focus to work I would get the edit box and it would have the old value in it. Now I get a blank. I would like to have the current value in there for the user to see.
I am using identical code to a page that does work this way
http://aspnet.4guysfromrolla.com/demos/dgExample21.aspx

Yet, I get different results than they do.
 
Back
Top