2

我的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

但是,删除一个只是删除 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
4

3 回答 3

1

您应该将 strFilter = row.Cells(1).Text 行放在 if 语句上方(如果 LinkBut​​ton1.Text = "Update" Then)。

于 2015-06-24T16:05:31.123 回答
1

看起来做这个过程非常困难。我决定改用“选择”选项,因为我的问题似乎很困难。

在此处输入图像描述

我这样做是这样的:

对于选择选项行:

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”值制作的表格选项:

<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>
于 2015-06-26T13:54:27.213 回答
0

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

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

由于我从未调用过存储过程,我只是猜测它与您调用它进行删除的方式有关

于 2015-06-24T15:28:49.670 回答