ASP 報表

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>
                    &nbsp;
                    <label>
                        結束時間:</label>
                    <asp:TextBox ID="tbxToDateTime" runat="server" Width="120"></asp:TextBox>
                    &nbsp;
                    <asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" />
                    &nbsp;
                    <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();
        }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章