WebSite9

DataListBingding.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class DataListBingding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) listbind();

    }
    void listbind() {
        string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        DataSet ds = new DataSet();
        SqlConnection sqlconn = new SqlConnection(sqlconnstr);
        SqlDataAdapter sqld = new SqlDataAdapter("select * from student", sqlconn);
        sqld.Fill(ds, "tabstudent");
     
        DataList1.DataBind();

    }
}

SingleValueBinding.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SingleValueBinding : System.Web.UI.Page
{
    public String SingleValueBindingStr = "单值绑定";
    public String SingleValueBindingStr1 = "男";
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();
        this.TextBox1.Text = this.SingleValueBindingStr + "2";
        this.TextBox2.Text = this.SingleValueBindingStr + "3";
        this.TextBox3.Text = this.SingleValueBindingStr1;
    }
}

ListValueBinding.aspx.cs

public partial class ListValueBinding : System.Web.UI.Page
{
   
    //定义并初始化字符串数组
    String[] DataSourceForDDL = new String[] { "张小兵", "李明", "陈飞" };
    //定义哈希表
    Hashtable DataSourceForCBL = new Hashtable(3);
    //定义Arraylist
    ArrayList DataSourceForBL = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack) {
            //初始化哈希表DataSourceForCBL
            this.DataSourceForCBL.Add("徐明明", "男");
            this.DataSourceForCBL.Add("韩旭", "女");
            this.DataSourceForCBL.Add("陈静", "女");
            //初始化DataSourceForBL
            this.DataSourceForBL.Add(new Key_ValueClass("百度", "http://www.baidu.com"));
            this.DataSourceForBL.Add(new Key_ValueClass("CSDN", "http://www.csdn.net"));
            //为dropdownlist绑定数据
            this.DropDownList1.DataSource = this.DataSourceForDDL;
            this.DropDownList1.DataBind();
            //完成绑定后在dropdownlist中第一个位置插入一个数据项
            this.DropDownList1.Items.Insert(0, "请选择");
            //为checkboxlist绑定数据
            this.CheckBoxList1.DataSource = this.DataSourceForCBL;
            //由于哈希表中存储一个键值对的集合并希望在checkboxlist中处理键值对,因此设定数据源后还需
            //设定datatextlist和datavaluefield属性
            this.CheckBoxList1.DataTextField = "key";
            this.CheckBoxList1.DataValueField = "value";
            this.CheckBoxList1.DataBind();
            this.BulletedList1.DataSource = this.DataSourceForBL;
            this.BulletedList1.DataTextField = "Name";
            this.BulletedList1.DataValueField = "Url";
            this.BulletedList1.DataBind();

        }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox2.Text = "";
        TextBox2.Text = DropDownList1.SelectedValue;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected) TextBox1.Text += li.Text + ":" + li.Value + ",";
        }
    }
}

dropdownlist3.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
public partial class dropdownlist3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


    }
}

GridViewBingding.aspx.cs

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

public partial class GridViewBingding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        DataSet ds = new DataSet();
        using (SqlConnection sqlconn = new SqlConnection(sqlconnstr))
        {
            SqlDataAdapter sqld = new SqlDataAdapter("select * from student", sqlconn);
            sqld.Fill(ds, "tabstudent");
        }
        GridView1.DataSource = ds.Tables["tabstudent"].DefaultView;
        GridView1.DataBind();
        Label1.Text = "查找成功";

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

你可能感兴趣的:(学习,课本知识)