简体   繁体   中英

How to check if the checbox is checked in asp.net

I need to check if the check-boxes are checked and from there save the rows of data that the user has selected into my database but I don't know how to even start.

That includes how to save the data to my database.

FYI: The check-boxes are inside of a 'GridView' and then inside of a 'ItemTemplate'.

If you're unsure about what I'm trying to achieve just ask and I'll try to make it clearer. Here's the code I have so far:

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

Here's the code behind:

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>

Thanks in advance!

So you want all checked CheckBoxes ? You have to use GridViewRow.FindControl to get their references. You can use this little query in your save-function:

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);
    // ...
}

Whoops, here is the VB.NET version:

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

Did you try to use the Checked attribute on the Checkbox object?

If QuestionSelector.Checked Then 
    DoSomething()
End If

you can try this:

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

and also this:

In C#:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM