如何在gridview asp.net vb语言中为按钮制作事件?

问题描述 投票:0回答:1

当我尝试单击 gridview 中更新所选行的按钮时出现错误

代码和图像中的错误

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



    If Not Page.IsPostBack Then
        Try
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Dim dt As New DataTable()

            con.ConnectionString = "Data Source=DESKTOP-L7LKJH5;Initial Catalog=logicist;Integrated Security=True"
            con.Open()

            cmd.CommandText = "select * from dalabaad where Status= @Status"
            cmd.Parameters.AddWithValue("@Status", "Pending".ToString)
            cmd.Connection = con

            Datagrid1.DataSource = cmd.ExecuteReader
            Datagrid1.DataBind()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If

    depart()
End Sub

Protected Sub Datagrid1_click(sender As Object, e As GridViewCommandEventArgs)
    ' Update button
    If (e.CommandName = "Select") Then
        Try
            Con.ConnectionString = "Data Source=DESKTOP-L7LKJH5;Initial Catalog=logicist;Integrated Security=True"
            Dim sqlText = "UPDATE dalabaad SET Status = @Status WHERE Invoice_No = '" + Datagrid1.SelectedIndex + "'"
            Using cnConnect = New SqlConnection(Con.ConnectionString)
                Using cm = New SqlCommand(sqlText, cnConnect)
                    cnConnect.Open()
                    cm.Parameters.Add("@Status", SqlDbType.NVarChar).Value = "Approved".ToString

                    cm.ExecuteNonQuery()
                    MsgBox("Updated")
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If


End Sub

Asp.net vb 代码

        <asp:GridView ID="Datagrid1" runat="server" AutoGenerateColumns="False" Width="860px" OnRowCommand="Datagrid1_click" >
<Columns>
    <asp:BoundField DataField="RefNo" HeaderText="RefNo" ReadOnly="True" />
    <asp:BoundField DataField="Invoice_No" HeaderText="Invoice_No" />
    <asp:BoundField DataField="Item_Name" HeaderText="ITem Name" />
    <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
    <asp:BoundField DataField="Department" HeaderText="Department" />
    <asp:BoundField DataField="ReceiverName" HeaderText="Receiver Name" />
    <asp:BoundField DataField="Date" HeaderText="Date" />
    <asp:BoundField DataField="Approval_By" HeaderText="Approval_By" />
    <asp:BoundField DataField="Status" HeaderText="Status" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button runat="server"  Text="Button" CommandName="Select"  ></asp:Button>
        </ItemTemplate>
    </asp:TemplateField>

</Columns>

Gridview 的 HTML 代码

未将对象引用设置为对象的实例。

当我点击 Gridview 中的按钮时出现错误消息

我尝试过多次换地方,但没有成功

asp.net vb.net asp.net-core asp.net-mvc-3
1个回答
0
投票

最好将一个普通的 asp.net 按钮放入该网格视图中。

所以,这个:

       <asp:Button id="cmdmyupdate" runat="server"  
            Text="Status Update" 
            OnClick="cmdmyupdate_Click"
        >
        </asp:Button>

那么,让我们创建点击事件(就像您对任何普通按钮所做的那样,对吧?)。

因此,输入时看起来像这样:(您需要/想要使用/让 intel-sense 为后面的代码创建按钮事件)。

enter image description here

好的,现在如果我们翻转到代码后面,我们就会点击按钮。

所以,现在你的代码可以是这样的:

我们需要从该行获取/抓取发票号码。

我们必须使用 .Cells() 集合(从零开始,因此发票编号是第二个,或“1”)。

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


    Dim btn As Button = sender
    Dim gvRow As GridViewRow = btn.NamingContainer
    Dim sInvoice As String = gvRow.Cells(1).Text

    Dim scon As String =
       "Data Source=DESKTOP-L7LKJH5;Initial Catalog=logicist;Integrated Security=True"

    Dim sqlText As String =
       "UPDATE dalabaad SET Status = @Status WHERE Invoice_No = @InvoiceNum"
    Using cnConnect = New SqlConnection(scon)
        Using cm = New SqlCommand(sqlText, cnConnect)
            cnConnect.Open()
            cm.Parameters.Add("@Status", SqlDbType.NVarChar).Value = "Approved"
            cm.Parameters.Add("@InvoiceNum", SqlDbType.NVarChar).Value = sInvoice
            cm.ExecuteNonQuery()
        End Using
    End Using

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.