ASP界面代码:
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<asp: ToolkitScriptManager ID="ScriptManager1" runat="server" /><asp: UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<script type="text/javascript">
var prevselitem = null;
function selectx(row) {
if (prevselitem != null) {
prevselitem.style.backgroundColor = '#ffffff';
}
row.style.backgroundColor = 'PeachPuff';
prevselitem = row;
}
</script>
<p align="center">
<asp:Label ID="lbTitle" runat="Server" CssClass="lbTitle" Text="报表 "></asp:Label>
</p>
<div align="center">
<p>
<label>
开始时间:</label>
<asp:TextBox ID="tbxFromDateTime" runat="server" Width="120"></asp:TextBox>
<label>
结束时间:</label>
<asp:TextBox ID="tbxToDateTime" runat="server" Width="120"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" />
<asp:Button ID="btnDefault" runat="server" Text="默认" OnClick="btnDefault_Click" />
<asp:Button ID="btnReport" runat="server" Text="导出报表" OnClick="btnReport_Click" />
</p>
<script type="text/javascript">
$(function () {
bindEvent();
});
bindEvent = function () {
$('#MainContent_tbxFromDateTime').datetimepicker();
$('#MainContent_tbxToDateTime').datetimepicker();
}
</script>
</div>
<asp: GridView ID="GridView1" runat="server" AllowSorting="false" PageSize="18" AllowPaging="True"
CellSpacing="1" CellPadding="2" Font-Size="9pt" _disibledevent="GridView1_RowDataBound"
ForeColor="Black" GridLines="None" OnRowDataBound="GridView1_RowDataBound1" OnPageIndexChanging="GridView1_PageIndexChanging"
Width="630px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
HorizontalAlign="Center">
<FooterStyle BackColor="Tan" />
<RowStyle HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<SelectedRowStyle BackColor="DarkSlateBlue" CssClass="SelectedNodeStyle" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<PagerTemplate>
当前第:
<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
页/共:
<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
页
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
转到第
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
CommandName="Page" Text="GO" />
</PagerTemplate>
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
<p>
</p>
</ContentTemplate>
< Triggers>
<asp:AsyncPostBackTrigger ControlID="btnDefault" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
<asp:PostBackTrigger ControlID="btnReport" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
ASP后台代码:
int GetSortColumnIndex()
{
foreach (DataControlField field in GridView1.Columns)
{
if (field.SortExpression == GridView1.SortExpression)
{
return GridView1.Columns.IndexOf(field);
}
}
return -1;
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
}
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");//点击行变色
e.Row.Attributes["style"] = "Cursor:hand"; //设置悬浮鼠标指针形状为"小手"
for (int i = 1; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Text = Convert.ToDouble(e.Row.Cells[i].Text).ToString("F2");
}
// GridView1.HeaderRow.Cells[10].Visible = false;
// e.Row.Cells[10].Visible = false;//隐藏选择按钮
//String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select<# WebPartBody #>quot; + e.Row.RowIndex.ToString());
// e.Row.Attributes.Add("onclick", evt);//执行选择行GridView1_SelectedIndexChanged事件
}
}
protected void GridViewToExcel(GridView gv, string FileName) //导出到Excel
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// 得到该控件
GridView theGrid = sender as GridView;
int newPageIndex = 0;
if (e.NewPageIndex == -3)
{
//点击了Go按钮
TextBox txtNewPageIndex = null;
//GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (pagerRow != null)
{
//得到text控件
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
}
if (txtNewPageIndex != null)
{
//得到索引
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
}
}
else
{
//点击了其他的按钮
newPageIndex = e.NewPageIndex;
}
//防止新索引溢出
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
//得到新的值
theGrid.PageIndex = newPageIndex;
//重新绑定
bingDesignatioonName();
}
protected void bingDesignatioonName() //绑定数据
{
DateTime fromDateTime = DateTime.Parse(tbxFromDateTime.Text);
DateTime toDateTime = DateTime.Parse(tbxToDateTime.Text);
if (fromDateTime > toDateTime)
{
toDateTime = fromDateTime.AddDays(1);
tbxToDateTime.Text = toDateTime.ToString("MM/dd/yyyy HH:mm");
}
GetSQLCmd(Session["RealReportid"].ToString(), fromDateTime.ToString(), toDateTime.ToString());
bindEvent();
}