前台:
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
<br />
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</div>
</form>
</body>
后台:
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select * from province";
DataTable dt = SQLHelper.ExecuteDataTable(sql);
this.RadioButtonList1.DataSource = dt;
this.RadioButtonList1.DataTextField = "Provinces";
this.RadioButtonList1.DataValueField = "PId";
this.RadioButtonList1.DataBind();
this.CheckBoxList1.DataSource = dt;
this.CheckBoxList1.DataTextField = "Provinces";
this.CheckBoxList1.DataValueField = "PId";
this.CheckBoxList1.DataBind();
}
SQLHelper类:
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
return dt;
}
本文介绍如何使用ASP.NET中的RadioButtonList和CheckBoxList控件,并通过SQLHelper类从数据库中获取省份数据进行绑定。该方法适用于网站后台配置,能够动态加载数据到前端页面。
1061

被折叠的 条评论
为什么被折叠?



