这篇文章介绍了asp
net 数据绑定的实例代码
有需要的朋友可以参考一下
复制代码 代码如下:
public partial class _Default : SystemWebUIPage
{
protected string title="大家好"; //前台代码<title><%#title %></title>
protected void Page_Load(object sender EventArgs e)
{
DataSet ds = new DataSet();
string sql = ConfigurationManagerConnectionStrings["strsql"]ConnectionString;
using (SqlConnection sqlCnn=new SqlConnection(sql))
{
using (SqlCommand sqlCmm=sqlCnnCreateCommand())
{
sqlCmmCommandText = "select * from List";
SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
adapterFill(ds);
}
thisRadioButtonListDataSource = dsTables[];
thisRadioButtonListDataTextField = "listname";
thisRadioButtonListDataValueField = "id";
//thisRadioButtonListDataBind();
thisCheckBoxListDataSource = dsTables[];
thisCheckBoxListDataTextField = "listname";
thisCheckBoxListDataValueField = "id";
//thisRadioButtonListDataBind();
thisDataBind();
} //数据绑定到RadioButtonListCheckBoxList
if (!IsPostBack)
{
DataSet ds = new DataSet();
using (SqlConnection sqlCnn = new SqlConnection(sql))
{
using (SqlCommand sqlCmm = sqlCnnCreateCommand())
{
sqlCmmCommandText = "select provinceidprovincename from Province";
SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
adapterFill(ds);
thisDropDownListDataSource = dsTables[];
thisDropDownListDataTextField = "provincename";
thisDropDownListDataValueField = "provinceid";
thisDropDownListDataBind();
}
}
}
}
protected void DropDownList_SelectedIndexChanged(object sender EventArgs e)
{
DataSet ds = new DataSet();
string str = ConfigurationManagerConnectionStrings["strsql"]ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(str))
{
using (SqlCommand sqlCmm = sqlCnnCreateCommand())
{
sqlCmmCommandText = "select cityidcityname from City where provinceid=" + thisDropDownListSelectedValue + "";
SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
adapterFill(ds);
thisDropDownListDataSource = dsTables[];
thisDropDownListDataTextField = "cityname";
thisDropDownListDataValueField = "cityid";
thisDropDownListDataBind();
}
}
}//实现省市二级联动
}