DataBind練着玩呢

 

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected string GetStyleOfbody()
    {
        return this.DropDownList1.SelectedValue;
       
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
              NewMethod();
              NewMethod1();

        }
       
     
    }

    private void NewMethod1()
    {
        string str1 = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds1 = new DataSet();
        using (SqlConnection sqlcnn1 = new SqlConnection())
        {
            sqlcnn1.ConnectionString = str1;
            SqlCommand sqlcmm1 = sqlcnn1.CreateCommand();
            sqlcmm1.CommandText = "select id,name from province";
            SqlDataAdapter da1 = new SqlDataAdapter(sqlcmm1);
            da1.Fill(ds1);
        }
        this.RadioButtonList1.DataSource = ds1.Tables[0];
        this.RadioButtonList1.DataTextField = "name";
        this.RadioButtonList1.DataValueField = "id";
        this.DataBind();
    }

    private void NewMethod()
    {
        string str = "server=.;database=DataBind;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.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds = new DataSet();
        using (SqlConnection sqlcn = new SqlConnection())
        {
            sqlcn.ConnectionString = str;
            SqlCommand sqlcm = sqlcn.CreateCommand();
            sqlcm.CommandText = "select id,myid,name from city where id=@id";
            sqlcm.Parameters.Add(new SqlParameter("@id", this.DropDownList1.SelectedValue));
            sqlcn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sqlcm);
            sda.Fill(ds);
          
          
        }
        this.DropDownList2.DataSource = ds.Tables[0];
        this.DropDownList2.DataTextField = "name";
        this.DropDownList2.DataValueField = "id";
        this.DataBind();
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds = new DataSet();
        using (SqlConnection sqlcn = new SqlConnection())
        {
            sqlcn.ConnectionString = str;
            SqlCommand sqlcm = sqlcn.CreateCommand();
            sqlcm.CommandText = "select id,myid,name from city where id=@id";
            sqlcm.Parameters.Add(new SqlParameter("@id", this.RadioButtonList1.SelectedValue));
            sqlcn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sqlcm);
            sda.Fill(ds);
          
          
        }
        this.RadioButtonList2.DataSource = ds.Tables[0];
        this.RadioButtonList2.DataTextField= "name";
        this.RadioButtonList2.DataValueField = "id";
        this.DataBind();
    }

}

發佈了31 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章