asp.Netデータバインドのインスタンスコード
4043 ワード
public partial class _Default : System.Web.UI.Page
{
protected string title=" "; //
<br/> protected void Page_Load(object sender, EventArgs e)
<br/> {
<br/> DataSet ds = new DataSet();
<br/> string sql = ConfigurationManager.ConnectionStrings["strsql"].ConnectionString;
<br/> using (SqlConnection sqlCnn=new SqlConnection(sql))
<br/> {
<br/> using (SqlCommand sqlCmm=sqlCnn.CreateCommand())
<br/> {
<br/> sqlCmm.CommandText = "select * from List";
<br/> SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
<br/> adapter.Fill(ds);
<br/> }
<br/> this.RadioButtonList1.DataSource = ds.Tables[0];
<br/> this.RadioButtonList1.DataTextField = "listname";
<br/> this.RadioButtonList1.DataValueField = "id";
<br/> //this.RadioButtonList1.DataBind();
<br/> this.CheckBoxList1.DataSource = ds.Tables[0];
<br/> this.CheckBoxList1.DataTextField = "listname";
<br/> this.CheckBoxList1.DataValueField = "id";
<br/> //this.RadioButtonList1.DataBind();
<br/> this.DataBind();
<br/> } // RadioButtonList,CheckBoxList
<br/> if (!IsPostBack)
<br/> {
<br/> DataSet ds1 = new DataSet();
<br/> using (SqlConnection sqlCnn1 = new SqlConnection(sql))
<br/> {
<br/> using (SqlCommand sqlCmm1 = sqlCnn1.CreateCommand())
<br/> {
<br/> sqlCmm1.CommandText = "select provinceid,provincename from Province";
<br/> SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm1);
<br/> adapter.Fill(ds1);
<br/> this.DropDownList1.DataSource = ds1.Tables[0];
<br/> this.DropDownList1.DataTextField = "provincename";
<br/> this.DropDownList1.DataValueField = "provinceid";
<br/> this.DropDownList1.DataBind();
<br/> }
<br/> }
<br/> }
<br/> }
<br/> protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
<br/> {
<br/> DataSet ds = new DataSet();
<br/> string str = ConfigurationManager.ConnectionStrings["strsql"].ConnectionString;
<br/> using (SqlConnection sqlCnn = new SqlConnection(str))
<br/> {
<br/> using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
<br/> {
<br/> sqlCmm.CommandText = "select cityid,cityname from City where provinceid='" + this.DropDownList1.SelectedValue + "'";
<br/> SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
<br/> adapter.Fill(ds);
<br/> this.DropDownList2.DataSource = ds.Tables[0];
<br/> this.DropDownList2.DataTextField = "cityname";
<br/> this.DropDownList2.DataValueField = "cityid";
<br/> this.DropDownList2.DataBind();
<br/> }
<br/> }
<br/> }//
<br/>}
<br/>
</code></pre>
<div class="clearfix">
<span id="art_bot" class="jbTestPos"/>
</div>
</div>
</div>
</div>