You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.7 KiB
C#

12 months ago
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DSWeb.DataAccess;
namespace DSWeb
{
public partial class wucGridView : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["OrderDire"] = "ASC";
}
}
public void gvBind(string strSql)
{
string od = "";
if (ViewState["SortOrder"] == null)
{
od = "";
}
else
{
od = " order by " + ViewState["SortOrder"].ToString().Trim() + " " + ViewState["OrderDire"];
}
//
ViewState["gvBind"] = strSql;
if (od.Trim() != "")
{
strSql += od;
}
DataSet ds = SqlHelper.OpenSqlDataSet(SqlHelper.ConnectionStringLocalTransaction, strSql);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortOrder"] = e.SortExpression;
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
//
if (ViewState["gvBind"] != null)
{
gvBind(ViewState["gvBind"].ToString());
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//表头
if (e.Row.RowType == DataControlRowType.Header)
{
//单元格变色
foreach (TableCell c in e.Row.Cells)
{
c.Attributes["onmouseover"] = "javascript:bgcolor=this.style.backgroundColor;this.style.backgroundColor='#CDE0FA';";
c.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=bgcolor;";
}
e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;
e.Row.Cells[1].Visible = false;
}
//行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#A7CDF0';");//,this.style.fontWeight='Bold';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
//设置悬浮鼠标指针形状为"小手"
//e.Row.Attributes["style"] = "Cursor:hand";
//单击行选中变色
e.Row.Attributes.Add("onclick ", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#A7CDF0';window.oldtr=this;");//字体变色this.style.color='#FFFFFF';
//e.Row.ForeColor = System.Drawing.Color.White;
//双击该行,弹出详细的信息页面
string strGID = e.Row.Cells[1].Text;
string strHandle = e.Row.Cells[e.Row.Cells.Count - 1].Text.Replace("&","&");
//string ID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes.Add("OnDblClick", "return openLink('" + strGID + "','" + strHandle + "')");
e.Row.ToolTip = "双击该行,显示更详细的信息。";
//e.Row.BackColor = System.Drawing.Color.FromName("#FFE4B5");
// 为 CheckBox 添加 Client 的 onClick 事件, 实现按"Shift" + 鼠标左键 实现多选
((CheckBox)e.Row.FindControl("chkSelected")).Attributes.Add("onclick", "checkboxClick(this);");
e.Row.Attributes.Add("onclick", "selectRow('" + strGID + "')");
e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;
e.Row.Cells[1].Visible = false;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//
if (ViewState["gvBind"] != null)
{
gvBind(ViewState["gvBind"].ToString());
}
}
}
}