locked
VB.NET Grid view Item template field-Edit,update button outside RRS feed

  • Question

  • User-1578974752 posted

    I have to click Edit Button outside the gridview  and make itemtemplate column of grid view to be editable(make textbox visible ).If update is clicked the label will be visible and textbox invisible.

    but below code is showing error., IsInEditMode  is not defined.How to solve that..Thanks

    Dim [Property] As IsInEditMode ->in this line


      <ItemTemplate>
                                     <asp:Label ID="Label1" Visible= '<%# Not CBool(IsInEditMode)  %>' runat="server" Enabled='<%# Eval("QUANTITY") %>' Text="Label"></asp:Label>
                                     <asp:TextBox ID="TextBox1" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("QUANTITY") %>'></asp:TextBox>
                                 </ItemTemplate>


    Private Sub BindData()
        Dim connectionString As String = "Server=localhost;Database=School;Trusted_Connection=true"
        Dim myConnection As SqlConnection = New SqlConnection(connectionString)
        Dim ad As SqlDataAdapter = New SqlDataAdapter("SELECT UserID, FirstName, LastName FROM Users", myConnection)
        Dim ds As DataSet = New DataSet()
        ad.Fill(ds)
        gvUsers.DataSource = ds
        gvUsers.DataBind()
    End Sub

    Dim [Property] As IsInEditMode

     Private isEditMode As Boolean = False

        Protected Property IsInEditMode As Boolean
            Get
                Return Me.isEditMode
            End Get
            Set(ByVal value As Boolean)
                Me.isEditMode = value
            End Set
        End Property

     Class SurroundingClass
            Private isEditMode As Boolean = False

            Protected Property IsInEditMode As Boolean
                Get
                    Return Me.isEditMode
                End Get
                Set(ByVal value As Boolean)
                    Me.isEditMode = value
                End Set
            End Property
        End Class

    Saturday, October 27, 2018 5:39 AM

