投票程序,RadioButtonList绑定后,刚打开的页面RadioButtonList是没有选中的,怎么使页面一打开默认第一项被选中。
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
SqlConnection con=DB.createCon();
con.Open();
SqlCommand cmd=new SqlCommand("select vTitle from vote where vID="+this.vID+"",con);
this.Label1.Text=Convert.ToString(cmd.ExecuteScalar());
// 在此处放置用户代码以初始化页面
SqlConnection con1=DB.createCon();
con1.Open();
SqlCommand cmd1=new SqlCommand("select vItemID,vItem from voteItem where vID="+this.vID+"",con1);
SqlDataReader sdr=cmd1.ExecuteReader();
this.RadioButtonList1.DataSource=sdr;
this.RadioButtonList1.DataTextField="vItem";
this.RadioButtonList1.DataValueField="vItemID";
this.RadioButtonList1.DataBind();
sdr.Close();
con1.Close();
}
}
回答: 加上this.RadioButtonList1.SelectedIndex = 0; //选中第一项