Link to home
Start Free TrialLog in
Avatar of tentavarious
tentavarious

asked on

setting the focus on a row within a datagrid on a webform

I am trying to set the focus to a certain row within a datagrid upon a selection made from a user this is how it is set up the user selects a sub heading from a drop down box and the focus gets set to the corresponding row on the datagrid. the problem is the row gets changed to textboxes is there any way around this.  my code is below.

 Select Case ddlAccount.SelectedItem.Value
            Case "1"
                numhold = "89"
            Case "2"
                numhold = "124"
            Case "3"
                numhold = "134"
            Case "4"
                numhold = "144"
            Case "5"
                numhold = "155"
            Case "6"
                numhold = "161"
            Case "7"
                numhold = "165"
            Case "8"
                numhold = "184"
            Case "9"
                numhold = "190"
            Case "10"
                numhold = "200"
            Case "11"
                numhold = "203"
            Case "12"
                numhold = "210"
end select


 if you look at the "dim focusBox as textbox" thats how the focus gets set to the row i dont want that because the row gets turned into textboxes. is there a better way to set the focus without changing the row?  


  Dim Dataset11 As New DataSet
        Dim scriptJs As String
        Dim objDataAdapter1 As New OleDbDataAdapter("SELECT * FROM [accounts$]", strConn)
        objDataAdapter1.Fill(Dataset11) 'dataadapter method
        dgAccounts.DataSource = Dataset11.Tables(0).DefaultView
        Dim index As Integer
        index = numhold
        dgAccounts.EditItemIndex = index

        dgAccounts.DataBind()
        Dim focusBox As TextBox
        focusBox = dgAccounts.Items(index).Cells(0).Controls(0)

        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
Avatar of EBatista
EBatista

not sure if i understand your Q, anyway, look at this link
http://aspnet.4guysfromrolla.com/articles/090902-1.aspx

are you saying that you dont want the textboxs to be rendered?
Hi tentavarious,

Actually when you use
dgAccounts.EditItemIndex = index
dgAccounts.DataBind()

you tell to DataGrid that for this Item with Index inde, datagrid should use the controls in the EditTemplate
i don't understand what is the purpose of setting focus on row in the datagrid - but actually you can add additional column , with a hidden field in it
then when you want to set focus on the row - just set the focus to this hidden field


HTH
B..M
Avatar of tentavarious

ASKER

The purpose of setting the focus is because the datagrid has around 400 rows and is split into subsections that i gave up top they are in a dropdownlist and instead of having the user scrolling through the whole grid, i want to give the user the option of jumping to a specific spot within the grid by selecting the subheading from the dropdownlist.  The only way i have found how to do this is by setting the focus on a row, and the only way i now how to set the focus is within a text box so the row gets converted to textboxes so the focus can be set and i dont like doing it that way.  The hidden field sounds promising how would i go about doing that.  Would the whole column be invisible?  could you elborate?
no
you have to set the visible/invisible state of the column manual
but you can insert the hidden field in one of other columns
example
<Columns>
  <asp:TemplateColumn>
    <ItemTemplate>
        <asp:TextBox id="YourTextBOx" runat="server" /> <!-- This is some of your controls that you are currently use-->
        <input type="hidden" id="someid">
    </ItemTemplate>
  </asp:TemplateColumn>
</Columns>


B..M
But how can i set the focus to the text box if its invisible?  Also how would i compare it to the case select i have at the top?
NO, you won't set focus to INVISIBLE TextBox
you can use this
 Dim focusField As HtmlInputHidden
        focusField = dgAccounts.Items(index).Cells(0).FindCOntrol("someid")

        scriptJs = "<Script Language=JavaScript>" & vbCrLf
        scriptJs &= "document.getElementById('" & focusField .UniqueID & "').focus();" & vbCrLf

<Columns>
  <asp:TemplateColumn>
    <ItemTemplate>
        <asp:TextBox id="YourTextBOx" runat="server" /> <!-- This is some of your controls that you are currently use-->
        <input type="hidden" id="someid" runat="server" name="somename">
    </ItemTemplate>
  </asp:TemplateColumn>
</Columns>

B..M
Doesn't work i get an error saying can't move focus because it's invisible, not enabled or type that does not accept focus.  Is there anyotherway to jump to a row within a datagrid?  Or is the only way to set the focus?
look here
https://www.experts-exchange.com/questions/20943480/How-to-unfocus.html

may be the problem is that in my suggestion id and name attributes are different, but may be they should be the same
B..M
i call the method from the drop down event
 setfocus("hiddenfocus")

