我如何使用asp.net从datalist中的复选框插入数据库

时间:2014-02-26 04:59:27

标签: asp.net sql-server-2012

<asp:DataList ID="DataList1" runat="server" BackColor="#CCCCCC" DataSourceID="SqlDataSource1" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" ForeColor="Black" style="margin-right: 260px" Width="720px">
                    <ItemTemplate>
                        &nbsp;<asp:Label ID="questionLabel" runat="server" Text='<%# Eval("question") %>' />
                        <br />
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                        A:
                        <asp:Label ID="answer1Label" runat="server" Text='<%# Eval("answer1") %>' />
                        <br />
                        <asp:CheckBox ID="CheckBox2" runat="server" />
                        B:
                        <asp:Label ID="answer2Label" runat="server" Text='<%# Eval("answer2") %>' />
                        <br />
                        <asp:CheckBox ID="CheckBox3" runat="server" />
                        C:
                        <asp:Label ID="answer3Label" runat="server" Text='<%# Eval("answer3") %>' />
                        <br />
                        <asp:CheckBox ID="CheckBox4" runat="server"  />
                        D:
                        <asp:Label ID="answer4Label" runat="server" Text='<%# Eval("answer4") %>' />
                        <br />
                        <br />

例如:

我希望在以下情况下将answer1labl插入数据库中的student_answer列:checkbox1.checked == true

1 个答案:

答案 0 :(得分:0)

在按钮单击中,您可以这样做,

 protected void btnSave_Click(object sender, EventArgs e)
    {
        foreach (DataListItem item in DataList1.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("CheckBox1");

            if (cb.Checked)
            {
                string text = ((Label)item.FindControl("answer1Label")).Text;

                //save code
            }
        }
    }