2012-08-13 64 views
0

我有一个中继器控件,里面有两个选择Sex(男,女)的单选按钮。 当我保存页面时,我将所选值(“M”或“F”)保存到数据中。当我加载页面时,我希望根据保存的内容选择单选按钮。从Repeater控件中的数据中选择RadioButtonList中的值

因此,在这种情况下,我的转发器具有(Container.DataItem).Sex,它等于'M'或'F'。我如何在我的单选按钮中使用这些数据来选择适当的值?

这是我想使用(但它不存在)的那种功能:

<asp:RadioButtonList runat="server" SelectedValue='<%# ((Dependent)(Container.DataItem)).Sex %>' /> 

请注意,我无法操纵单选按钮的代码隐藏,因为我不可以访问单个中继器项目。

回答

1

你需要做的是使用2 RadioButton控制(1为女性,1为男性)。

然后指定GroupName以取消选择另一个时的选择。 比方说GroupName="Sex"

然后指定根据您的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' %>" /> 
+0

谢谢,这是解决方案c失去了我的目标。 – proseidon 2012-08-13 16:29:26

0

请注意,我不能操纵在 代码隐藏单选按钮,因为我没有访问各个中继器 项目。

这就是ItemDataBound是...

<asp:Repeater id="Repeater1" OnItemDataBound="repeaterDatabound" runat="server"> 

而且在后面的代码:

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(); 
    } 
} 
相关问题