熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net 數據綁定的實例代碼

2022-06-13   來源: .NET編程 
這篇文章介紹了aspnet 數據綁定的實例代碼有需要的朋友可以參考一下   復制代碼 代碼如下:

  
public partial class _Default : SystemWebUIPage
{
    protected string title="大家好";            //前台代碼<title><%#title %></title>
    protected void Page_Load(object sender EventArgs e)
    {
        DataSet ds = new DataSet();
        string sql = ConfigurationManagerConnectionStrings["strsql"]ConnectionString;
        using (SqlConnection sqlCnn=new SqlConnection(sql))
        {
            using (SqlCommand sqlCmm=sqlCnnCreateCommand())
            {
                sqlCmmCommandText = "select * from List";
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
                adapterFill(ds);
            }
            thisRadioButtonListDataSource = dsTables[];
            thisRadioButtonListDataTextField = "listname";
            thisRadioButtonListDataValueField = "id";
            //thisRadioButtonListDataBind();
            thisCheckBoxListDataSource = dsTables[];
            thisCheckBoxListDataTextField = "listname";
            thisCheckBoxListDataValueField = "id";
            //thisRadioButtonListDataBind();
            thisDataBind();
        }            //數據綁定到RadioButtonListCheckBoxList
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            using (SqlConnection sqlCnn = new SqlConnection(sql))
            {
                using (SqlCommand sqlCmm = sqlCnnCreateCommand())
                {
                    sqlCmmCommandText = "select provinceidprovincename from Province";
                    SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
                    adapterFill(ds);
                    thisDropDownListDataSource = dsTables[];
                    thisDropDownListDataTextField = "provincename";
                    thisDropDownListDataValueField = "provinceid";
                    thisDropDownListDataBind();
                }
            }
        }
    }
    protected void DropDownList_SelectedIndexChanged(object sender EventArgs e)
    {
        DataSet ds = new DataSet();
        string str = ConfigurationManagerConnectionStrings["strsql"]ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(str))
        {
            using (SqlCommand sqlCmm = sqlCnnCreateCommand())
            {
                sqlCmmCommandText = "select cityidcityname from City where provinceid=" + thisDropDownListSelectedValue + "";
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
                adapterFill(ds);
                thisDropDownListDataSource = dsTables[];
                thisDropDownListDataTextField = "cityname";
                thisDropDownListDataValueField = "cityid";
                thisDropDownListDataBind();
            }
        }
    }//實現省市二級聯動
}


From:http://tw.wingwit.com/Article/program/net/201311/14140.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.