asp.net总结(5)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default7 : System.Web.UI.Page
{
    string ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa ;Password=sa ;Initial Catalog=test1;Data Source=192.168.10.250";
    string[] ClassNameArray = { "大学", "中学", "高中" };
    public string aa;

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScriptBlock();
        if (!IsPostBack)
        {
           dataviewbind();
          //dataviewbind1();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {   

      System.Data.Common.DbDataRecord db = (System.Data.Common.DbDataRecord)e.Row.DataItem;

            TextBox ddt = (TextBox)e.Row.FindControl("Textbox2");
            ddt.Text = (string)db["age"];
   //此处由于我是通过OleDbDataReader 作为数据源,这样的话我在绑定事件中要对控件赋值的话,可以利用
db["age"];来得到字段的值

              
             //DataRowView db = (DataRowView)e.Row.DataItem;
            //DataRow myDataRow = db.Row;//可以不写这一步,DataRowView与DataRowView.row的区别是效率的问题
TextBox ddt = (TextBox)e.Row.FindControl("Textbox2");
            ddt.Text = (string)db["age"];
   //此处由于我是通过DataView作为数据源,这样的话我在绑定事件中要对控件赋值的话,可以利用
db["age"];来得到字段的值


            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            Button ddb = (Button)e.Row.FindControl("Button1");
            ddb.Text = (string)DataBinder.Eval(e.Row.DataItem,"sex");//后台同样可以用Eval进行绑定
            ddb.Attributes["OnClick"] = " alert('" + ddb.Text +"');";
            ddl.Attributes["OnChange"] = " funStr('" + ddl.ClientID + "');";//像这种增加客户端的事件,经过我的测试,必须加在_RowDataBound事件中才能起到作用
            ddl.DataSource = ClassNameArray;
            ddl.DataBind();
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            ddl.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);//像这种动态的给服务器控件增加事件,必须写在_RowCreated事件中才能起到作用
        }
      
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl=(DropDownList)sender;
        Response.Write(ddl.SelectedItem.Text);
    }

    protected void ClientScriptBlock() {
        ClientScriptManager CSM = Page.ClientScript;
        String script1 ;
        script1 = @"<script language='javascript'>
                  function funStr(obj){
                  alert(document.getElementById(obj).selectedIndex);
                 
                  }
                  </script>";                                   
        if (!CSM.IsClientScriptBlockRegistered("clientScript"))
        {
            CSM.RegisterClientScriptBlock(this.GetType(),"clientScript", script1);
        }
    
    }

    private void dataviewbind()
    {
        System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(ConnectionString);
        cn.Open();
        string sql = "SELECT * From worker  order by name";
        System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, cn);
          System.Data.OleDb.OleDbDataReader dr= cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);


        GridView1.DataSource = dr;

        GridView1.DataBind();
        dr.Close();
        cmd.Dispose();
        cn.Dispose();
        cn = null;
    }

    private void dataviewbind1()
    {
        System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(ConnectionString);
        cn.Open();
        string sql = "SELECT * From worker  order by name";
        System.Data.DataSet ds = new System.Data.DataSet();
        System.Data.OleDb.OleDbDataAdapter da=new System.Data.OleDb.OleDbDataAdapter(sql,cn);
        da.Fill(ds, "DS");
        DataView dvwGrid   = ds.Tables[0].DefaultView;
        //GridView1.DataSource =
dvwGrid  ;//05将这样的绑定后的dataitem为datarowview类型,
需要经过上面的转换

        GridView1.DataBind();
        da.Dispose();
        ds.Dispose();
        cn.Dispose();
        cn = null;
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值