![](/img/trans.png)
[英]Dynamically add ListItem to RadiobuttonList with proper ID in ASP.NET
[英]How to add a value from variable to asp:ListItem in a asp:RadioButtonList
我有這個代碼:
<asp:RadioButtonList id="radInteresse" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
而且我有這些字符串:
string var1="Only You";
string var2="Need Is";
string var3="Love";
現在,我想將var1
的值設置為第一個ListItem
,將var2
的值設置為第二個,並將var3
設置為第三個。
我該怎么做?
像這樣:
radInteresse.Items[0].Value = var1;
radInteresse.Items[2].Value = var2;
您可以使用此:
<asp:RadioButtonList id="radInteresse" runat="server">
</asp:RadioButtonList>
有了以下代碼:
radInterese.DataSource = new[] { "Only You", "Need Is", "Love" };
radInterese.DataBind();
如果您不需要直接引用那些ListItems。
DataSource可以是任何數組或可枚舉的集合,因此這些示例也可以使用:
radInterese.DataSource = new[] { var1, var2, var3 };
radInterese.DataBind();
要么
string[] myVars;
// make sure you set the value of this array here
radInterese.DataSource = myVars;
radInterese.DataBind();
要么
List<string> myVars = new List<string>();
// make sure you add items to this List here, e.g. myVars.Add(var1);
radInterese.DataSource = myVars;
radInterese.DataBind();
用於:
List<String> lst = new List<String>();
lst.Add("Only You");
lst.Add("Need Is");
lst.Add("Love");
RadioButtonList1.DataSource = lst;
RadioButtonList1.DataBind();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.