locked
curson on template textfield inside the gridview RRS feed

  • Question

  • User-1578974752 posted

    Hi

    Inside the grid view, I have  text box1 with Text changed event. After executing the code in textchanged event I want to set the mouse cursor on the next template textbox (textbox15) which is in cell(12)
    but I can not use boxg.Focus() as boxg is a string error message is showing

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)

     Dim boxg As String = CType(box.NamingContainer, GridViewRow).Cells(12).Text

    Appreciate the help

    Tuesday, March 12, 2019 9:17 AM

Answers

  • User-893317190 posted

    Hi shsu,

    You haven't set the textbox focus in your TextBox1_TextChanged.

    Below is  my test code.

    Public Partial Class MoveCursor
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                GridView1.DataSource = New Integer() {1, 2, 3, 4, 5}
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim row As GridViewRow = TryCast(((TryCast(sender, TextBox)).NamingContainer), GridViewRow)
            Dim field As TextBox = (TryCast(row.FindControl("TextBox2"), TextBox)) ' find the textbox to be focused
    
            For Each item As GridViewRow In GridView1.Rows
    
                If item.RowIndex <> row.RowIndex Then
                    item.Enabled = True
                    item.Visible = False
                End If
            Next
    
            field.Focus()
        End Sub
    End Class
    <form id="form1" runat="server">
            <div style="width:100px;overflow:scroll">
            <asp:GridView ID="GridView1" runat="server">
    
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
                          
                     
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
                </div>
    
        </form>

    My result.

    If it is not your  case , please post your aspx code.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 25, 2019 6:01 AM

