2012-08-13 62 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(); 
    } 
} 
相關問題