Asp.net Data Binding

Source: Internet
Author: User

Public partial class _ Default: System. Web. UI. Page

{

Protected string title = "Hello everyone"; // front-end 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 ();

} // Bind data to RadioButtonList and CheckBoxList

If (! IsPostBack)

{

DataSet ds1 = new DataSet ();

Using (SqlConnection sqlCnn1 = new SqlConnection (SQL ))

{

Using (SqlCommand sqlcmcm1 = sqlCnn1.CreateCommand ())

{

Sqlcmcm1.commandtext = "select provinceid, provincename from Province ";

SqlDataAdapter adapter = new SqlDataAdapter (sqlcmcm1 );

Adapter. Fill (ds1 );

This. DropDownList1.DataSource = ds1.Tables [0];

This. DropDownList1.DataTextField = "provincename ";

This. DropDownList1.DataValueField = "provinceid ";

This. DropDownList1.DataBind ();

}

}

}

}

Protected void dropdownlistincluselectedindexchanged (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 ())

{Www.2cto.com

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 ();

}

}

} // Implement provincial/municipal level-2 Linkage

}

 

From haitaoDoit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.