使用 Gridview LinkBut​​ton 制作 If 语句

Make If Statments Using Gridview LinkButtons

我的 gridview 的每一行内都有 2 个链接按钮。 我想知道如何使用 If 语句来确定应该进行哪些更改。

我当前的 If 语句(我知道是错误的)如下:

If LinkButton1.Text = "Update" Then

    Dim row As GridViewRow = DisplayClassifieds.SelectedRow
    strFilter = row.Cells(1).Text

    strSelect = "SELECT Classid, Addate, Category, Username, Phonenbr, Email, Description, Fulldescription FROM TABLENAME WHERE Classid = '" & strFilter & "' "

        Page.Session.Add("Admin_Updates", strSelect)
        Response.Redirect("DispAd.aspx")

ElseIf LinkButton2.Text = "Delete" Then

    Dim ClassifiedStr As New OleDbCommand

        ClassifiedStr.CommandType = CommandType.StoredProcedure
        ClassifiedStr.CommandText = "delete_classifieds"
        ClassifiedStr.Connection = conn

        'Must be organized based on Stored Procedure
        ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter
        conn.Open()

        ClassifiedStr.ExecuteNonQuery()
        conn.Close()
        Response.AddHeader("Refresh", "1")

End if

我用什么代替我的台词If LinkButton1.Text = "Update"

更新:

我将 CommandName="UpdateRow" 和 "DeleteRow" 添加到 HTML Linkbutton 并执行了以下操作:

If LinkButton1.CommandName = "UpdateRow" 

ElseIf LinkButton2.CommandName = "DeleteRow" Then

但是,Delete 只是删除 LinkBut​​ton 而不是奇怪的数据库记录?!不知道为什么。

我还看到“显示”按钮只有在单击 Delete、更改页面、返回到已删除 Delete 的第一页后才会起作用。所以如果 Delete 存在 Display 不起作用。

已更新的完整版本无法使用 版本 1

