【问题标题】:How to check if the checbox is checked in asp.net如何检查复选框是否在asp.net中选中
【发布时间】:2014-01-09 20:29:25
【问题描述】:

我需要检查复选框是否被选中,然后将用户选择的数据行保存到我的数据库中,但我什至不知道如何开始。

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

仅供参考:复选框位于“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>

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    所以你想全部检查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
    

    【讨论】:

    • 谢谢!工作得很好,感谢帮助! :)
    【解决方案2】:

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

    If QuestionSelector.Checked Then 
        DoSomething()
    End If
    

    【讨论】:

    • 这是答案还是评论?
    • 我如何从选中的文本框中获取行以存储在我的数据库中?
    • @Callum 好吧,在这种情况下,您应该开始阅读一些基本教程,也许是 MSDN 文档。谷歌搜索“asp.net 复选框类参考”可能也会有所帮助。
    【解决方案3】:

    你可以试试这个:

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

    还有这个:

    在 C# 中:

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

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      相关资源
      最近更新 更多