简单绑定:
前台:<%# %> 中间写字符串的名字 例如:<%# color%>
后台: protected string color="blue";
<%# %>可以写在前台任何地方;
this.DataBind();
复杂绑定:
例如:对DropDownList1和RadioButtonList的绑定
string str = "server=.;database=students;integrated security=sspi";
DataSet ds = new DataSet();
using (SqlConnection sqlCnn = new SqlConnection())
{
sqlCnn.ConnectionString = str;
SqlCommand sqlcmm = sqlCnn.CreateCommand();
sqlcmm.CommandText = "select id,name from province";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
this.DropDownList1.DataSource = ds.Tables[0];
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "id";
//this.DropDownList1.DataBind();
this.RadioButtonList1.DataSource = ds.Tables[0];
this.RadioButtonList1.DataTextField = "name";
this.RadioButtonList1.DataValueField = "id";
//this.RadioButtonList1.DataBind();
this.GridView1.DataSource = ds.Tables[0];
this.DataBind();
绑定对类的使用:
后台代码:
List<DataHelper.Province> list = DataHelper.Class1.GetProvince();
this.DropDownList1.DataSource = list;
this.DropDownList1.DataTextField = "provincename";
this.DropDownList1.DataValueField = "provinceid";
this.GridView1.DataSource = list;
this.DataBind();
类的代码:
public class Class1
{
public static List<Province> GetProvince()
{
string str = "server=.;database=students;integrated security=sspi";
DataSet ds = new DataSet();
using (SqlConnection sqlCnn = new SqlConnection())
{
sqlCnn.ConnectionString = str;
SqlCommand sqlcmm = sqlCnn.CreateCommand();
sqlcmm.CommandText = "select id,name from province";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
List<Province> list = new List<Province>();
Province p;
foreach (DataRow item in ds.Tables[0].Rows)
{
p = new Province();
p.ProvinceName = item["name"].ToString();
p.ProvinceID = item["id"].ToString();
list.Add(p);
}
return list;
}
}
public class Province
{
public string ProvinceName{get;set;}
public string ProvinceID{get;set;}
}
绑定数据实现两级联动:
public partial class Default2 : System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["sqlstring"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection cnn = new SqlConnection(str))
{
using (SqlCommand cmm = cnn.CreateCommand())
{
cmm.CommandText = "select AreaCode,AreaName from TB_Area where ParentAreaCode='0001'";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmm);
da.Fill(ds);
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "AreaName";
DropDownList1.DataValueField = "AreaCode";
}
}
this.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection cnn = new SqlConnection(str))
{
using (SqlCommand cmm = cnn.CreateCommand())
{
cmm.CommandText = "select AreaCode,AreaName from TB_Area where AreaCode like '" + DropDownList1.SelectedValue + "__' ";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmm);
da.Fill(ds);
DropDownList2.DataSource = ds.Tables[0];
DropDownList2.DataTextField = "AreaName";
DropDownList2.DataValueField = "AreaCode";
}
}
this.DataBind();
}
}