locked
Cursor is disappearing after reaching the template Textbox form another template textbox inside Gridview -vb.net RRS feed

  • Question

  • User-1578974752 posted

    There are 2 template textboxes inside the grid view. Edit and update button are outside the gridview so that on Click Edit all template textboxes will be visible and while updating labels will be visible.While Edit,cursor is moving from one textbox to another but then disappearing. ( If validation fails in textbox1,then all other rows will become disabled).

    How can I make cursor in the template textbox2 upon tab the template textbox1.It is working but disappearing.  Appreciate the help

    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

    Thursday, April 25, 2019 2:54 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>

    The 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 5:58 AM