C# 填充 ASP.NET 中继器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15189805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populating an ASP.NET Repeater
提问by Javi
I have a repeater in my aspx:
我的 aspx 中有一个中继器:
<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound"
Visible="true">
</asp:Repeater>
in the c# side of the web I wrote this function:
在网络的 c# 端我写了这个函数:
protected void createRadioButtons(DataSet ds){
List<System.Web.UI.WebControls.RadioButton> buttons = new List<System.Web.UI.WebControls.RadioButton>();
foreach (DataTable dt in ds.Tables){
foreach (DataRow r in dt.Rows){
System.Web.UI.WebControls.RadioButton rb = new System.Web.UI.WebControls.RadioButton();
rb.Text = r[1] + " " + r[2] + " " + r[3] + " " + r[4];
rb.GroupName = (string)r[5];
buttons.Add(rb);
}
}
rptDummy.DataSource = buttons;
rptDummy.DataBind();
}
But when trying it, it shows nothing. What am I doing wrong?
但是当尝试它时,它什么也没显示。我究竟做错了什么?
采纳答案by Mt. Schneiders
Try this:
尝试这个:
1 - Define the Repeater
:
1 - 定义Repeater
:
<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound" >
<ItemTemplate>
<asp:RadioButtonList ID="rbl" runat="server" DataTextField="Item2" DataValueField="Item2" />
</ItemTemplate>
</asp:Repeater>
2 - Build the data structure and bind the repeater:
2 - 构建数据结构并绑定转发器:
List<Tuple<string,string>> values = new List<Tuple<string,string>>();
foreach (DataTable dt in ds.Tables){
foreach (DataRow r in dt.Rows){
string text = r[1] + " " + r[2] + " " + r[3] + " " + r[4];
string groupName = (string)r[5];
values.Add(new Tuple<string,string>(groupName, text));
}
}
//Group the values per RadioButton GroupName
rptDummy.DataSource = values.GroupBy(x => x.Item1);
rptDummy.DataBind();
3 - Define the OnItemDataBound
event:
3 - 定义OnItemDataBound
事件:
protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
IGrouping<string, Tuple<string, string>> group = (IGrouping<string, Tuple<string, string>>)e.Item.DataItem;
RadioButtonList list = (RadioButtonList)e.Item.FindControl("rbl");
list.DataSource = group;
list.DataBind();
}
}
You see, each IGrouping<string, Tuple<string, string>>
refers to a group of RadioButtons of a certain GroupName, they are also the items from the repeater. For each item we create a new RadioButtonList that represents the whole group of RadioButtons.
你看,每一个都是IGrouping<string, Tuple<string, string>>
指某个GroupName的一组RadioButtons,它们也是repeater中的items。对于每个项目,我们创建一个新的 RadioButtonList,代表整个 RadioButtons 组。
You can make it better by using a different DataStructure than a Tuple
, it is often unclear what Item1
and Item2
means.
您可以通过使用与 a 不同的 DataStructure 使其更好Tuple
,但通常不清楚是什么Item1
和Item2
意味着什么。
UPDATE:
更新:
If you want to see the selected values:
如果要查看所选值:
protected void button_OnClick(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptDummy.Items)
{
RadioButtonList list = (RadioButtonList)item.FindControl("rbl");
string selectedValue = list.SelectedValue;
}
}
回答by syed mohsin
You should put the RadioButton
in repeater and bind it in the createRadioButtons
event.
您应该放入RadioButton
中继器并将其绑定到createRadioButtons
事件中。