0

This is my aspx page..................

    <asp:Panel ID="UpdatePanel1" runat="server" Visible="false" >   
           <rsweb:ReportViewer ID="ReportingForPrintingReportViewer" runat="server"
           Width="100%" Height="100%" Font-Names="Verdana" Font-Size="8pt"
           InteractiveDeviceInfos="(Collection)" WaitMessageFont-Names="Verdana"
           WaitMessageFont-Size="14pt">  
           <LocalReport ReportPath="Report.rdlc">
           <DataSources>
           <rsweb:ReportDataSource DataSourceId="ObjectDataSource2" Name="DataSet1" />
           </DataSources>
           </LocalReport>
           </rsweb:ReportViewer>  
           <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
             SelectMethod="GetData" TypeName="DataSet1TableAdapters.tblTotalFeeTableAdapter">
           </asp:ObjectDataSource>
                 </asp:Panel>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="150px"
           onclick="btnSubmit_Click" />
       <asp:Button ID="btnReset" runat="server" Text="Reset" Width="150px" />
       <asp:Button ID="btnCreateBill" runat="server" Text="CreateBill" Width="150px" 
           onclick="btnCreateBill_Click"/>


       <asp:PopupControlExtender ID="btnCreateBill_PopupControlExtender" OffsetX="-1100" OffsetY="115" 
           runat="server" DynamicServicePath="" Enabled="True" ExtenderControlID="" 
           TargetControlID="btnCreateBill" PopupControlID="UpdatePanel1">
       </asp:PopupControlExtender>

