我的gridview每行内有2个Linkbuttons。
我想知道如何使用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"和#34; DeleteRow"到HTML Linkbutton并执行以下操作:
If LinkButton1.CommandName = "UpdateRow"
和
ElseIf LinkButton2.CommandName = "DeleteRow" Then
然而,删除一个只是删除LinkButton而不是数据库记录,这很奇怪?!不知道为什么。
我还看到,只有点击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
答案 0 :(得分:1)
你应该把strFilter = row.Cells(1).Text行放在if语句上面(如果LinkButton1.Text =“Update”那么)。
答案 1 :(得分: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>
答案 2 :(得分:0)
我对使用/调用存储过程并不熟悉,但如果不是太麻烦,请尝试在commandtext属性中输入删除查询,例如
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
由于我从未调用过存储过程,我只是猜测它与你为删除它的方式有关