如何选择datalist中的每个单选按钮值?

时间:2017-02-21 17:35:34

标签: c# asp.net

下面的datalist代表一组问题和答案。

当用户点击“最终提交”按钮时,如何将用户选择的正确答案单选按钮值插入数据库?

<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1" BackColor="White"  BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" onselectedindexchanged="rd_CS_CheckedChanged">
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <AlternatingItemStyle BackColor="#F7F7F7" />
            <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <ItemTemplate>
               Q:
            <asp:Label ID="QLabel" runat="server" Text='<%# Eval("Q") %>' />
            <br />
                A:
                <asp:RadioButton ID="rd_CS" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("A") %>'></asp:RadioButton>
                <br />
                B:
                <asp:RadioButton ID="rd_CS2" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("B") %>'></asp:RadioButton>
                <br />
                 C:
                <asp:RadioButton ID="rd_CS3" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("C") %>'></asp:RadioButton>
                <br />
                 D:
                <asp:RadioButton ID="rd_CS4" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("D") %>'></asp:RadioButton>

                <p style="color: #FF3300">
            <asp:Label ID="Correct_AnswerLabel" runat="server" 
                Text='<%# Eval("Correct_Answer") %>' Visible="False" /></p>

    </ItemTemplate>
    <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
</asp:DataList>

    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/App_Data/Quize.mdb" 
        SelectCommand="SELECT [Q],[A], [B], [C], [D], [Correct Answer] AS Correct_Answer FROM [QuizData]">
    </asp:AccessDataSource>


    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

2 个答案:

答案 0 :(得分:1)

在Button1_中点击您的代码隐藏,您可以改进此方法:

protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (DataListItem item in DataList1.Items)
        {
            RadioButton rd_CS = (RadioButton)item.FindControl("rd_CS");
            RadioButton rd_CS2 = (RadioButton)item.FindControl("rd_CS2");
            RadioButton rd_CS3 = (RadioButton)item.FindControl("rd_CS3");
            RadioButton rd_CS4 = (RadioButton)item.FindControl("rd_CS4");

            if (rd_CS.Checked)
            {
                Insert(rd_CS.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton
            }
            if (rd_CS2.Checked)
            {
                Insert(rd_CS2.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton
            }
            if (rd_CS3.Checked)
            {
                Insert(rd_CS3.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton
            }
            if (rd_CS4.Checked)
            {
                Insert(rd_CS4.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton
            }
        }
    }

Insert函数定义与:

相同
private void Insert(string value)
    {
        //Your code here to save on database
        OleDbConnection connection = new OleDbCommand("Your sql connection String");
        OleDbCommand command = new OleDbCommand("Your sql insert query");
        command.Connection = connection;

        //Parámeters of command
        OleDbParameter param = new OleDbParameter("Parameter name and next your type", OleDbType.VarChar);
        param.Value = value;

        command.Parameters.Add(param);

        command.Connection.Open();

        command.ExecuteNonQuery();

        command.Connection.Close();

        //Your value is saved now
    }

这是您可以在所要求的datalist上保存所有已检查的radiobutton的方法

答案 1 :(得分:0)

这是我的代码隐藏。当我点击按钮时,重复选择的单选按钮值将保存在我的数据库中。这是我的问题。我的数据库中只需要唯一的值

protected void rd_CS_CheckedChanged(object sender,EventArgs e)     {         string myRadioText = String.Empty;

    foreach (DataListItem item in DataList1.Items)
    {

        RadioButton rd_CS = (RadioButton)item.FindControl("rd_CS");
        RadioButton rd_CS2 = (RadioButton)item.FindControl("rd_CS2");
        RadioButton rd_CS3 = (RadioButton)item.FindControl("rd_CS3");
        RadioButton rd_CS4 = (RadioButton)item.FindControl("rd_CS4");


        if (rd_CS != null && rd_CS.Checked)
        {
            myRadioText = rd_CS.Text;
            Label1.Text = myRadioText.ToString();

        }
        else if (rd_CS2 != null && rd_CS2.Checked)
        {
            myRadioText = rd_CS2.Text;
            Label1.Text = myRadioText.ToString();
        }
        else if (rd_CS3 != null && rd_CS3.Checked)
        {
            myRadioText = rd_CS3.Text;
            Label1.Text = myRadioText.ToString();
        }
        else if (rd_CS4 != null && rd_CS4.Checked)
        {
            myRadioText = rd_CS4.Text;
            Label1.Text = myRadioText.ToString();
        }
        string str = Server.MapPath("~/App_Data/Quize.mdb");
        OleDbConnection ole = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str + ";Persist Security Info=True");
        ole.Open();
        OleDbCommand cmd = new OleDbCommand("insert into Userdata  values ('" + Label1.Text.Trim().Replace("'", "''") + "','" + Label1.Text.Trim().Replace("'", "''") + "',)", ole);
        cmd.ExecuteNonQuery();

}