All replies

  • User-943250815 posted

    The only way I see is calling a javascript from code behind

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
     Dim boxg As String = CType(box.NamingContainer, GridViewRow).Cells(12).Text
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "text", "MoveToNextControl()", True)

    Tuesday, March 12, 2019 11:49 AM
  • User-1174608757 posted

    Hi shsu,

    According to your description, I have made a sample here.

     Dim boxg As String = CType(box.NamingContainer, GridViewRow).Cells(12).Text

    You should define it as Texbox but not String.

    You could both just set the focus  in code behind or you could call javascript in code behind. Here is the demo ,I hope it could help you.

    1.set focus in code behind:
    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
       <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                
                <asp:GridView ID="GridView1" runat="server" ShowFooter="true">
                    <Columns>
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:TextBox  ID="TextBox2" runat="server"></asp:TextBox>
                            </FooterTemplate>
    
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    

    aspx.vb:

    Public Class Focus
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim sql As String = "select * from students"
                GridView1.DataSource = sqlhelper.ExecuteDataTable(sql)
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
            'Page.ClientScript.RegisterStartupScript(Page.GetType(), " ", "aa()", True)
            Dim box As System.Web.UI.WebControls.TextBox = GridView1.FooterRow.FindControl("TextBox2")
            box.Focus()
    
        End Sub
    
    End Class

    use javascript:

    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.3.1.js"></script>
        <script>
            function aa() { 
               
                    $(".TextBox2").focus()
    
             
    
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                
                <asp:GridView ID="GridView1" runat="server" ShowFooter="true">
                    <Columns>
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:TextBox  class="TextBox2" runat="server"></asp:TextBox>
                            </FooterTemplate>
    
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    Public Class textbox
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                 Dim sql As String ="select * from students"
                GridView1.DataSource = sqlhelper.ExecuteDataTable(sql)
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
            Page.ClientScript.RegisterStartupScript(Page.GetType(), " ", "aa()", True)
        End Sub
    End Class

    You could see as below:

    Best Regards

    Wei 

    Wednesday, March 13, 2019 2:56 AM
  • User-1578974752 posted

    Thanks

    Here both text boxes are inside the  Grid view

    Actually,textbox1 and textbox2 are 2 template fields inside the gridview .The location is near by so that with tab the cursor will autamaticaly move to textbox2.text  .

    Textchanged have some validation .

    But here when I change textbox1 value then postback is happening and then cursor is not showing on the next textbox. tried your solutions but not working. Thanks

    Thursday, March 14, 2019 6:35 AM
  • User-1174608757 posted

    Hi shsu, 

    Could you please post your code about your program? I hope you could post details about both in front end and  code behind. It will help me to solve your problem.

    Best Regards

    Wei

    Thursday, March 14, 2019 7:16 AM
  • User-943250815 posted

    When you have a TemplateField with controls inside you have to use FindControl, to get nested control. Gridview bellow have 2 textbox in a single column

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RecID" DataSourceID="LinqMyData">
        <Columns>
          <asp:BoundField DataField="RecID" HeaderText="RecID" InsertVisible="False" SortExpression="RecID" />
          <asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="True" SortExpression="Code" />
          <asp:TemplateField HeaderText="Name/Description">
            <ItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="True"></asp:TextBox>
              <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Notes">
            <ItemTemplate>
              <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
    <script>
      //function MoveToNextControl(ctrlID) { $(ctrlID).focus() } 
    </script>

    With some adjustments of Wei Zang suggestion for Focus or Javascript (commented). I choose Focus, very simple
    For Javascript you have to send ClientID as a parameter since it change for each row

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
        Dim gRow As GridViewRow = sender.NamingContainer
        Dim gCell As TableCell = gRow.Cells(6)
        Dim tBox1 As TextBox = gCell.FindControl("TextBox1")
        Dim zText = tBox1.Text
    
        Dim tBox3 As TextBox = gRow.FindControl("TextBox3")
        tBox3.Focus()
    
    ' To call javascript 'Dim tBox3ClientID As String = tBox3.ClientID 'Page.ClientScript.RegisterStartupScript(Page.GetType(), "text", "MoveToNextControl(tBox3ClientID )", True) End Sub

    Thursday, March 14, 2019 2:19 PM
  • User-1578974752 posted

    Below is my code . Textbox1 is a template field inside the Gridview and after that I want the cursor on Cells(12).

    Dim quantityNEW2 As String = CType(box.NamingContainer, GridViewRow).Cells(12).Text

    Actually it coming but disappearing that moment itself. The gridview is in DIV with scroll bar. Even for the First row ,cursor is disappearing after coming to next cell. Appreciate the help
            
         

    Code as below:

     Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
            Dim box As TextBox = CType(sender, TextBox)
           
            'Dim k1 As System.Web.UI.WebControls.TextBox = CType(box.NamingContainer.FindControl("HiddenField1"), HiddenField)

            Dim hidden As HiddenField = CType(box.NamingContainer.FindControl("HiddenField1"), HiddenField) 'get the hidden which records the index of current row
            Dim quantity As String = CType(box.NamingContainer, GridViewRow).Cells(6).Text  'get the value of quantity
            'NEW
        
            Dim quantityNEW2 As String = CType(box.NamingContainer, GridViewRow).Cells(12).Text
        
            Dim count As Int32 = -1
            If box.Text = "" Then   'box is  reciept
                box.Text = 0
            End If
            If CInt(box.Text) > CInt(quantity) Then       

                For Each item As GridViewRow In kgrid.Rows   ' if not, make the following row invisible
                    If item.RowType = DataControlRowType.DataRow Then   
                        count = count + 1
                        If count > CInt(hidden.Value) Or count < CInt(hidden.Value) Then
                            item.Enabled = False
                            Update.Visible = False
                            MESSAGELBL.Text = "Quantity must be Less than ABC"
                        End If
                        If count = CInt(hidden.Value) Then
                            Update.Visible = False
                            MESSAGELBL.Text = "Quantity must be Less than ABC"
                        End If
                    End If
                Next
            Else

            End If

            If CInt(box.Text) <= CInt(quantity) Then    ' if  so ,make the following row visible

                For Each item As GridViewRow In kgrid.Rows
                    If item.RowType = DataControlRowType.DataRow Then
                        count = count + 1
                        If count > CInt(hidden.Value) Or count < CInt(hidden.Value) Then
                            item.Enabled = True
                            Update.Visible = True
                            MESSAGELBL.Text = ""
                        End If
                        If count = CInt(hidden.Value) Then
                            Update.Visible = True
                            MESSAGELBL.Text = ""
                        End If
                    End If
                Next

            End If

    Wednesday, April 24, 2019 2:14 AM
  • User-893317190 posted

    Hi shsu,

    You haven't set the textbox focus in your TextBox1_TextChanged.

    Below is  my test code.

    Public Partial Class MoveCursor
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                GridView1.DataSource = New Integer() {1, 2, 3, 4, 5}
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim row As GridViewRow = TryCast(((TryCast(sender, TextBox)).NamingContainer), GridViewRow)
            Dim field As TextBox = (TryCast(row.FindControl("TextBox2"), TextBox)) ' find the textbox to be focused
    
            For Each item As GridViewRow In GridView1.Rows
    
                If item.RowIndex <> row.RowIndex Then
                    item.Enabled = True
                    item.Visible = False
                End If
            Next
    
            field.Focus()
        End Sub
    End Class
    <form id="form1" runat="server">
            <div style="width:100px;overflow:scroll">
            <asp:GridView ID="GridView1" runat="server">
    
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
                          
                     
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
                </div>
    
        </form>

    My result.

    If it is not your  case , please post your aspx code.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 25, 2019 6:01 AM
  • User-1578974752 posted

    Thanks Ackerly Xy. It is working now.

    Friday, April 26, 2019 8:27 AM
  • User-893317190 posted

    Hi shsu,

    Not sure why you meet this problem, in my case , even I move to rows at the bottom, the focus will not lose.

     Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim row As GridViewRow = TryCast(((TryCast(sender, TextBox)).NamingContainer), GridViewRow)
            Dim field As TextBox = (TryCast(row.FindControl("TextBox2"), TextBox))
    
            For Each item As GridViewRow In GridView1.Rows
    
                If item.RowIndex <> row.RowIndex Then
                    item.Enabled = False
                End If
            Next
    
            field.Focus()
        End Sub
      

    Could you share your   mark up in aspx to let us reproduce your problem?

    Best regards,

    Ackerly Xu

    Friday, April 26, 2019 9:12 AM