使用CheckBox和Button

时间:2016-07-20 11:00:54

标签: asp.net vb.net visual-studio

我有一个带有复选框的gridview,因此管理员可以批准订单。他会获得一个订单列表以及他检查的订单列表,并在单击按钮时取消选中,它将更新数据库中的批准字段。

选中 - 将已批准字段中的值更新为 - 已批准

未选中 - 将已批准字段中的值更新为 - 已拒登

但是我遇到了一些困难:

我有一个带有复选框的网格:

<asp:GridView ID="GridViewdoc" runat="server" AutoGenerateColumns="False">
   <Columns>
  <asp:BoundField DataField="OrderID" HeaderText="Order Id" />
  <asp:BoundField DataField="DoctorId" HeaderText="Doctor Id" />
  <asp:BoundField DataField="Forename" HeaderText="Forename" />
  <asp:BoundField DataField="Surname" HeaderText="Surname" />
  <asp:BoundField DataField="MedicineId" HeaderText="Medicine Id" />
  <asp:BoundField DataField="MedicineName" HeaderText="Medicine Name" />
  <asp:BoundField DataField="pharmname" HeaderText="Pharmacy Name" />
  <asp:BoundField DataField="Dateordered" HeaderText="Date Ordered" />
    <asp:TemplateField> 
  <ItemTemplate> 
 <asp:CheckBox Text="Approve" ID="ApproveBox" runat="server" /> 
  </ItemTemplate> 
    </asp:TemplateField>
  </Columns>

它从我的数据库中的订单表中保存此select语句:

Create View docgridview 
As 

Select A.OrderID, A.DoctorId, B.Forename, B.Surname, A.MedicineId, C.Name as MedicineName, D.pharmname, A.Dateordered, Approved
From  order_pres  A
Left  Join Patient   B on (A.PatientId  = B.PatientId)
Left  Join Medicine  C on (A.MedicineId = C.MedicineId)
Left  Join pharmacy  D on (A.PharmacyId   = D.PharmacyId)
Left  Join Doctor    E on (A.DoctorId  = E.DoctorId)

并将其绑定到网格视图(选择docgridview)

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Not IsPostBack Then
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
        Dim cmd3string As String = " Select * From docgridview  WHERE DoctorId = " & Session("DoctorId")
        Dim dt As New System.Data.DataTable()
        Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd3string, conn)
        conn.Open()
        da.Fill(dt)
        conn.Close()



        GridViewdoc.DataSource = dt
        GridViewdoc.DataBind()
        HttpContext.Current.Session("Approved") = dt
    End If

,这会控制复选框和按钮

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


    If e.Row.RowType = DataControlRowType.DataRow Then

        CType(e.Row.FindControl("Approvebox"), CheckBox).Checked = True
    End If

End Sub
Protected Sub btnapprove_Click(sender As Object, e As System.EventArgs) Handles btnapprove.Click
    Dim dt As Data.DataTable = Session("Approved")
    For Each row As GridViewRow In GridViewdoc.Rows
        Dim cb As CheckBox = row.FindControl("ApproveBox")
        If cb.checked Then
            dt.Rows(row.RowIndex).Item(8) = "Approved"
        Else
            dt.Rows(row.RowIndex).Item(8) = "Disapproved"
        End If
    Next
    GridViewdoc.DataSource = dt
    GridViewdoc.DataBind()
End Sub

我遇到了错误

enter image description here

对于使用button.click检查的复选框 - “已批准”&#39;订单表中的列应更改为值已批准

用于使用button.click进行UNchecked的复选框 - &#39;已批准&#39;订单表中的列应更改为已拒登

这是将要更新的订单表(已批准设置为位,我不确定这是否正确):

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

[Order_pres].[Approved]是一个位字段,您正在尝试将其设置为字符串值。只需将其设置为cb.checked

所以不要这样:

 If cb.checked Then
      dt.Rows(row.RowIndex).Item(8) = "Approved"
 Else
      dt.Rows(row.RowIndex).Item(8) = "Disapproved"
 End If

这样做:

dt.Rows(row.RowIndex).Item(8) = cb.checked

答案 1 :(得分:0)

您的字段有点,这意味着它可以采用 True false 的值。对于您的情况,您有2个我认为合适的行动案例:

1-在数据库中将类型更改为nvarchar: 通过将类型更改为nvarchar,您可以自由设置值

2-将输入更改为true或false,然后使用VB.net将值从Boolean转换为Approved / Disapproved。

我建议采取第二项措施,因为它会保持数据库的不变。