Protected Sub DisplayClassifieds_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DisplayClassifieds.SelectedIndexChanged

    Dim conn As OleDbConnection = New OleDbConnection("Provider=""********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

    Dim strSelect As String
    Dim strFilter As String = " "
    ' Dim counter As Integer = 0
    ' Dim v As Integer = 0
    'cell = DisplayClassifieds[0,Row].Value


    Dim row As GridViewRow = DisplayClassifieds.SelectedRow
    strFilter = row.Cells(1).Text

    strSelect = "SELECT Classid, Addate, Category, Username, Phonenbr, Email, Description, Fulldescription FROM TABLENAME WHERE Classid = '" & strFilter & "' "

    If LinkButton1.commandName = "UpdateRow" Then
        Page.Session.Add("Admin_Updates", strSelect)
        Response.Redirect("DispAd.aspx")

    ElseIf LinkButton2.commandName = "DeleteRow" Then

        Dim ClassifiedStr As New OleDbCommand

        ClassifiedStr.CommandText = "DELETE * FROM TABLENAME WHERE Classid = '" & strFilter & "'"
        ClassifiedStr.Connection = conn

        'Must be organized based on Stored Procedure
        ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter
        conn.Open()

        ClassifiedStr.ExecuteNonQuery()
        conn.Close()
        Response.AddHeader("Refresh", "1")
        Response.Redirect("QRY2.aspx")
    End If


End Sub

版本 2

Sub LinkButton1_Click(sender As Object, e As EventArgs)
    Dim conn As OleDbConnection = New OleDbConnection("Provider=""********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

    Dim strSelect As String
    Dim strFilter As String = " "
    Dim counter As Integer = 0
    Dim v As Integer = 0
    'cell = DisplayClassifieds[0,Row].Value


    Dim row As GridViewRow = DisplayClassifieds.SelectedRow
    strFilter = row.Cells(1).Text

    strSelect = "SELECT Classid, Addate, Category, Username, Phonenbr, Email, Description, Fulldescription FROM TABLENAME WHERE Classid = '" & strFilter & "' "

    Page.Session.Add("Update_Values", strSelect)
    Response.Redirect("DispAdUpdate.aspx")
End Sub
Sub LinkButton2_Click(sender As Object, e As EventArgs)

    Dim conn As OleDbConnection = New OleDbConnection("Provider=""*******"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

    Dim strFilter As String = " "
    Dim row As GridViewRow = DisplayClassifieds.SelectedRow
    strFilter = row.Cells(1).Text

    Dim ClassifiedStr As New OleDbCommand

    ClassifiedStr.CommandType = CommandType.StoredProcedure
    ClassifiedStr.CommandText = "delete_classifieds"
    ClassifiedStr.Connection = conn

    'Must be organized based on Stored Procedure
    ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter
    conn.Open()

    ClassifiedStr.ExecuteNonQuery()
    conn.Close()
    Response.AddHeader("Refresh", "1")
    Response.Redirect("QRY2.aspx")
End Sub

我不太熟悉 using/calling 存储过程,但是,如果不是太麻烦,请尝试在命令文本 属性 中输入删除查询,例如

ElseIf LinkButton2.Text = "Delete" Then

Dim ClassifiedStr As New OleDbCommand


     ClassifiedStr.CommandText = "DELETE * FROM TABLENAME WHERE val_id = @val_id"
    ClassifiedStr.Connection = conn

    'Must be organized based on Stored Procedure
    ClassifiedStr.Parameters.AddWithValue("@val_id", strFilter)
    conn.Open()

    ClassifiedStr.ExecuteNonQuery()
    conn.Close()
    Response.AddHeader("Refresh", "1")

End if

因为我从来没有调用过存储过程,所以我只是猜测它与您为删除调用它的方式有关

您应该将 strFilter = row.Cells(1).Text 行放在 if 语句上方 (If LinkButton1.Text = "Update" Then).

看来做这个过程很辛苦。 我决定改用 "select" 选项,因为我的问题似乎很难。

我是这样做的:

对于 select 选项行:

Protected Sub DisplayClassifieds_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DisplayClassifieds.SelectedIndexChanged

    Dim row As GridViewRow = DisplayClassifieds.SelectedRow

End Sub

然后制作一个采用该索引的删除和更新按钮....

 Protected Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
    Dim conn As OleDbConnection = New OleDbConnection("Provider=""******"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)


    If Page.IsValid Then
        If DisplayClassifieds.SelectedIndex = -1 Then
            Response.Write("<script language=""javascript"">alert('You must select a record.');</script>")
            Exit Sub
        End If


        Dim ClassifiedStr As New OleDbCommand
        ClassifiedStr.CommandType = CommandType.StoredProcedure
        ClassifiedStr.CommandText = "delete_classifieds"
        ClassifiedStr.Connection = conn

        'Must be organized based on Stored Procedure
        'DataKey is the DataKey that we labeled as Classid(same name as ID field in Oracle)
        ClassifiedStr.Parameters.Add("val_id", OleDbType.Numeric).Value = CInt(DisplayClassifieds.SelectedDataKey.Value)
        conn.Open()

        ClassifiedStr.ExecuteNonQuery()

....etc

我的 VB.net 中的底部 "DataKey" 代码来自我使用 "DataKeyNames" 值所做的 table 选项:

<asp:GridView ID="DisplayClassifieds" runat="server" align="center" 
                Width="100%" AllowSorting="True" AutoGenerateColumns="False" 
                AutoGenerateSelectButton="True" EnableModelValidation="True" 
                BorderColor="Black" BorderStyle="Solid" DataKeyNames="Classid" >
    <Columns>

        <asp:BoundField DataField="Classid" HeaderText="ID" 
            SortExpression="Date" Visible = "false"> 
               <ItemStyle cssClass="grid_padding" />
        </asp:BoundField>

        ....etc
    </Columns>

</asp:GridView>