Answers

  • User-893317190 posted

    Hi shsu,

    Expression in <%#  %> could only visit data bound with your datasource, and your datasource is SELECT UserID, FirstName, LastName FROM Users without IsInEditMode.

    So you couldn't use IsInEditMode  in your gridview.

    If you want to make label and textbox visible or invisible in your gridview, you could use findControl.

    However , how do you ensure which row is in edit mode with a botton outside the gridview?

    I suggest you could use edit button  in each row,and use  OnRowUpdating and OnRowUpdating event.

    Below is a small sample for visiting  textbox in itemTemplate using button outside the gridview.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
    
               <Columns >
                   <asp:TemplateField HeaderText="edit">
                        <ItemTemplate>
                                     <asp:Label ID="Label1" Visible="true" runat="server" Enabled="true" Text="Label"></asp:Label>
                                     <asp:TextBox ID="TextBox1" Visible="false" runat="server" Text=''></asp:TextBox>
                                 </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
            </asp:GridView>
    
            <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click" />
            
            <asp:Button ID="Update" runat="server" Text="Update" OnClick="Update_Click" />

    Code behind.

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                GridView1.DataSource = New Int32() {1, 1}
                GridView1.DataBind()
            End If
    
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True
                End If
            Next
        End Sub
    
        Protected Sub Update_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = True
                    box.Visible = False
                End If
            Next
        End Sub

    Result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 29, 2018 2:14 AM
  • User-893317190 posted

    Hi shsu,

    If you want to check the value of the textbox, you could check it in the  update button's onclick event.

    Below is my code.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  >
    
               <Columns >
                   <asp:TemplateField HeaderText="edit">
                        <ItemTemplate>
                                     <asp:Label ID="Label1" Visible="true" runat="server" Enabled="true" Text='<%# DataBinder.GetDataItem(Container)   %>'></asp:Label>
                                        
                                     <asp:TextBox ID="TextBox1" Visible="false" runat="server" Text=''></asp:TextBox>
                                 </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
            </asp:GridView>
    
            <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click" />
            
            <asp:Button ID="Update" runat="server" Text="Update" OnClick="Update_Click" />

    Code behind. You loop through the gridview's rows and find the textbox and label then compare their value , if ok, you could update the value. Of course , you need a column to store the id of your  data to update the data.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                GridView1.DataSource = New Int32() {1, 4}
                GridView1.DataBind()
            End If
    
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True
                End If
            Next
        End Sub
    
        Protected Sub Update_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)  'get the original value
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)  ' get the data to update
                    If CInt(label.Text) < CInt(box.Text) Then
                        Response.Write("you could update" + "<br/>")
                        ' you could update the data in database  here
                    Else
                        Response.Write("you could not update" + "<br/>")
    
                    End If
    
    
                    label.Visible = True
                    box.Visible = False
                End If
            Next
        End Sub

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 29, 2018 4:34 AM
  • User-893317190 posted

    Hi shsu, 

    You could use the textbox's ontextchanged event.

    Below is my code.

    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  >
    
               <Columns >
                   <asp:BoundField DataField="item_number" HeaderText="id" />
    
                   <asp:BoundField  DataField="quantity" HeaderText="quantity" />
                   <asp:TemplateField HeaderText="edit">
    
                    
                        <ItemTemplate>
                                     <asp:Label ID="Label1" Visible="true" runat="server" Enabled="true" Text='<%# Eval("quantity") %>'></asp:Label>
                                        
                                     <asp:TextBox ID="TextBox1" Visible="false" runat="server" Text='' AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    <!--use the hiddenfield to record the index of current item-->
                            <asp:HiddenField ID="HiddenField1" runat="server" Value="<%# Container.DataItemIndex  %>"  />
                                 </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
            </asp:GridView>
    
            <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click" />
            
        
        </form>

    Code behind. 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
    
                Dim list As ArrayList = New ArrayList()
                list.Add(New With {.item_number = 5, .quantity = 6})
                list.Add(New With {.item_number = 3, .quantity = 4})
                list.Add(New With {.item_number = 1, .quantity = 2})
                list.Add(New With {.item_number = 2, .quantity = 9})
                list.Add(New With {.item_number = 4, .quantity = 12})
                list.Add(New With {.item_number = 6, .quantity = 11})
                GridView1.DataSource = list
                GridView1.DataBind()
            End If
    
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True
                End If
            Next
        End Sub
    
        
    
        Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
            Dim box As TextBox = CType(sender, TextBox)
    
            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(1).Text  'get the value of quantity
            Dim count As Int32 = -1
            If CInt(box.Text) < CInt(quantity) Then        ' check whether the texted value is bigger than quantity
    
                For Each item As GridViewRow In GridView1.Rows   ' if not, make the following row invisible 'you could also make the textbox of the 
                    If item.RowType = DataControlRowType.DataRow Then   'following rows disabled
                        count = count + 1
                        If count > CInt(hidden.Value) Then
                            item.Visible = False
                        End If
                    End If
                Next
    
            End If
    
            If CInt(box.Text) >= CInt(quantity) Then    ' if  so ,make the following row visible
    
                For Each item As GridViewRow In GridView1.Rows
                    If item.RowType = DataControlRowType.DataRow Then
                        count = count + 1
                        If count > CInt(hidden.Value) Then
                            item.Visible = True
                        End If
                    End If
                Next
    
            End If
    
    
        End Sub

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2018 1:55 AM

