如何检查是否在asp.net中检查了checbox

时间:2014-01-09 11:56:23

标签: asp.net database vb.net gridview checkbox

我需要检查是否选中了复选框,然后从那里将用户选择的数据行保存到我的数据库中,但我不知道如何启动。

包括如何将数据保存到我的数据库。

仅供参考:复选框位于“GridView”内,然后位于“ItemTemplate”内。

如果你不确定我想要实现什么,那就问一下,我会尽量让它更清楚。 这是我到目前为止的代码:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="TestCreation.aspx.vb" Inherits="SpellingBee.testcreation1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h1>Compile Your Tests</h1>

<asp:GridView ID="CreateTest" runat="server" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="QuestionID" DataSourceID="SqlDataSource1" ForeColor="#333333" 
        GridLines="None">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="QuestionSelector" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="QuestionID" HeaderText="QuestionID" 
            InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
        <asp:BoundField DataField="Answer" HeaderText="Answer" 
            SortExpression="Answer" />
        <asp:BoundField DataField="Question" HeaderText="Question" 
            SortExpression="Question" />
        <asp:BoundField DataField="SubjectID" HeaderText="SubjectID" 
            SortExpression="SubjectID" />
        <asp:TemplateField></asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="SELECT [QuestionID], [Answer], [Question], [SubjectID] FROM [Question]">
    </asp:SqlDataSource>

    <asp:Button ID="QuestionCompiler" runat="server" Text="Compile Selected Questions" />

<h1>Preview Previous Tests</h1>

</asp:Content>

这是背后的代码:

Public Class testcreation1
    Inherits System.Web.UI.Page

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

            If Session.Item("User Type") <> "Teacher" Then
                Response.Redirect("/")
            End If

        End Sub

        Protected Sub QuestionCompiler_Click(sender As Object, e As EventArgs) Handles QuestionCompiler.Click



        End Sub
    End Class
    <h1>Preview Previous Tests</h1>

    </asp:Content>

提前致谢!

3 个答案:

答案 0 :(得分:2)

所以你想要检查所有CheckBoxes吗?您必须使用GridViewRow.FindControl来获取他们的参考。您可以在保存功能中使用这个小查询:

IEnumerable<GridViewRow> allCheckedRows = CreateTest.Rows.Cast<GridViewRow>()
    .Where(row => ((CheckBox)row.FindControl("QuestionSelector")).Checked);

foreach(GridViewRow checkedRow in allCheckedRows)
{
    // implement the save function for this row
    int questionID = int.Parse(checkedRow.Cells[1].Text);
    // ...
}

哎呀,这是VB.NET版本:

Dim allCheckedRows = From row In CreateTest.Rows.Cast(Of GridViewRow)()
                     Where DirectCast(row.FindControl("QuestionSelector"), CheckBox).Checked

For Each checkedRow As GridViewRow In allCheckedRows
    ' implement the save function for this row '
    Dim questionID As Int32 = Int32.Parse(checkedRow.Cells(1).Text)
    ' ... '
Next

答案 1 :(得分:0)

您是否尝试使用Checked对象上的Checkbox属性?

If QuestionSelector.Checked Then 
    DoSomething()
End If

答案 2 :(得分:0)

你可以试试这个:

if (QuestionSelector.Checked == true) {
   //Do Whatever
} 

还有这个:

在C#中:

CheckBox chbx = GridView1.HeaderRow.FindControl("QuestionSelector") as CheckBox;
   if (chbx != null && chbx.Checked){
      //Do Whatever
   }