2011-08-24 4 views
1

이 코드가 있습니다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으로 설정하십시오.

어떻게하면됩니까? 이처럼

답변

3

:이 코드 숨김으로

<asp:RadioButtonList id="radInteresse" runat="server">   
</asp:RadioButtonList> 

:

radInteresse.Items[0].Value = var1; 
radInteresse.Items[2].Value = var2; 
2

당신이 사용할 수

radInterese.DataSource = new[] { "Only You", "Need Is", "Love" }; 
radInterese.DataBind(); 

직접 그 많은 ListItems를 참조 할 필요가없는 경우 .

데이터 소스는 배열 또는 열거 수집 될 수 있으므로 이러한 예는 너무 일 것이다 :

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

사용 :

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