All replies

  • User-893317190 posted

    Hi shsu,

    Expression in <%#  %> could only visit data bound with your datasource, and your datasource is SELECT UserID, FirstName, LastName FROM Users without IsInEditMode.

    So you couldn't use IsInEditMode  in your gridview.

    If you want to make label and textbox visible or invisible in your gridview, you could use findControl.

    However , how do you ensure which row is in edit mode with a botton outside the gridview?

    I suggest you could use edit button  in each row,and use  OnRowUpdating and OnRowUpdating event.

    Below is a small sample for visiting  textbox in itemTemplate using button outside the gridview.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
    
               <Columns >
                   <asp:TemplateField HeaderText="edit">
                        <ItemTemplate>
                                     <asp:Label ID="Label1" Visible="true" runat="server" Enabled="true" Text="Label"></asp:Label>
                                     <asp:TextBox ID="TextBox1" Visible="false" runat="server" Text=''></asp:TextBox>
                                 </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
            </asp:GridView>
    
            <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click" />
            
            <asp:Button ID="Update" runat="server" Text="Update" OnClick="Update_Click" />

    Code behind.

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                GridView1.DataSource = New Int32() {1, 1}
                GridView1.DataBind()
            End If
    
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True
                End If
            Next
        End Sub
    
        Protected Sub Update_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = True
                    box.Visible = False
                End If
            Next
        End Sub

    Result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 29, 2018 2:14 AM
  • User-1578974752 posted

    Thanks fo the help

    I have added two textboxes as template fields in the gridview as per your code.
    <asp:TemplateField>

                                 <ItemTemplate>
                                   
                                     <asp:Label ID="Label94" Visible="true" Text='<%# DataBinder.Eval(Container.DataItem, "QUANTITY") %>'  runat="server" Enabled="true"></asp:Label>
                                     <asp:TextBox ID="TextBox2" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "QUANTITY") %>'  runat="server"></asp:TextBox>
                                 </ItemTemplate>

                             </asp:TemplateField>
    <asp:TemplateField>

                                 <ItemTemplate>
                                   
                                     <asp:Label ID="Label94" Visible="true" Text='<%# DataBinder.Eval(Container.DataItem, "ORD") %>'  runat="server" Enabled="true"></asp:Label>
                                     <asp:TextBox ID="TextBox2" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "ORD") %>'  runat="server"></asp:TextBox>
                                 </ItemTemplate>

                             </asp:TemplateField>


    These values of coloums have to be updated to the table together once I click update button .

    Protected Sub Update_Click(sender As Object, e As EventArgs) Handles Update.Click

    For Each item As GridViewRow In kgrid.Rows

    If item.RowType = DataControlRowType.DataRow Then

    Dim label As Label = CType(item.FindControl("Label1"), Label)

    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)

    label.Visible = True

    box.Visible = False

    'NEW

    Dim label2 As Label = CType(item.FindControl("Label94"), Label)

    Dim box2 As TextBox = CType(item.FindControl("TextBox2"), TextBox)

    label2.Visible = True

    box2.Visible = False

    'END

    conn.Open()

    cmd.Connection = conn

      cmd.CommandText = "update QtyR set QUANTITY='" + box.Text + "', ORD='" + box2.Text + "'  where ITEM_NUMBER =' " + TestID.Text + "'"

    Session.Add("Utestid", UserTestID.Text)

    cmd.ExecuteNonQuery()

     

     

     

    End If

    Next

    End Sub

    where ITEM_NUMBER =' " + TestID.Text + "'"  .  tHIS ITEM_NUMBER is a column inside the gridview .How to access that field..

     Quantity textbox must be validated such that if the amount is greater than orignal (another column of gridview) then can not update..this also is possible in gridview..? Thanks

    Monday, October 29, 2018 3:16 AM
  • User-893317190 posted

    Hi shsu,

    If you want to check the value of the textbox, you could check it in the  update button's onclick event.

    Below is my code.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  >
    
               <Columns >
                   <asp:TemplateField HeaderText="edit">
                        <ItemTemplate>
                                     <asp:Label ID="Label1" Visible="true" runat="server" Enabled="true" Text='<%# DataBinder.GetDataItem(Container)   %>'></asp:Label>
                                        
                                     <asp:TextBox ID="TextBox1" Visible="false" runat="server" Text=''></asp:TextBox>
                                 </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
            </asp:GridView>
    
            <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click" />
            
            <asp:Button ID="Update" runat="server" Text="Update" OnClick="Update_Click" />

    Code behind. You loop through the gridview's rows and find the textbox and label then compare their value , if ok, you could update the value. Of course , you need a column to store the id of your  data to update the data.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                GridView1.DataSource = New Int32() {1, 4}
                GridView1.DataBind()
            End If
    
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True
                End If
            Next
        End Sub
    
        Protected Sub Update_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)  'get the original value
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)  ' get the data to update
                    If CInt(label.Text) < CInt(box.Text) Then
                        Response.Write("you could update" + "<br/>")
                        ' you could update the data in database  here
                    Else
                        Response.Write("you could not update" + "<br/>")
    
                    End If
    
    
                    label.Visible = True
                    box.Visible = False
                End If
            Next
        End Sub

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 29, 2018 4:34 AM
  • User-1578974752 posted

    Thanks Ackerly.

     If CInt(label.Text) < CInt(box.Text) Then 

    comparing filed is not a template field ,it is a bound filed so how to capture its value? Appreciate the help

    If CInt(label.Text) < boundfiled

    I am updating each row.The primary key of the grid view dbtable is item_number.How to access the itemnumber filed (bound filed) in to a textbox as Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox

    The bound filed is the third column in the gridview

    cmd.CommandText = "update QtyR set QUANTITY='" + box.Text + "', ORD='" + box2.Text + "'  where ITEM_NUMBER =' " + TestID.Text + "'"

    Monday, October 29, 2018 4:52 AM
  • User-893317190 posted

    Hi shsu,

    You could use  item.Cell(2).Text to get the id.

    Below is the code.

      For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)  'get the original value
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)  ' get the data to update
                    If CInt(label.Text) < CInt(box.Text) Then
                        Response.Write("you could update" + "<br/>")
                        ' you could update the data in database  here
                        Response.Write("id is :" + item.Cells(2).Text + "<br/>")
                    Else
                        Response.Write("you could not update" + "<br/>")
    
                    End If
    
    
                    label.Visible = True
                    box.Visible = False
                End If
            Next

    Best regards,

    Ackerly Xu

    Monday, October 29, 2018 5:14 AM
  • User-1578974752 posted

    Thanks..Instead of this..is it possible like this?

    There are 2 columns .  column 1  have no value and   column 2 have value 5.

    Once user type 6 in column1,check whether value is greater than column 2 then not allow user to move to next column ,just like required field validator do  .instead of update button ,I have to place the above code in which place.Thanks

    Monday, October 29, 2018 9:23 AM
  • User-893317190 posted

    Hi shsu, 

    You could use the textbox's ontextchanged event.

    Below is my code.

    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  >
    
               <Columns >
                   <asp:BoundField DataField="item_number" HeaderText="id" />
    
                   <asp:BoundField  DataField="quantity" HeaderText="quantity" />
                   <asp:TemplateField HeaderText="edit">
    
                    
                        <ItemTemplate>
                                     <asp:Label ID="Label1" Visible="true" runat="server" Enabled="true" Text='<%# Eval("quantity") %>'></asp:Label>
                                        
                                     <asp:TextBox ID="TextBox1" Visible="false" runat="server" Text='' AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    <!--use the hiddenfield to record the index of current item-->
                            <asp:HiddenField ID="HiddenField1" runat="server" Value="<%# Container.DataItemIndex  %>"  />
                                 </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
            </asp:GridView>
    
            <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click" />
            
        
        </form>

    Code behind. 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
    
                Dim list As ArrayList = New ArrayList()
                list.Add(New With {.item_number = 5, .quantity = 6})
                list.Add(New With {.item_number = 3, .quantity = 4})
                list.Add(New With {.item_number = 1, .quantity = 2})
                list.Add(New With {.item_number = 2, .quantity = 9})
                list.Add(New With {.item_number = 4, .quantity = 12})
                list.Add(New With {.item_number = 6, .quantity = 11})
                GridView1.DataSource = list
                GridView1.DataBind()
            End If
    
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
                If item.RowType = DataControlRowType.DataRow Then
                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True
                End If
            Next
        End Sub
    
        
    
        Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
            Dim box As TextBox = CType(sender, TextBox)
    
            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(1).Text  'get the value of quantity
            Dim count As Int32 = -1
            If CInt(box.Text) < CInt(quantity) Then        ' check whether the texted value is bigger than quantity
    
                For Each item As GridViewRow In GridView1.Rows   ' if not, make the following row invisible 'you could also make the textbox of the 
                    If item.RowType = DataControlRowType.DataRow Then   'following rows disabled
                        count = count + 1
                        If count > CInt(hidden.Value) Then
                            item.Visible = False
                        End If
                    End If
                Next
    
            End If
    
            If CInt(box.Text) >= CInt(quantity) Then    ' if  so ,make the following row visible
    
                For Each item As GridViewRow In GridView1.Rows
                    If item.RowType = DataControlRowType.DataRow Then
                        count = count + 1
                        If count > CInt(hidden.Value) Then
                            item.Visible = True
                        End If
                    End If
                Next
    
            End If
    
    
        End Sub

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2018 1:55 AM
  • User-1578974752 posted

    Thanks Ackerly.

    If the number is less than other value ,all rows except the editing one is disabled . Since I have 2 columns for editing. So is it possible, the editing cell only become enable and another column of the same row also become disable

    Is it possible

    Tuesday, October 30, 2018 2:47 AM