在Datalist中使用RadioButtonList [英] Use of RadioButtonList in Datalist
本文介绍了在Datalist中使用RadioButtonList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
在我的项目中,我使用数据列表显示所有问题,相应的选项在radiobutton-list
现在问题在于Datalist-ItemBoundEvent只有第一个选项是绑定,其余选项不显示在radiobutton列表中
我的asp页面是
Hello,
In my project i am using a data-list to display all the questions and the corresponding options are in radiobutton-list
now the problem is in Datalist-ItemBoundEvent only the first option is binding,rest options are not displaying in the radiobutton list
my asp page is
<asp:DataList ID="dlQuestions" runat="server" CssClass="datalist" OnItemDataBound="dlQuestions_ItemDataBound">
<ItemTemplate>
<table>
<colgroup>
<col class="column1" style="" />
<col class="column2" style="" />
</colgroup>
<tr>
<td>
<span class="heading4"><%#Container.ItemIndex+1 %>.</span>
<asp:HiddenField ID="hf1" runat="server" Value='<%#Eval("TrainingExamQuestionBankId")%>' />
</td>
<td>
<span class="heading4"><%#Eval("Question") %></span>
</td>
</tr>
<tr>
<td></td>
<td class="Label">
<asp:RadioButtonList runat="server" ID="rbOptions">
</asp:RadioButtonList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
和我背后的鳕鱼就像
and my cod behind is like
protected void dlQuestions_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("rbOptions");
HiddenField hdn = (HiddenField)e.Item.FindControl("hf1");
int QuestionId = Convert.ToInt32(hdn.Value);
var query = (from a in TraningExamQuestionBank
where a.TrainingExamQuestionBankId == QuestionId
select new
{
a.OptionA,
a.OptionB,
a.OptionC,
a.OptionD,
a.OptionE,
}).ToList();
DataTable dt = ConvertToDataTable(query);
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = dt.Columns[0].ToString();
RadioButtonList1.DataValueField = dt.Columns[0].ToString();
RadioButtonList1.DataBind();
}
}
public DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable Table = new DataTable();
foreach (PropertyDescriptor prop in properties)
Table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = Table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
Table.Rows.Add(row);
}
return Table;
}
请帮助.... !!
please help....!!
推荐答案
好像你有一个记录所有选项,它将绑定到单选按钮列表中的一个项目。你能做的是;获取记录并创建包含所有选项的列表并将其绑定到单选按钮列表,如下所示
it seems you have one record with all the options and it will bind in to a one item in radio button list. what you can do is; get the record and create list with all the options and bind it to radio button list as below
var item= (from a in TraningExamQuestionBank
where a.TrainingExamQuestionBankId == QuestionId
select new
{
a.OptionA,
a.OptionB,
a.OptionC,
a.OptionD,
a.OptionE,
}).FirstOrDefault();
if(item !=null)
{
List<string> items = new List<string>(){ item.OptionA, item.OptionB, item.OptionC, item.OptionD, item.OptionE};
RadioButtonList1.DataSource = items;
RadioButtonList1 DataBind();
}
这篇关于在Datalist中使用RadioButtonList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文