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>
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/zhouyanlong/archive/2006/09/27/1295392.aspx