asp.net 數(shù)據(jù)綁定的實例代碼
更新時間:2013年07月30日 10:46:06 作者:
這篇文章介紹了asp.net 數(shù)據(jù)綁定的實例代碼,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
public partial class _Default : System.Web.UI.Page
{
protected string title="大家好"; //前臺代碼<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();
}
}
}//實現(xiàn)省市二級聯(lián)動
}
相關(guān)文章
asp.net 長文章通過設(shè)定的行數(shù)分頁
長文章通過設(shè)定的行數(shù)來實現(xiàn)分頁的代碼。2009-12-12
Visual Studio 2015和 .NET Core安裝教程
這篇文章主要為大家詳細(xì)介紹了Visual Studio Community 2015和 .NET Core安裝圖文教程,感興趣的小伙伴們可以參考一下2016-07-07
javascript判斷是否有對RadioButtonList選項選擇
寫個Javascript來判斷是否有對RadioButtonList選項選擇,附動畫演示,感興趣的朋友可以了解下,希望對您們有幫助2013-01-01
asp.net實現(xiàn)非常實用的自定義頁面基類(附源碼)
這篇文章主要介紹了asp.net實現(xiàn)非常實用的自定義頁面基類,包含日志處理、控件賦值、異常處理等功能,非常具有實用價值,需要的朋友可以參考下2015-11-11
使用ASP.NET一般處理程序或WebService返回JSON的實現(xiàn)代碼
今天, 將為大家說明如何在 ASP.NET 中使用一般處理程序或者 WebService 向 javascript 返回 JSON2011-10-10

