2014-01-09 73 views
0

我需要檢查是否選中了複選框,並從那裏保存用戶選擇的數據行到我的數據庫但我不知道如何甚至開始。如何檢查是否在asp.net中檢查了checbox

這包括如何將數據保存到我的數據庫。

僅供參考:複選框位於「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> 

提前感謝!

回答

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 
+0

謝謝!工作得很好,感謝幫助! :) – Callum

+0

+1使用LINQ找到'allCheckedRows'! –

0

您是否嘗試使用Checkbox對象上的Checked屬性?

If QuestionSelector.Checked Then 
    DoSomething() 
End If 
+0

這是一個回答或評論? – zkanoca

+0

然後,我如何從選中的文本框中獲取行以存儲在我的數據庫中? – Callum

+0

@Callum那麼,在這種情況下,你應該開始閱讀一些基本的教程,也許MSDN文檔。谷歌搜索「asp.net複選框類參考」也可以幫助。 – nestedloop

0

你可以試試這個:

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

而且這樣的:

在C#:

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

我認爲他的代碼是在VB中,而不是C#。雖然不重要... – nestedloop

相關問題