I added the template column
<input type="hidden" name="hiddenfocus" />

now how can i set the focus to the row that gets selected from the drop down, the numhold variable is equal to the row number on the datagrid. any ideas? i think this is the right way to do it.
 Sub setfocus(ByVal ctrl As String)
        'Please remember JavaScript objects are case sensitive
        Dim sJava As String = "<script language='Javascript'>document.Form1." & ctrl & ".focus(); </script>"
        RegisterStartupScript("setfocus", sJava)
    End Sub

i tried changing the setfocus sub procedure, but i cant get it to work i get a subscript out of range.  How can i get "ctrl" to equal the row number so the focus will get set to the correct row?

 Sub setfocus(ByVal ctrl As String)

        Dim index As Integer
        index = numhold
        ctrl = dgAccounts.Items(index).Cells(0).Controls(0).ToString
        Dim sJava As String = "<script language='Javascript'>document.Form1." & ctrl & ".focus(); </script>"
        RegisterStartupScript("setfocus", sJava)
    End Sub
numhold may be greater than the row count. In this case you will get Subscript out of range
How can i fix this? The number of rows goes past 300 and the highest value numhold could be is 240, numhold is a variable that gets set to a row number depending on what the user selects from the drop down.  What i need is for the focus to be set to the row number that is equal to the numhold, how can i change my above code to make it work?
make sure that the quantity of row is less than numhold
try
   focusField = dgAccounts.Items(index).Cells(0).FindCOntrol("someid")
catch
   msgbox("Sh........t")
End try
I dont know if that will work i have changed the sub procedure to

 Sub setfocus(ByVal ctrl As HtmlInputHidden)
Dim index As Integer
        Dim Dataset11 As New DataSet
        Dim objDataAdapter1 As New OleDbDataAdapter("SELECT * FROM [accounts$]", strConn)
        objDataAdapter1.Fill(Dataset11) 'dataadapter method
        dgAccounts.DataSource = Dataset11.Tables(0).DefaultView
        index = numhold
        dgAccounts.EditItemIndex = index
        dgAccounts.DataBind()

        ctrl = dgAccounts.Items(index).Cells(0).Controls(0)
        Dim sJava As String = "<script language='Javascript'>document.Form1." & ctrl.UniqueID & ".focus(); </script>"
        RegisterStartupScript("setfocus", sJava)
end sub

I dont have a focusField variable i am trying to set the focus to a template column i created within my datagrid dgAccounts. the focus should be set to the same row number as selected from the dropdownlist. I call the sub procedure
setfocus from the dropdownlist event and pass the "hiddenfocus" argument. I get an error on this line of code
"  ctrl = dgAccounts.Items(index).Cells(0).Controls(0)" I know i am not doing something right, but i can't figure out what.

<asp:datagrid id="dgAccounts" runat="server">
<Columns
      <asp:TemplateColumn>
      <ItemTemplate>                                                            <input type="hidden" name="hiddenfocus" />                                          </ItemTemplate>                                                      </asp:TemplateColumn>
</Columns>
</asp:datagrid>
Ok i changed it up and i got this error
The cast is not valid, before i had the focusbox as a textbox but then it converted the rows to textboxes which i dont want is there any control that i can set the focusbox to. that will not convert the rows and still be able to recieve the focus.
 Dim focusBox As HtmlInputHidden
Line 160:        focusBox = dgAccounts.Items(index).Cells(0).Controls(0)
Line 161:

Here is my code
  Dim Dataset11 As New DataSet
        Dim scriptJs As String
        Dim objDataAdapter1 As New OleDbDataAdapter("SELECT * FROM [accounts$]", strConn)
        objDataAdapter1.Fill(Dataset11) 'dataadapter method
        dgAccounts.DataSource = Dataset11.Tables(0).DefaultView
        Dim index As Integer
        index = numhold
        dgAccounts.EditItemIndex = index

        dgAccounts.DataBind()
        Dim focusBox As HtmlInputHidden
        focusBox = dgAccounts.Items(index).Cells(0).Controls(0)

        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
i solved it, i just changed all my textbox properties to fit the rest of the datagrid and it blends in prettygood.

focusBox.ReadOnly = True
        focusBox.ForeColor = Color.DarkSlateBlue
        focusBox.Font.Size = Medium
        focusBox.BorderStyle = Groove

thanks for all of the help
ASKER CERTIFIED SOLUTION
Avatar of dante469
dante469

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial