Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Editing a row in a gridview by the use of the click into button event

I have this gridview in my application :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True">
        <Columns>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:Button ID="Savebtn" runat="server" Text="تحديث البيانات"  OnClick="gv_RowEditing"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="السعر الأقصى">
                <ItemTemplate>
                    <asp:TextBox ID="mintxt" runat="server" Text='<%#Eval("prix max")%>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="السعر الأدنى"  >
                <ItemTemplate>
                    <asp:TextBox ID="maxtxt" runat="server"  Text='<%#Eval("prix min")%>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Datvente" HeaderText="التاريخ" SortExpression="Datvente" />
            <asp:BoundField DataField="NomAdh" HeaderText="الإسم و اللقب" SortExpression="NomAdh" />
            <asp:BoundField DataField="CodAdh" HeaderText="المنخرط" SortExpression="CodAdh" />

             <asp:TemplateField >
                <ItemTemplate>

                    <asp:HiddenField ID="Ref" runat="server"  Value='<%#Eval("Ref")%>'/>
                </ItemTemplate>
            </asp:TemplateField>


        </Columns>
    </asp:GridView>

The event of the click into button :

Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As EventArgs)


        Try
            Dim min As Double = Double.Parse(GridView1.SelectedRow.Cells("mintxt").ToString())
            Dim max As Double = Double.Parse(GridView1.SelectedRow.Cells("maxtxt").ToString())
            Dim reference As String = Double.Parse(GridView1.SelectedRow.Cells("Ref").ToString())
            If min > max Then
                avis2.Text = "الصيغة خاطئة"
                Return
            End If
            DataAccessLayer.updatetraitementprix(min, max, reference)
            avis2.Text = ""
            FillingGrid(Session("region"), Session("date"), Session("speculation"))
        Catch ex As Exception
            avis2.Text = "الصيغة خاطئة"
            GridView1.Visible = True
            Return
        End Try
     Me.FillingGrid(Session("region"), Session("date"), Session("speculation"))
    End Sub

I need to get the value of the column Ref and the new values of fields maxtxt and mintxt . but it didn't work

err

Upvotes: 0

Views: 2950

Answers (3)

codeandcloud
codeandcloud

Reputation: 55200

There is absolutely need of RowEditing event. What you are doing is firing a simple OnClick event. So make these changes.

Markup ( Name change so that you may not confuse it with RowEditing event )

<asp:TemplateField >
    <ItemTemplate>
        <asp:Button ID="Savebtn" runat="server" 
            Text="تحديث البيانات" 
            OnClick="Savebtn_Click" />
    </ItemTemplate>
</asp:TemplateField>

Code-behind

Protected Sub Savebtn_Click(sender As Object, e As System.EventArgs)

    Dim btn As Button = CType(sender, Button)
    Dim clickedRow As GridViewRow = CType(btn.NamingContainer, GridViewRow)

    Dim minTextBox As TextBox = CType(clickedRow.FindControl("mintxt"), TextBox)
    Dim maxTextBox As TextBox = CType(clickedRow.FindControl("maxtxt"), TextBox)
    Dim refHidden As HiddenField = CType(clickedRow.FindControl("Ref"), HiddenField)

    Dim min As Double = Double.Parse(minTextBox.Text)
    Dim max As Double = Double.Parse(maxTextBox.Text)
    Dim reference As Double = Double.Parse(refHidden.Value)

    'rest of the code goes here

End Sub

Upvotes: 2

jack
jack

Reputation: 1113

The reason you're getting this error is actually because the selectedrow is nothing, so you first need to get the current selected row then access all the controls in that row.

Please replace your code to the following:

Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As EventArgs)

        Try
            'First, get the saveBtn
            Dim saveBtn As Button = DirectCast(sender, Button)

            'Next, get the selected row of that button
            Dim selectedRow As GridViewRow = DirectCast(saveBtn.Parent.Parent, GridViewRow)

            'Now you can access all the controls of that row
            Dim mintxt As TextBox = DirectCast(selectedRow.FindControl("mintxt"), TextBox)
            Dim maxtxt As TextBox = DirectCast(selectedRow.FindControl("mintxt"), TextBox)
            Dim Ref As HiddenField = DirectCast(selectedRow.FindControl("Ref"), HiddenField)

            'Get the values of the controls
            Dim min As Double = Double.Parse(mintxt.Text)
            Dim max As Double = Double.Parse(maxtxt.Text)
            Dim refVal As Double = Double.Parse(Ref.Value)

            If min > max Then
                avis2.Text = "الصيغة خاطئة"
                Return
            End If
            DataAccessLayer.updatetraitementprix(min, max, refVal)
            avis2.Text = ""
            FillingGrid(Session("region"), Session("date"), Session("speculation"))
        Catch ex As Exception
            avis2.Text = "الصيغة خاطئة"
            GridView1.Visible = True
            Return
        End Try
        Me.FillingGrid(Session("region"), Session("date"), Session("speculation"))

    End Sub

Upvotes: 1

Rafa Paez
Rafa Paez

Reputation: 4860

Since the TextBox controls are inside of TemplateField's, you have to use Control#FindControl method to find them. Be aware of the Cell's index for each control.

Dim tbMin As TextBox = CType(GridView1.SelectedRow.Cells(1).FindControl("mintxt"), TextBox)
Dim min As Double = Double.Parse(tbMin.Text)

Upvotes: 1

Related Questions