locked
Remembering Radio Selection RRS feed

  • Question

  • User-701766627 posted

    Hi,

    There is a similar thread to this one, which shows something close to what I'm trying to accomplish:

    https://forums.asp.net/t/2037814.aspx?Remembering+Dropdown+Box+selection+in+WebMatrix+post+method+

    I want the radio button to remain checked after the form is posted, and the page is loaded again. Here is what I've got:

    <form method="post">
        <table>
            <tr>
                <td><label>Day in field:</label></td>
                <td><input type="radio" name="Field_Office" value="A @Request.Form["Field_Office"]" id="DayField"/></td>
            </tr>
            <tr>
                <td><label>Day in office:</label></td>
                <td><input type="radio" name="Field_Office" value="B @Request.Form["Field_Office"]" id="DayOffice"/></td>
            </tr>
        </table>
    </form>

    Thanks in advance.

    Saturday, January 14, 2017 8:11 PM

Answers

  • User-2057865890 posted

    Hi Oscar,

    <form method="post">
        <table>
            <tr>
                <td><label>Day in field:</label></td>
                <td><input type="radio" name="Field_Office" value="A" 
    @(Request.Form["Field_Office"] == "A" ? "checked" : String.Empty) id="DayField" /></td> </tr> <tr> <td><label>Day in office:</label></td> <td><input type="radio" name="Field_Office" value="B"
    @(Request.Form["Field_Office"] == "B" ? "checked" : String.Empty) id="DayOffice" /></td> </tr> </table> <input type="submit" value="submit" /> </form>

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2017 5:26 AM

All replies

  • User2117486576 posted

    Basically you need to get the boolean "selected" attribute on the radio button you want checked.  The best way is to set a different value for each button.  Then when you analyze the posted form you can tell which button was set when the form was posted.

    <form method="post">
        <table>
            <tr>
                <td><label>Day in field:</label></td>
                <td><input type="radio" name="Field_Office" value="DayField" @(Request.Form["Field_Office"] == "DayField" ? "selected" : String.Empty) id="DayField"/></td>
            </tr>
            <tr>
                <td><label>Day in office:</label></td>
                <td><input type="radio" name="Field_Office" value="DayOffice" @(Request.Form["Field_Office"] == "DayOffice" ? "selected" : String.Empty) id="DayOffice"/></td>
            </tr>
        </table>
    </form>

    Sunday, January 15, 2017 2:14 PM
  • User-701766627 posted

    Thanks for your help RichardY. I have exactly what you shared and it isn't working for me.

    Sunday, January 15, 2017 10:27 PM
  • User-2057865890 posted

    Hi Oscar,

    <form method="post">
        <table>
            <tr>
                <td><label>Day in field:</label></td>
                <td><input type="radio" name="Field_Office" value="A" 
    @(Request.Form["Field_Office"] == "A" ? "checked" : String.Empty) id="DayField" /></td> </tr> <tr> <td><label>Day in office:</label></td> <td><input type="radio" name="Field_Office" value="B"
    @(Request.Form["Field_Office"] == "B" ? "checked" : String.Empty) id="DayOffice" /></td> </tr> </table> <input type="submit" value="submit" /> </form>

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2017 5:26 AM