【问题标题】:Make If Statments Using Gridview LinkButtons使用 Gridview 链接按钮制作 If 语句
【发布时间】:2015-06-24 14:36:44
【问题描述】:

我的 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、更改页面、返回已删除 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

【问题讨论】:

    标签: asp.net vb.net if-statement gridview asplinkbutton


    【解决方案1】:

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

    【讨论】:

    • 我以前看过这个,所以我做了那个更正。还是一样的结果。如果您愿意,我可以显示完整的更新代码?
    • 请分享链接按钮点击事件的完整代码。
    • 我的按钮点击版本以及是否上传了语句版本
    • 尝试使用 ClassifiedStr.Parameters.Add(New OleDbParameter("val_id", strFilter)) 代替 ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter跨度>
    【解决方案2】:

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

    我是这样做的:

    对于选择选项行:

    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>
    

    【讨论】:

      【解决方案3】:

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

      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
      

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

      【讨论】:

      • 我的存储过程和你的sql语句一样。你的版本也做同样的事情。我点击它...返回一个网页并返回主页,Delete 链接按钮不见了。不删除实际记录。
      • 你能在你的参数上设置一个断点吗?为删除添加行。我想知道这是否真的得到了正确的参数值?
      • 我在上面编辑了我的答案,试试看它是否真的删除了记录
      • 仍然删除链接按钮。单击时,我尝试为每个单独的按钮创建一个Sub LinkButton1_Click(sender As Object, e As EventArgs),但仍然会发生同样的问题。
      • 但是你确定你是否真的得到了参数的“val_id”值?我想如果它没有得到一个值,这可能会给你一个错误
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 2021-02-04
      • 2016-03-19
      • 1970-01-01
      • 2013-05-16
      相关资源
      最近更新 更多