2011-08-26 53 views
0

我有一个radiobuttonlist,我在运行时用数据源填充。现在我想要的是在加载页面时默认选择文本为“每日”的项目。如何实现这一目标?Radiobutton list在页面上选择一个项目加载

+0

能否请你告诉我们哟你目前的代码是什么?使它更容易发现可能的语法错误 – diceler

+0

代码与我在那里复制和粘贴的代码相同。 – asma

回答

1
foreach (ListItem item in RadioButtonList1.Items) 
      { 
       if (item.Text.Contains("Daily At")) 
       { 
        item.Selected = true; 
        break; 
       } 
      } 
1

设置选定值属性。

if(!IsPostBack) 
{ 
.... 
RadioButtonList1.DataBind(); 
RadioButtonList1.SelectedValue="Daily At"; 
} 

您可以使用SelectedIndex属性。

if(!IsPostBack) 
    { 
    .... 
    RadioButtonList1.DataBind(); 
    RadioButtonList1.SelectedIndex=1; 
    } 

这是给你的参考样本:在Page_Load事件

public class Data 
    { 
     public int No { get; set; } 
     public string Name { get; set; } 
    } 

代码

if (!IsPostBack) 
     { 
      List<Data> list = new List<Data>() 
      { 
       new Data() { Name="Test1", No=10}, 
       new Data() { Name="Test2", No=20}, 
       new Data() { Name="Test3", No=30} 
      }; 

      RadioButtonList1.DataSource = list; 
      RadioButtonList1.DataTextField = "Name"; 
      RadioButtonList1.DataValueField = "No"; 
      RadioButtonList1.DataBind(); 

      RadioButtonList1.SelectedValue = "30"; 
     } 
+0

例外情况:''RadioButtonList1'有一个无效的SelectedValue,因为它在项目列表中不存在。 参数名称:值' – asma

1

试试这个

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    {  
     RadioButtonList.DataBind(); 
     RadioButtonList.Items.FindByText("Daily At").Selected=true; 
    } 
} 
相关问题