I read and implemented the sample form that teaches how to restore form variables after a postback; pretty cool. However, my form includes radio buttons and I can't figure out how to restore the checked radio button after postback. Any suggestions would
be greatly appreciated.
When the page loads, and after a POST the "C" radio button is checked. I can check "A" or "B" button, but when the Submit button is clicked, Request("LN") returns the "A" or "B" within a code block, and the "C" button is checked. Where did my translation
go wrong?
Thanks a lot; that did the trick! I understood how you were approaching the problem; I just forgot what language I was in. One last wrinkle remains with this issue. How do I initialize the radio buttons so that one button is selected when the page loads?
GrandpaB
Member
389 Points
367 Posts
How to Restore Radio Button Values
Nov 19, 2010 01:58 PM|LINK
Good morning WebMatrix Adventurers,
I read and implemented the sample form that teaches how to restore form variables after a postback; pretty cool. However, my form includes radio buttons and I can't figure out how to restore the checked radio button after postback. Any suggestions would be greatly appreciated.
</div>pranavkm
Participant
796 Points
106 Posts
Re: How to Restore Radio Button Values
Nov 19, 2010 04:57 PM|LINK
There are two ways to go about this:
* RadioButton takes a parameter named "isChecked" that determines if a button's checked.
@Html.RadioButton("flavor", "vanilla", isChecked: Request.Form["flavor"] == "vanilla") @Html.RadioButton("flavor", "chocolate", isChecked: Request.Form["flavor"] == "chocolate")* Alternatively, you could let the ModelState to do this comparison for you.
@{ var scaryThings = new[] { "Spiders", "Zombies", "Angry gnomes", "Vampires", "Sparkly Vampires" }; if (IsPost) { ModelState.SetModelValue("scaryThing", Request["scaryThing"]); } } <form method="post" action=""> The thing that scares me the most: @foreach(var thing in scaryThings) { <label>@Html.RadioButton("scaryThing", thing)@thing </label> } <br /> <input type="submit" value="Submit" /> </form>Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: How to Restore Radio Button Values
Nov 19, 2010 05:38 PM|LINK
Alternatively, you could do this:
<form> <input type="radio" name="radio1" value="1"@(Request["radio1"] == "1" ? " checked=\"checked\"" : "") /> <input type="radio" name="radio1" value="2"@(Request["radio1"] == "2" ? " checked=\"checked\"" : "") /> <input type="submit" /> </form>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GrandpaB
Member
389 Points
367 Posts
Re: How to Restore Radio Button Values
Nov 19, 2010 05:57 PM|LINK
Pranavkm,
Thanks for your suggestion, since I',m working in a vbhtml page, I tried the following translation:
<input type="radio" name="LN" value="A" checked=Request("LN")="A">A</input>
<input type="radio" name="LN" value="B" checked=Request("LN")="B">B</input>
<input type="radio" name="LN" value="C" checked=Request("LN")="C">C</input>
When the page loads, and after a POST the "C" radio button is checked. I can check "A" or "B" button, but when the Submit button is clicked, Request("LN") returns the "A" or "B" within a code block, and the "C" button is checked. Where did my translation go wrong?
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: How to Restore Radio Button Values
Nov 19, 2010 07:08 PM|LINK
<form> <input type="radio" name="LN" value="A" @IIf(Request("LN") = "A", " checked=""checked""", "") />A <input type="radio" name="LN" value="B" @IIf(Request("LN") = "B", " checked=""checked""", "") />B <input type="radio" name="LN" value="C" @IIf(Request("LN") = "C", " checked=""checked""", "") />C <input type="submit" /> </form>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GrandpaB
Member
389 Points
367 Posts
Re: How to Restore Radio Button Values
Nov 19, 2010 07:22 PM|LINK
Mike,
Thanks for your input. Again I tried several translations of your solution and I have not yet found the magic bullet! Here is my latest attempt:
<input type="radio" name="LN" value="A" @code Request("LN")="A" ? " checked=true" : "" end code >A</input>
This version gave me a Compile Error Message: BC30526: Property 'Item' is 'ReadOnly'.
Another version:
<input type="radio" name="LN" value="A" @(Request("LN")="A" ? " checked=true" : "") >A</input>
Produced this response: Compiler Error Message: BC36637: The '?' character cannot be used here.
Any other suggestions will be greatly appreciated.
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: How to Restore Radio Button Values
Nov 19, 2010 07:36 PM|LINK
Copy my most recent reply, which has been written in VB , and tested in a vbhtml file on my machine.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GrandpaB
Member
389 Points
367 Posts
Re: How to Restore Radio Button Values
Nov 20, 2010 12:33 AM|LINK
Mike,
Thanks a lot; that did the trick! I understood how you were approaching the problem; I just forgot what language I was in. One last wrinkle remains with this issue. How do I initialize the radio buttons so that one button is selected when the page loads?
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: How to Restore Radio Button Values
Nov 20, 2010 05:46 AM|LINK
<form> <input type="radio" name="LN" value="A" @IIf((Request("LN") = "A" Or Not IsPost()), " checked=""checked""", "") />A <input type="radio" name="LN" value="B" @IIf(Request("LN") = "B", " checked=""checked""", "") />B <input type="radio" name="LN" value="C" @IIf(Request("LN") = "C", " checked=""checked""", "") />C <input type="submit" /> </form>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GrandpaB
Member
389 Points
367 Posts
Re: How to Restore Radio Button Values
Nov 20, 2010 12:27 PM|LINK
Mike,
Thanks again. Here is what I concocted:
<input type="radio" name="LN" value="A" @IIf(IsPost,IIf(Request("LN") = "A", " checked=""checked""", "")," checked=""checked""") />A
If you don't mind, I'll use your solution it's a lot cleaner!