sharad
sharad

Reputation: 13

How to get selected each radio button value in datalist?

In below datalist represents set of question's and answer.

How to insert the user selected right answer radio button value into database when the user clicks on Final submit button?

<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" />

Upvotes: 1

Views: 920

Answers (2)

sharad
sharad

Reputation: 13

This is my codebehind .When i click on button then Repeated selected radio button values are save in my database . this is my problem. I need only unique value in my database

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();

}

Upvotes: 0

Magnus Alexander
Magnus Alexander

Reputation: 96

In Button1_Click on your codebehind you can improve this method:

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
            }
        }
    }

And the Insert function definition will be same as:

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
    }

This is how you can save all checked radiobutton on datalist you asked

Upvotes: 1

Related Questions