2012-02-12 59 views
0

我想填充一個包含標籤和RadioButtonList在ASP.NET webform中的中繼器進行小測驗。無法填充ASP.NET中繼器模板中的RadioButtonList

這是我使用的是類:

public class Option 
{ 
    private string _body; 
    private bool? _isCorrect; 

    #region properties 
    public string Body 
    { 
     get { return _body; } 
    } 

    public bool? IsCorrect 
    { 
     get { return _isCorrect; } 
    } 

    #endregion 

    #region constructors 

    public Option(string body) 
    { 
     _body = body; 
     _isCorrect = null; 
    } 

    public Option(string body, bool isCorrect) 
    { 
     _body = body; 
     _isCorrect = isCorrect; 
    } 
    #endregion 

    #region methods 

    public override string ToString() 
    { 
     return _body; 
    } 

    #endregion 

} 

和:

public class Question 
{ 
    private string _body; 
    private Option[] _optionsArray; 
    private List<Option> _optionsList; 

    #region properties 

    public string Body 
    { 
     get { return _body; } 
    } 

    public Option[] Options 
    { 
     get { return _optionsArray; } 
    } 

    public List<Option> OptionsList 
    { 
     get { return _optionsList; } 

    } 
    #endregion 


    #region constructors 

    public Question(string body, Option[] options) 
    { 
     _body = body; 

     _optionsArray = options; 

     _optionsList = new List<Option>(); 
     foreach (Option opt in options) 
     { 
      _optionsList.Add(opt); 
     } 
    } 

    #endregion 

    #region methods 

    public override string ToString() 
    { 
     return _body; 
    } 

    public List<Option> GetOptions() 
    { 
     return OptionsList; 
    } 

    #endregion 

} 

我的網頁表單是這樣的:

<div runat="server" ID="quizDiv"> 



    <br /> 
    <br /> 

    <asp:Repeater ID="Repeater1" runat="server"> 
     <ItemTemplate> 
      <%--<asp:Label ID="questionBody" runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Body") %>>--%> 
      <asp:Label ID="questionBody" runat="server" Text=<%# ((Question)Container.DataItem).Body %>> 
      <asp:radiobuttonlist ID="blah" runat="server" DataTextField="Body" DataValueField="Body" DataSource=<%# ((Question)Container.DataItem).OptionsList %> > 

      </asp:radiobuttonlist> 
      </asp:Label> 
     </ItemTemplate> 
    </asp:Repeater> 
    <br /> 


</div> 

和後臺代碼如下所示:

protected void btnStart_Click(object sender, EventArgs e) 
    { 
     Dataset1 ds = new Dataset1(); 
     question = ds.CreateQuestion(); 

     List<Question> qts = new List<Question>(); 
     qts.Add(question); 

     Repeater1.DataSource = qts; 
     Repeater1.DataBind(); 

    } 

我暫且只用一個問題。顯示我的問題的標籤顯示正常,但沒有單選按鈕顯示用於顯示答案選項。我經歷了許多示例,這似乎適用於人們通過DataTable或DataSet使用數據庫中的數據時。然而,無論我如何使用Datasource,DataValueField和DataTextField參數,RadioButtonList仍然完全沒有用。正如你所看到的,我最初使用了一個Option的數組,並且嘗試了一個List但是無濟於事。

我在這裏錯過了什麼?


編輯 - 發現我的錯誤 我有 <asp:label><asp:radiobuttonlist>標籤嵌套錯了!這個標籤封裝了單選按鈕列表,並從我能做出的事情中引起問題。


另類 - 感謝balexandre

balexandre做只使用代碼behing提供了一個可行的替代方案。謝謝!

我將這個備選方案的代碼列表包含在需要它的所有人中,並在整個這篇文章中發生。

更改的標記,看起來像這樣:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> 
     <ItemTemplate> 
      <%--<asp:Label ID="questionBody" runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Body") %>>--%> 
      <asp:Label ID="questionBody" runat="server" Text=<%# ((Question)Container.DataItem).Body %>> 
      </asp:Label> 
      <asp:radiobuttonlist ID="blah" runat="server" > 

      </asp:radiobuttonlist> 

     </ItemTemplate> 
    </asp:Repeater> 

和代碼背後一定有事件處理程序:

protected void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
    { 

     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 

      RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("blah"); 
      // do anything with your rbl 
      foreach (Option opt in question.Options) 
      { 
       rbl.Items.Add(opt.ToString()); 
      } 
     } 
    } 

回答

1

可以使用Repeater方法OnItemDataBound女巫只能訪問模板在向頁面繪製任何內容之前調用。

void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { 

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { 

     RadioButtonList rbl = (RadioButtonList)e.Item.FindControl["blah"]; 
     // do anything with your rbl 

    } 
}  

,或者通過所有控件在Repeater收集循環,女巫例如您已提交的頁面。


BTW,並只爲您的信息

驗證碼:

private string _body; 
private bool? _isCorrect; 

public string Body 
{ 
    get { return _body; } 
} 

public bool? IsCorrect 
{ 
    get { return _isCorrect; } 
} 

相同

public string Body { private get; } 
public bool? IsCorrect { private get; } 
+0

感謝您,您在尋找單選按鈕列表的方法後面的代碼並設置它的內容也適用。我已經將它包含在下面的代碼中。 如果我使用創建屬性的快捷方式,我還可以訪問私有成員嗎?例如:public string Body {private get; },現在我不想要setter了,我的想法是隻允許通過構造函數設置成員,並且只允許通過get訪問器檢索其內容。我還可以在構造函數中使用_body嗎?它會接受嗎? – nikhilxp64 2012-02-12 23:07:24

+0

我想我可以這樣做: public string Body { get; 私人設置; } – nikhilxp64 2012-02-12 23:13:13