asp. net instance code for data binding

  • 2020-06-19 10:07:09
  • OfStack


public partial class _Default : System.Web.UI.Page
{
    protected string title=" Hello, everyone ";            // The front desk 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();
        }            // Data binding to 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();
            }
        }
    }// Implement provinces and cities 2 Levels of linkage 
}

Related articles: