简体   繁体   中英

Saving radiobutton value to Database

Im trying to take some selected value to use it in an insert method I have to the database (this is working with given vaalues), now I want to give to take the value from a form. Everything's fine but the radio buttons... I do not know how to take the value :P.

Here it is:

    <tr>
        <td>Oral/Written Communications:</td>
        <td>
        <asp:RadioButton ID="form1_1" GroupName="form1" runat="server" Text="1" />
        <asp:RadioButton ID="form1_2" GroupName="form1" runat="server" Text="2" />
        <asp:RadioButton ID="form1_3" GroupName="form1" runat="server" Text="3" />
        <asp:RadioButton ID="form1_4" GroupName="form1" runat="server" Text="4" />
        <asp:RadioButton ID="form1_5" GroupName="form1" runat="server" Text="5" />
            <br /></td>
    </tr>

And in the backend...

int selected = form1.value

This is not working... Not even recognizing the form1. etc.... How can I handle this?

EB.

When you work with a RadioButton control, each one of them is a separate control and you need to check them like this:

string selectedValue;
if (form1_1.Checked)
    selectedValue = form1_1.Text;
else if (form1_2.Checked)
    selectedValue = form1_2.Text;
...

You can also go after the Request.Form collection, but I don't find it to be easy to use with RadioButton controls.

Maybe the easiest thing, if you can change the code slightly, is to use a RadioButtonList control:

<asp:RadioButtonList ID="form1list" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem Text="1" />
    <asp:ListItem Text="2" />
    <asp:ListItem Text="3" />
    <asp:ListItem Text="4" />
    <asp:ListItem Text="5" />
</asp:RadioButtonList>

Then you can simply refer to the control as a whole:

form1list.SelectedValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM