簡單資料繫結和複雜資料繫結

c979170768發表於2011-11-21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

        //  string con = "server=.\\sqlexpress;initial catalog=english;integrated security=true";
        using (SqlConnection con1 = new SqlConnection(strcon))
        {
            DataSet dt = new DataSet();

            using (SqlCommand cmd = new SqlCommand("select * from [user]", con1))
            {
                con1.Open();
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                ada.Fill(dt);

                DropDownList1.DataSource = dt.Tables[0];
                DropDownList1.DataTextField = "name";
                DropDownList1.DataTextField = "id";
                DropDownList1.DataBind();


                RadioButtonList1.DataSource = dt.Tables[0];
                RadioButtonList1.DataTextField = "stuid";
                RadioButtonList1.DataValueField = "id";
                RadioButtonList1.DataBind();


                CheckBoxList1.DataSource = dt.Tables[0];
                CheckBoxList1.DataTextField = "name";
                CheckBoxList1.DataBind();
            }
        }
    }    //資料集

    protected void Button2_Click(object sender, EventArgs e)
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        DataTable dt = new DataTable();
        using (SqlConnection con=new SqlConnection(strcon))
        {
            using (SqlCommand cmd=new SqlCommand("select * from [user]",con))
            {
                con.Open();
                using (SqlDataReader myreader=cmd.ExecuteReader())
                {
                    dt.Load(myreader);
  
                }  
            } 
        }
        GridView gv = new GridView();
        gv.DataSource = dt;
        gv.ID = "baining";
        gv.DataBind();
        disp.Controls.Add(gv);
    }    //用DataTable

    protected void Button3_Click(object sender, EventArgs e)
    {
       
        Class1.prove.getname();
        DropDownList1.DataSource =
        DropDownList1.DataSource = "id";
              
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

        //  string con = "server=.\\sqlexpress;initial catalog=english;integrated security=true";
        using (SqlConnection con1 = new SqlConnection(strcon))
        {
            DataSet dt = new DataSet();

            using (SqlCommand cmd = new SqlCommand("select * from [user]", con1))
            {
                con1.Open();
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                ada.Fill(dt);

                DropDownList1.DataSource = dt.Tables[0];
                DropDownList1.DataTextField = "name";
                DropDownList1.DataTextField = "id";
                DropDownList1.DataBind();


                RadioButtonList1.DataSource = dt.Tables[0];
                RadioButtonList1.DataTextField = "stuid";
                RadioButtonList1.DataValueField = "id";
                RadioButtonList1.DataBind();


                CheckBoxList1.DataSource = dt.Tables[0];
                CheckBoxList1.DataTextField = "name";
                CheckBoxList1.DataBind();
            }
        }
    }    //資料集

    protected void Button2_Click(object sender, EventArgs e)
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        DataTable dt = new DataTable();
        using (SqlConnection con=new SqlConnection(strcon))
        {
            using (SqlCommand cmd=new SqlCommand("select * from [user]",con))
            {
                con.Open();
                using (SqlDataReader myreader=cmd.ExecuteReader())
                {
                    dt.Load(myreader);
  
                }  
            } 
        }
        GridView gv = new GridView();
        gv.DataSource = dt;
        gv.ID = "baining";
        gv.DataBind();
        disp.Controls.Add(gv);
    }    //用DataTable

    protected void Button3_Click(object sender, EventArgs e)
    {
       
        Class1.prove.getname();
        DropDownList1.DataSource =
        DropDownList1.DataSource = "id";
              
    }
}


 


            

相關文章