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;
}
}
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;
}
}