【问题标题】:Checkbox update in GridViewGridView 中的复选框更新
【发布时间】:2013-01-21 14:06:04
【问题描述】:

当人们使用GridView 在我的网站上搜索时,我希望有一个checkbox 列,他们单击该列会将名为STATUS 的列中的值更改为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

【问题讨论】:

    标签: asp.net vb.net checkbox


    【解决方案1】:

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

    但是当你将字符串绑定到布尔值时,状态有字符串值,它会抛出错误。

    【讨论】:

      【解决方案2】:

      和前面提到的 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
      

      【讨论】:

      • 谢谢阿比德,但现在我和以前一样困惑。 Visual Studio 不喜欢 e.RowIndex,但我也不确定该代码是如何执行任何操作的,因为我没有看到它触发任何 sql 更新命令将 SQL 值更新为 U。拉我的头发,但我感谢您的帮助。
      • 我已经更新了我的答案。我的错误。它应该是 e.Row 代替 e.RowIndex。代码实际上做了一些事情。它清除了您在字符串到布尔转换时遇到的错误。您仍然需要在网格行更新事件中挂钩您的更新逻辑,您已经完成了。如果这仍然不清楚,请告诉我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多