This article describes the ASP.net data binding instance code, the need for friends can refer to the
copy code code as follows:
public partial class _default:system.web.ui.page
{
protected string title= "everyone good"; Front code <title><% #title%></title>
protected void Page_Load (object sender, EventArgs e)
{
DataSet ds = new DataSet ();
String sql = configurationmanager.connectionstrings["strSQL"]. ConnectionString;
using (SqlConnection sqlcnn=new SqlConnection (SQL))
{
using (SqlCommand Sqlcmm=sqlcnn.createcommand ())
{
sqlcmm.commandtext = "SELECT * from List";
SqlDataAdapter adapter = new SqlDataAdapter (SQLCMM);
adapter. Fill (DS);
}
this. Radiobuttonlist1.datasource = ds. Tables[0];
this. Radiobuttonlist1.datatextfield = "ListName";
This. Radiobuttonlist1.datavaluefield = "id";
//this. Radiobuttonlist1.databind ();
this. Checkboxlist1.datasource = ds. Tables[0];
this. Checkboxlist1.datatextfield = "ListName";
this. Checkboxlist1.datavaluefield = "id";
//this. Radiobuttonlist1.databind ();
this. DataBind ();
}//Data binding to Radiobuttonlist,checkboxlist
if (! IsPostBack)
{
DataSet ds1 = new DataSet ();
using (SqlConnection sqlCnn1 = new SqlConnection (SQL))
{
using (SqlCommand sqlCmm1 = Sqlcnn1.createcommand ())
{
sqlcmm1.commandtext = "Select Provinceid,provincename from Province";
SqlDataAdapter adapter = new SqlDataAdapter (SQLCMM1);
adapter. Fill (DS1);
this. Dropdownlist1.datasource = DS1. Tables[0];
this. Dropdownlist1.datatextfield = "Provincename";
this. Dropdownlist1.datavaluefield = "Provinceid";
This. Dropdownlist1.databind ();
}
}
}
}
protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
DataSet ds = new DataSet ();
string str = configurationmanager.connectionstrings["strSQL"]. ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection (str))
{
using (SqlCommand SQLCMM = Sqlcnn.createcommand ())
{
sqlcmm.commandtext = "Select Cityid,cityname from city where provinceid= '" + this. Dropdownlist1.selectedvalue + "'";
SqlDataAdapter adapter = new SqlDataAdapter (SQLCMM);
adapter. Fill (DS);
this. Dropdownlist2.datasource = ds. Tables[0];
This. Dropdownlist2.datatextfield = "CityName";
this. Dropdownlist2.datavaluefield = "Cityid";
this. Dropdownlist2.databind ();
}
}
}//realize two-level linkage
}