0

当人们使用 a 在我的网站上搜索时,我GridView希望有一个checkbox他们单击的列,该列将更改名为STATUSto的列中的值U。我正在寻找有效的代码,所以我希望你们能提供帮助。我是一个完整的菜鸟,所以如果你知道答案,请描述一下。

按钮代码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
4

2 回答 2

0

就像前面提到的 Kiran 一样,status 有一个字符串值,因此您需要在行数据绑定事件中进行一些处理。

标记

<asp:TemplateField HeaderText="Successful Contact?">
            <EditItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server"   AutoPostBack="true" />
            </EditItemTemplate>
            <ItemTemplate> 
            <asp:CheckBox ID="CheckBox1" runat="server" 
               Enabled="False" /></ItemTemplate>
        </asp:TemplateField>   

背后的代码

   Protected Sub GridView1_RowDataBound(sender As Object, e As   System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

  If e.Row.RowType = DataControlRowType.DataRow Then
  'Evaluate the state of the check box, true when status =U , and false otherwise
  Dim itemstatus As Boolean=If(DataBinder.Eval(e.Row.DataItem, "Status"), True, False)
  Dim CheckBoxItemTemp As CheckBox=CType(e.Row.FindControl("CheckBox1"), CheckBox)
  CheckBoxItemTemp.Checked=itemstatus
  End if


  If e.Row.RowState & DataControlRowState.Edit Then
 'same as above but  now for edit item template
  Dim editstatus As Boolean=If(DataBinder.Eval(e.Row.DataItem, "Status"), True, False)
  Dim CheckBoxEditTemp As CheckBox=CType(e.Row.FindControl("CheckBox1"), CheckBox)
  CheckBoxEditTemp.Checked=editstatus
  End if

 End Sub
于 2013-01-20T19:38:30.767 回答
0

您正在为复选框选中属性绑定状态值,该属性采用布尔值。

但是状态具有字符串值,因为您将字符串绑定到布尔值,它会引发错误。

于 2013-01-20T16:08:38.233 回答