对RadioButtonList控件绑定后,选择其中的一项,然后提交,但是在Label1中看不到我选中的项,并且选中的标签也不见了,恨苦恼,实在不知道怎么回事了?才来求问各位。
共用到5个控件:两个RadioButton,一个RadioButtonList,一个Button,一个Label。
以下是源码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace Example05.Ex05_01
{
/// <summary>
/// Ex05_10 的摘要说明。
/// </summary>
public class Ex05_10 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton RadioButton1;
protected System.Web.UI.WebControls.RadioButton RadioButton2;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text="";
try
{
DataSet ds=new DataSet();
SqlConnection myConnection=new SqlConnection("server=Localhost;uid=sa;pwd=admin;database=Northwind");
string strSQL="select distinct Title from Employees";
SqlDataAdapter myCommand=new SqlDataAdapter(strSQL,myConnection);
myCommand.Fill(ds,"Employees"); RadioButtonList1.DataSource=ds.Tables["Employees"].DefaultView;
RadioButtonList1.DataValueField=ds.Tables["Employees"].Columns[0].ColumnName;
RadioButtonList1.DataTextField=ds.Tables["Employees"].Columns[0].ColumnName;
RadioButtonList1.DataBind(); RadioButtonList1.RepeatDirection=System.Web.UI.WebControls.RepeatDirection.Horizontal;
RadioButtonList1.RepeatColumns=2;
}
catch
{
Response.Write("<script language='javascript'>alert('连接失败!')</script>");
}// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
string ss="性别:";
if(RadioButton1.Checked==true)
{
ss+=RadioButton1.Text;
}
if(RadioButton2.Checked==true)
{
ss+=RadioButton2.Text;
} ss+=",职位:";

if(RadioButtonList1.SelectedIndex > -1)
{
ss+=RadioButtonList1.SelectedItem.Text;
} Label1.Text=ss;
}
}
}