当用户在我的网站上使用GridView
进行搜索时,我希望有一个checkbox
列,他们会点击这一列,将列STATUS
中的值更改为U
。我有一段时间寻找可行的代码,所以我希望你们可以提供帮助。我是一个完整的NOOB,如果你知道答案,请说明性。GridView中的复选框更新
Checkbox
按钮的代码 - 当前我搜索时它返回说它不能将字符串转换为boolean
。
<asp:TemplateField HeaderText="Successful Contact?">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" checked='<%#Bind("status")%>' AutoPostBack="true" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" checked='<%#Bind("status")%>'
Enabled="False" /></ItemTemplate>
</asp:TemplateField>
这是VB代码
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
For Each GridViewRow In GridView1.Rows
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim value As Boolean
value = DirectCast(GridView1.Rows(e.RowIndex).Cells(0).FindControl("CheckBox1"), CheckBox).Checked
Dim connectionString As String = ConfigurationManager.ConnectionStrings("Data Source=server;Initial Catalog=i3FCC01;Integrated Security=True;").ConnectionString
Dim com_sql As New SqlClient.SqlCommand
Dim insertSql As String
insertSql = "Update CallQueueFCC Set Status='U' where Id = @id"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@status", value)
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
Next
GridView1.EditIndex = -1
DataBind()
End Sub
谢谢里面,但现在我和以前一样混乱。 Visual Studio不喜欢e.RowIndex,但我也不确定该代码如何执行任何操作,因为我没有看到它触发任何sql update命令将SQL值更新为U.将我的头发拉出来,但是我感谢你的帮助。 – user1993989
我已经更新了我的答案。我的不好。它应该是e.Row来代替e.RowIndex。代码实际上做了一些事情。它清除了你正在对字符串进行布尔转换的错误。您仍然必须挂钩你在网格行更新事件中的更新逻辑,你已经完成了。让我知道如果这仍然不清楚。 –