如何在asp:RadioButtonList中为asp: listtiitem添加变量值

本文关键字:asp listtiitem 添加 变量值 中为 RadioButtonList | 更新日期: 2023-09-27 18:06:42

我有这样的代码:

<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设置为第三个。

我该怎么做?

如何在asp:RadioButtonList中为asp: listtiitem添加变量值

像这样:

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();

如果你不需要直接引用这些listtitem,

数据源可以是任何数组或可枚举集合,所以这些示例也可以工作:

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();

Use as:

List<String> lst = new List<String>();
lst.Add("Only You");
lst.Add("Need Is");
lst.Add("Love");
RadioButtonList1.DataSource = lst;
RadioButtonList1.DataBind();