我用asp.net连接了数据库之后自动在radiolist下面生成了好几个radiobutton,但问题是在初始化的时候设定第一个button被点中,然后成功得到数据后,建立了3个button,最后我发现那些radiobutton怎么点击,调试的时候都没有用,还是没办法响应这个点击事件,或者说,在便利radiolist下的Items[i].Selected的时候,还是按照初始化的样子第一个被点中了,其他的怎么点也没反应,求指导,谢谢
namespace WebApplicationHelloWorld
{
public partial class _Default : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter dataAdapter;
protected System.Data.DataSet myDataSet;
protected System.Data.SqlClient.SqlCommand myCommand;
protected System.Data.SqlClient.SqlConnection myConnection;
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
protected global::System.Web.UI.WebControls.RadioButtonList rbl1;
protected global::System.Web.UI.WebControls.Button Button1;
protected global::System.Web.UI.WebControls.TextBox txtName;
protected global::System.Web.UI.WebControls.Label lblFeedBack; protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Integrated Security=SSPI;data source=.\\SQLEXPRESS; database=TestDatabse;user id=lichen;password=123456;";
myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
myConnection.Open();
myDataSet = new System.Data.DataSet();
myDataSet.CaseSensitive = true; myCommand = new System.Data.SqlClient.SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select ID,Name from TsetTable_li"; dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
dataAdapter.SelectCommand = myCommand;
dataAdapter.TableMappings.Add("Table", "TsetTable_li");
dataAdapter.Fill(myDataSet);
rbl1.RepeatLayout = System.Web.UI.WebControls.RepeatLayout.Flow;
rbl1.DataTextField = "Name";
rbl1.DataValueField = "ID";
rbl1.DataSource = myDataSet.Tables["TsetTable_li"].DefaultView;
rbl1.DataBind();
rbl1.Items[0].Selected = true;
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.myConnection = new System.Data.SqlClient.SqlConnection();
this.dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
this.myCommand = new System.Data.SqlClient.SqlCommand();
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.myConnection.ConnectionString = "Integrated Security=SSPI;data source=.\\SQLEXPRESS; database=TestDatabse;user id=lichen;password=123456;";
this.dataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "TsetTable_li", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Name", "Name")})});
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion protected void Button1_Click(object sender, EventArgs e)
{
string msg;
msg = "Thank you " + txtName.Text + ". You chose "; for (int i = 0; i < rbl1.Items.Count; i++)
{
if (rbl1.Items[i].Selected)
{
msg = msg + rbl1.Items[i].Text;
lblFeedBack.Text = msg;
}
}
}
}
}
namespace WebApplicationHelloWorld
{
public partial class _Default : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter dataAdapter;
protected System.Data.DataSet myDataSet;
protected System.Data.SqlClient.SqlCommand myCommand;
protected System.Data.SqlClient.SqlConnection myConnection;
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
protected global::System.Web.UI.WebControls.RadioButtonList rbl1;
protected global::System.Web.UI.WebControls.Button Button1;
protected global::System.Web.UI.WebControls.TextBox txtName;
protected global::System.Web.UI.WebControls.Label lblFeedBack; protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Integrated Security=SSPI;data source=.\\SQLEXPRESS; database=TestDatabse;user id=lichen;password=123456;";
myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
myConnection.Open();
myDataSet = new System.Data.DataSet();
myDataSet.CaseSensitive = true; myCommand = new System.Data.SqlClient.SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select ID,Name from TsetTable_li"; dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
dataAdapter.SelectCommand = myCommand;
dataAdapter.TableMappings.Add("Table", "TsetTable_li");
dataAdapter.Fill(myDataSet);
rbl1.RepeatLayout = System.Web.UI.WebControls.RepeatLayout.Flow;
rbl1.DataTextField = "Name";
rbl1.DataValueField = "ID";
rbl1.DataSource = myDataSet.Tables["TsetTable_li"].DefaultView;
rbl1.DataBind();
rbl1.Items[0].Selected = true;
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.myConnection = new System.Data.SqlClient.SqlConnection();
this.dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
this.myCommand = new System.Data.SqlClient.SqlCommand();
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.myConnection.ConnectionString = "Integrated Security=SSPI;data source=.\\SQLEXPRESS; database=TestDatabse;user id=lichen;password=123456;";
this.dataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "TsetTable_li", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Name", "Name")})});
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion protected void Button1_Click(object sender, EventArgs e)
{
string msg;
msg = "Thank you " + txtName.Text + ". You chose "; for (int i = 0; i < rbl1.Items.Count; i++)
{
if (rbl1.Items[i].Selected)
{
msg = msg + rbl1.Items[i].Text;
lblFeedBack.Text = msg;
}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货