This is my cs page

 protected void btnCreateBill_Click(object sender, EventArgs e)
    {
        DisplayReport();
        UpdatePanel1.Visible = true;

    }
    private DataTable TotalInfoData()
    {
        try
        {
            //DataClassesDataContext db = null;
            //db = new DataClassesDataContext();
            //var s = from p in db.tblTotalFeess
            //        where p.Class == ClassDropDownList.SelectedItem.Value && p.StudentID == Convert.ToInt32(StudentNameDropDownList.SelectedValue)
            //        select p;

            //DataTable dt = new DataTable();
            //SQLHelper sqhlpr = new SQLHelper();
            //sqhlpr.SqlText = "select * from tblTotalFee where Class='" + ClassDropDownList.SelectedItem.Value + "'" + "and StudentID='" + StudentNameDropDownList.SelectedValue + "'";
            //DataTable dt = sqhlpr.getDataTable(false);
            //return dt;
            try
            {
                // Open Sql Connection  
                SqlConnection SqlCon = new SqlConnection(@"Data Source=PRATIKPC;Initial Catalog=dbbilling2.0;Integrated Security=True");
                SqlCon.Open();

                // Create a Command  
                SqlCommand SqlComm = new SqlCommand();
                SqlComm.Connection = SqlCon;
                SqlComm.CommandType = CommandType.Text;
                SqlComm.CommandText = "select * from tblTotalFee where Class='" + ClassDropDownList.SelectedItem.Value + "'" + "and StudentID='" + StudentNameDropDownList.SelectedValue + "'";

                // Create instance of Northwind DataSetXSD  
                DataSet1.tblTotalFeeDataTable dtbl = new DataSet1.tblTotalFeeDataTable();

                // Set a Data Commands  
                SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
                SqlDa.Fill(dtbl); // Fill Data in NorthwindDataSet Object.  

                return dtbl;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }  

        } 

        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    private void DisplayReport()
    {
        try
        {
            // Clear the Data Source   
            ReportingForPrintingReportViewer.LocalReport.DataSources.Clear();

            // Set a DataSource to the report  

            // First Parameter - Report DataSet Name  
            // Second Parameter - DataSource Object i.e DataTable  
            ReportingForPrintingReportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", TotalInfoData()));

            // OR Set Report Path  
            ReportingForPrintingReportViewer.LocalReport.ReportPath = HttpContext.Current.Server.MapPath("~/Report.rdlc");

            // Refresh and Display Report  
            ReportingForPrintingReportViewer.LocalReport.Refresh();

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

I have to click btnCreateBill twice to generate report viewer. Why? And how can I generate report viewer in a single click of a button?

4

2 回答 2

0

这是.aspx文件............

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="AccountReportParameter.aspx.cs" Inherits="TexERP.ReportSSRS.AccountReportParameter" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  <asp:UpdatePanel ID="updpnlLiteral" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pnlAddRecord" runat="server" BackColor="White" Height="150px" Width="600px"
                Visible="true">
<td align="right" colspan="4">
                            <asp:Button ID="btnSubmit" runat="server" Text="Show Report" OnClick="btnSubmit_Click"
                                ValidationGroup="FinalSave" />
                            <asp:Button ID="btnCancel" runat="server" Text="Reset" UseSubmitBehavior="false"
                                OnClick="btnCancel_Click" />
            </asp:Panel>
            <rsweb:ReportViewer ID="rptvwMain" runat="server" Width="907px" Height="850px" Font-Names="Verdana"
                Font-Size="8pt" ProcessingMode="Remote" ShowCredentialPrompts="false" InteractiveDeviceInfos="(Collection)"
                ShowZoomControl="true">
            </rsweb:ReportViewer>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

这是cs文件…………

using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
using BusinessAccessLevel;
using BusinessAccessLevel.Masters;
using Microsoft.Reporting.WebForms;

namespace TexERP.ReportCrystal
{
    public partial class AccountReportParameter : System.Web.UI.Page
    {       
        AccountReportBAL objAccountReportBAL;
        clsSession objSession;
        string pstrType;
        protected void Page_Load(object sender, EventArgs e)
        {
            objAccountReportBAL = new AccountReportBAL();
            objSession = new clsSession();

            pstrType = Request.QueryString["Type"];
            if (Session["objSession"] != null)
            {
                objSession = Session["objSession"] as clsSession;               
            }
            objAccountReportBAL = new AccountReportBAL();

            if (!IsPostBack)
            {

                RadDtpFromDate.SelectedDate = Convert.ToDateTime(objSession.FyFromDate);
                RadDtpToDate.SelectedDate = Convert.ToDateTime(objSession.FyToDate);


            }
            rptvwMain.LocalReport.Refresh();
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pstrType;
            pstrType = Request.QueryString["Type"];           
            LoadReport();
        }

        public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
        {
            // local variable for network credential.
            private string _UserName;
            private string _PassWord;
            private string _DomainName;
            public CustomReportCredentials(string UserName, string PassWord, string DomainName)
            {
                _UserName = UserName;
                _PassWord = PassWord;
                _DomainName = DomainName;
            }
            public System.Security.Principal.WindowsIdentity ImpersonationUser
            {
                get
                {
                    return null;  // not use ImpersonationUser
                }
            }
            public System.Net.ICredentials NetworkCredentials
            {
                get
                {
                    // use NetworkCredentials
                    return new NetworkCredential(_UserName, _PassWord, _DomainName);
                }
            }
            public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
            {
                // not use FormsCredentials unless you have implements a custom autentication.
                authCookie = null;
                user = password = authority = null;
                return false;
            }
        }

        void LoadReport()
        {
            string strCompanyName = objSession.SelCompanyName;
            string strHeading = "";
            string strBranchName = objSession.SelBranchName;
            rptvwMain.ProcessingMode = ProcessingMode.Remote;
            rptvwMain.ServerReport.ReportServerCredentials = new CustomReportCredentials(AppConfig.ReportServerUserName, AppConfig.ReportServerPassword, AppConfig.ReportServerDomain);
            string strReportServerUrl = AppConfig.ReportServerUrl + AppConfig.ReportServerFolder;
            rptvwMain.ServerReport.ReportServerUrl = new Uri(strReportServerUrl);
            List<ReportParameter> parameters = new List<ReportParameter>();
            if(pstrType == "GL")
            {
                strHeading = "General Ledger";
                rptvwMain.ServerReport.ReportPath = "/Account/AccountGeneralLedger";
            }

            parameters.Add(new ReportParameter("FyId", Convert.ToInt16(objSession.FyId).ToString()));
            parameters.Add(new ReportParameter("AccountGroupId", cmbAccountGroup.SelectedValue));
            parameters.Add(new ReportParameter("LedgerId", cmbLedgerId.SelectedValue));
            parameters.Add(new ReportParameter("BranchId", Convert.ToInt64(objSession.BranchId).ToString()));
            parameters.Add(new ReportParameter("StDate", Convert.ToDateTime(RadDtpFromDate.SelectedDate).ToString()));
            parameters.Add(new ReportParameter("EnDate", Convert.ToDateTime(RadDtpToDate.SelectedDate).ToString()));
            parameters.Add(new ReportParameter("CompanyName", strCompanyName.ToString()));
            parameters.Add(new ReportParameter("BranchName", strBranchName.ToString()));
            parameters.Add(new ReportParameter("Heading",strHeading.ToString()));
            rptvwMain.ServerReport.SetParameters(parameters);

            rptvwMain.ServerReport.SetDataSourceCredentials(new[] { new DataSourceCredentials() { Name =AppConfig.ReportServerDataSource , UserId = AppConfig.ReportServerDSUserName, Password = AppConfig.ReportServerDSPassword } });
            rptvwMain.ShowZoomControl = true;
            rptvwMain.ServerReport.Refresh();
        }
    }
}

试试这个代码作为参考代码......

于 2013-04-20T11:34:37.880 回答
0

您是否在 !ispostback 内部调用 Get Data 方法(您的数据源加载方法)?

看看这个

protected void Page_Load(object sender, EventArgs e)
{    
    if (!Page.IsPostBack)
        {   
             GetData()// Your Data Source load method
        }
}
于 2013-04-20T07:35:12.700 回答