asp.net 數(shù)據(jù)綁定的實(shí)例代碼
更新時(shí)間:2013年07月30日 10:46:06 作者:
這篇文章介紹了asp.net 數(shù)據(jù)綁定的實(shí)例代碼,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
public partial class _Default : System.Web.UI.Page
{
protected string title="大家好"; //前臺(tái)代碼<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();
} //數(shù)據(jù)綁定到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();
}
}
}//實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)
}
相關(guān)文章
Asp.net后臺(tái)把腳本樣式輸出到head標(biāo)簽中節(jié)省代碼冗余
最近在學(xué)習(xí)開(kāi)發(fā)服務(wù)器控件,其它就少不了為控件注冊(cè)js和css之類的資源文件,或者直接注冊(cè)純腳本樣式。其中就遇到如下問(wèn)題 1、 注冊(cè)的資源文件或純腳本樣式在生成的頁(yè)面中都不在head標(biāo)簽中(當(dāng)然這個(gè)不影響頁(yè)面功能) 2、 一個(gè)頁(yè)面使用多個(gè)一樣的控件時(shí),會(huì)出現(xiàn)重復(fù)輸入(出現(xiàn)多余代碼)2013-02-02C#中OpenFileDialog和PictrueBox的用法分析
這篇文章主要介紹了C#中OpenFileDialog和PictrueBox的用法,以實(shí)例的形式較為詳細(xì)的分析了OpenFileDialog和PictrueBox使用時(shí)的注意事項(xiàng)與具體用法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11ASP.NET MVC Webuploader實(shí)現(xiàn)上傳功能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC Webuploader實(shí)現(xiàn)上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09asp.net頁(yè)面SqlCacheDependency緩存實(shí)例
這篇文章主要介紹了asp.net頁(yè)面SqlCacheDependency緩存實(shí)例,以一個(gè)完整實(shí)例來(lái)展現(xiàn)asp.net中緩存技術(shù)的使用方法,需要的朋友可以參考下2014-08-08C#中Dictionary幾種遍歷的實(shí)現(xiàn)代碼
C#中Dictionary幾種遍歷的實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-02-02ASP.NET?MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
這篇文章介紹了ASP.NET?MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08合并兩個(gè)DataSet的數(shù)據(jù)內(nèi)容的方法
合并兩個(gè)DataSet的數(shù)據(jù)內(nèi)容的方法,需要的朋友可以參考一下2013-03-03