Reputation: 2305
I have a repeater control, and inside it I have two radio buttons selecting Sex (male, female). When I save the page, I save the selected value, either "M" or "F", into the data. When I load the page, I want the radio button to be selected based on what's saved.
So, in this case, my repeater has (Container.DataItem).Sex, which equals 'M' or 'F'. How can I use this data in my radiobuttons to select the appropriate value?
This is the kind of function I'd like to use (but it doesn't exist):
<asp:RadioButtonList runat="server" SelectedValue='<%# ((Dependent)(Container.DataItem)).Sex %>' />
Please note that I am unable to manipulate the radio buttons in the codebehind because I do not have access to the individual repeater items.
Upvotes: 0
Views: 2719
Reputation: 4686
Looking at this answer here Setting selected value on a dropdownlist from codebehind in a repeater in a formview you should be able to do something like:
<asp:RadioButtonList runat="server" SelectedValue='<%# Eval("Sex") %>' />
Upvotes: 1
Reputation: 63966
Please note that I am unable to manipulate the radio buttons in the codebehind because I do not have access to the individual repeater items.
That's what ItemDataBound
is for...
<asp:Repeater id="Repeater1" OnItemDataBound="repeaterDatabound" runat="server">
And on code behind:
protected void repeaterDatabound(object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
(e.Item.FindControl("Repeater1") as RadioButtonList).SelectedValue=((Dependent)e.Item.DataItem).Sex.ToString();
}
}
Upvotes: 0
Reputation: 13655
What you need to do is use 2 RadioButton
control (1 for Female, 1 for Male).
Then specify the GroupName
in order to unselect one when the other is selected.
Let's say GroupName="Sex"
.
Then specify when each control should be checked according to your DataItem
:
<asp:RadioButton ID="radioMale" runat="server" GroupName="Sex" Checked="<%# ((Dependent)(Container.DataItem)).Sex == 'M' %>" />
<asp:RadioButton ID="radioFemale" runat="server" GroupName="Sex" Checked="<%# ((Dependent)(Container.DataItem)).Sex == 'F' %>" />
Upvotes: 1