GridView中的复选框更新

时间:2013-01-20 14:47:44

标签: asp.net vb.net checkbox

当人们使用GridView在我的网站上搜索时,我希望他们点击一个checkbox列,这会将名为STATUS的列中的值更改为{{1} }}。我有一段时间找到有效的代码,所以我希望你们能提供帮助。我是一个完整的NOOB,如果你知道答案,请描述一下。

U按钮的代码 - 目前我在搜索时会回来说它无法将字符串转换为Checkbox

boolean

这是vb代码

                <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>       

2 个答案:

答案 0 :(得分:0)

您正在使用复选框选中属性的状态值,该属性采用布尔值。

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

答案 1 :(得分: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