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;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindToDropDownList();//绑定DropDownList
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
this.BindToGridView(id); //绑定GridView
}
}
//绑定到 DropDownList
private void BindToDropDownList()
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn1"]);
SqlDataAdapter sda = new SqlDataAdapter("select ProductID,ProductName from products", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "ProductName";
this.DropDownList1.DataValueField = "ProductID";
this.DropDownList1.DataBind();
}
//绑定到GridView
private void BindToGridView(string id)
{
this.GridView1.AllowPaging=true;//允许分页
this.GridView1.AllowSorting = true;//允许排序
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn1"]);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from [Order Details]where ProductID=" + id, conn);
DataSet ds = new DataSet();
sda1.Fill(ds);
//如果不排序用下面的绑定方法就可以了
//this.GridView1.DataSource = ds;
//this.GridView1.DataBind();
//如果要排序采用下面的梆定方法
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
//回调数据库
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
this.BindToGridView(id);
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
this.GridView1.PageIndex = e.NewPageIndex;
this.BindToGridView(id);
}
//行变色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");//结合前台html代码
e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);");
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = "asc";
}
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
this.BindToGridView(id);//重新绑定
}
}
前台Javascript:
<script language="javascript" type="text/javascript">
var _oldColor;
function SetNewColor(source)
{
_oldColor=source.style.backgroundColor;
source.style.backgroundColor='#008600';
}
function SetOldColor(source)
{
source.style.backgroundColor=_oldColor;
}
</script>
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/zhouyanlong/archive/2006/09/27/1295392.aspx