VB.net - 使Gridview复选框更新数据库中的boolean字段

时间:2010-11-21 13:10:34

标签: sql vb.net gridview checkbox

关于此问题有很多问题,但是我无法使用其中任何一个的答案来解决我的问题(经过多次尝试......)

我正在vb.net中创建一个asp.net Web应用程序。我的页面上有一个SqlDataSource和一个GridView:

<asp:SqlDataSource ID="msgUnread" runat="server" 
 ConnectionString="<%$ ConnectionStrings:edinsec %>" 

 SelectCommand="SELECT [msgdate], [email], [name], [message], [readit] FROM [messages]" 
 UpdateCommand="UPDATE messages SET readit = 'True' WHERE (msgid = @msgid)">
       <UpdateParameters>
     <asp:Parameter Name="msgid" />
 </UpdateParameters>
   </asp:SqlDataSource>


   <asp:GridView ID="unreadMessages" runat="server" AutoGenerateColumns="False" 
 BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
 CellPadding="3" DataSourceID="msgUnread">
 <RowStyle ForeColor="#000066" />
 <Columns>
     <asp:BoundField DataField="msgdate" HeaderText="Date &amp; time" 
         SortExpression="msgdate" />
     <asp:BoundField DataField="email" HeaderText="Email" 
         SortExpression="email" />
     <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
                 <asp:TemplateField HeaderText="Mark as read" SortExpression="readit">
         <%--<EditItemTemplate>
             <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("readit") %>'  OnCheckedChange="CheckBox1_CheckedChanged"  />
         </EditItemTemplate>--%>
         <ItemTemplate>
             <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("readit") %>' OnCheckChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>
         </ItemTemplate>
     </asp:TemplateField>
 </Columns>
 <FooterStyle BackColor="White" ForeColor="#000066" />
 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
   </asp:GridView>

正确填充GridView,正确显示复选框的值('readit'字段是位字段,即布尔值)。我正在尝试让脚本在单击复选框时更新数据库中的布尔值。目前虽然我甚至无法让脚本对点击作出反应(甚至不是MsgBox)。

这是我的CodeBehind:

Public Partial Class enqur
Inherits System.Web.UI.Page
WithEvents CheckBox1 As CheckBox


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

End Sub

Public Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    MsgBox("test")
End Sub

End Class

正如您所看到的,我正在玩WithEvents,但这似乎没有帮助。在上面的代码中,我试图得到的是对点击一个复选框的某种反应 - 但没有任何反应(也没有错误)。

我很难过。有人可以帮忙吗?非常感谢:)

3 个答案:

答案 0 :(得分:1)

经过深刻的讨论后,我在aspx文件中找到了这段代码:

<asp:TemplateField HeaderText="readit" SortExpression="readit">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_CheckedChanged" Checked='<%# Bind("readit") %>' 
            Enabled="true" />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_CheckedChanged" Checked='<%# Bind("readit") %>' />
    </EditItemTemplate>
      </asp:TemplateField>

...这在CodeBehind中:

Public Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 'Handles checkbox.CheckedChanged
    Dim box As CheckBox = DirectCast(sender, CheckBox)



    If box.Checked = True Then
  MsgBox("checked!")
    Else
  MsgBox("unchecked!")
    End If



End Sub

...单击每个复选框时会正常触发。现在让他们做一些有用的事情......

答案 1 :(得分:0)

更改

Public Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

Public Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox1.CheckedChange

基本上你忘了Handles CheckBox1.CheckedChange了。

答案 2 :(得分:0)

错误:当我在代码隐藏部分下添加“Handles CheckBox1.CheckedChanged”时,CheckBox1带有下划线并出现以下错误:“Handles子句需要在包含类型或其基类型中定义的WithEvents变量”。我怎么摆脱它?我刚刚删除了该行并在没有它的情况下运行了代码。

要更改由Gridview复选框表示的DoNotMail布尔值,并在数据库中自动更新,如果复选框从0(False,Will Mail)检查为1(True,Will not Mail),这里是我使用的代码。

对于我添加的default.aspx.vb代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    lastname.Focus() //sets the focus on the last name text box
    If Page.IsPostBack Then
        Response.Write("The DoNotMail value has been changed in the database for the selected field")
    End If

End Sub

Public Sub checkbox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim box As CheckBox = DirectCast(sender, CheckBox)
    If box.Checked = True Then
        donotmail.SelectedValue = 1
    Else
        donotmail.SelectedValue = 0
    End If


End Sub

对于default.aspx页面,我添加了:

                 <asp:TemplateField HeaderText="DoNotMail" SortExpression="DoNotMail">     
                 <ItemTemplate>         
                 <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox1_CheckedChanged" Checked='<%# Bind("DoNotMail") %>'
                               Enabled="true" />     
                 </ItemTemplate>     
                 <EditItemTemplate>         
                 <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox1_CheckedChanged" Checked='<%# Bind("DoNotMail") %>' />     
                 </EditItemTemplate>       
                 </asp:TemplateField>