首页 > 解决方案 > 如何根据条件更改 RDLC 报告中文本框的文本?

问题描述

我有一份RDLC报告。该报告中有一个显示文本,TextBox如. 现在我想根据某些条件更改该文本框的文本。就像当我单击按钮时,文本应更改为“雇用的员工记录”,而不是当我单击按钮时,文本应更改为“拒绝的员工记录”。这是我发送的报告页面的加载事件的条件TextBox2All Employees RecordHired EmployeesTextBox2All Employees RecordRejected EmployeesTextBox2

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (EmployeeID.PrintAllEmployees == "ALL EMPLOYEES")
            {
                PrintAllEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "HIRED EMPLOYEES")
            {
                PrintHiredEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "REJECTED EMPLOYEES")
            {
                PrintRejectedEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "UNVERIFIED EMPLOYEES")
            {
                PrintUnverifiedEmployeesMethod();
            }
            else
            {
                //SOMETHING
            }
        }
    }

这是图片 在此处输入图像描述

当第二个条件返回 true 时,texbox 文本更改为Hired Employees Record,依此类推....

我的第二个问题是,在报告中,只有第一页有标题文本,其余页面没有标题文本。如何做到这一点?请帮我。

标签: c#asp.netrdlcdynamic-rdlc-generation

解决方案


几天前我在这里问了一个问题,但我没有得到答案。所以我一直在寻找并解决了我的问题。所以我想回答这个问题,希望如果有人被困在这里,那么有人会从我的回答中得到帮助。毕竟我们必须互相帮助。所以一步一步来。

1)我通过右键单击添加一个Parameter来自工具箱并添加参数并命名它。 Report DataparamHeader在此处输入图像描述

在此处输入图像描述

在此处输入图像描述 2)我添加一个TextboxinReport .rdlc DesignDrag and Drop paramHeaderinto Textbox

3)然后我在我的方法中添加以下C#代码。PrintReport

ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptAllEmployeeRecord.rdlc");
        //Passing Parameter
        ReportParameterCollection reportParameters = new ReportParameterCollection();
        reportParameters.Add(new ReportParameter("paramHeader", "HIRED EMPLOYEES REPORT"));
        this.ReportViewer1.LocalReport.SetParameters(reportParameters);

ParamHeader是我在第一步中添加的参数名称,HIRED EMPLOYEES REPORT是我想要在第二步中添加的字符串值TextBox

所以我的整体方法看起来像这样

public void PrintHiredEmployeesMethod()
    {

        //set Processing Mode of Report as Local  
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        //set path of the Local report  
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptAllEmployeeRecord.rdlc");
        //Passing Parameter
        ReportParameterCollection reportParameters = new ReportParameterCollection();
        reportParameters.Add(new ReportParameter("paramHeader", "HIRED EMPLOYEES REPORT"));
        this.ReportViewer1.LocalReport.SetParameters(reportParameters);
        //creating object of DataSet dsEmployee and filling the DataSet using SQLDataAdapter  
        DataSetAllEmployee dsemp = new DataSetAllEmployee();
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {
            SqlCommand cmd = new SqlCommand(@"SELECT * FROM TableEmployee WHERE Status=@Status", con);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.AddWithValue("@Status","HIRED");
            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            adapt.Fill(dsemp, "dtAllEmployeeRecord");
        }
        //Providing DataSource for the Report  
        ReportDataSource rds = new ReportDataSource("DataSetAllEmployee", dsemp.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        //Add ReportDataSource  
        ReportViewer1.LocalReport.DataSources.Add(rds);
    }

我的Page_Load活动看起来像这样

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (EmployeeID.PrintAllEmployees == "ALL EMPLOYEES")
            {
                PrintAllEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "HIRED EMPLOYEES")
            {
                PrintHiredEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "REJECTED EMPLOYEES")
            {
                PrintRejectedEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "UNVERIFIED EMPLOYEES")
            {
                PrintUnverifiedEmployeesMethod();
            }
            else
            {
                //SOMETHING
            }
        }
    }

请注意,我有四种不同的方法,但我正在Header根据需要更改部分。就像当用户想要打印时HIRED EMPLOYEES REPORT,然后Header Section显示HIRED EMPLOYEES REPORT,如果用户想要生成ALL EMPLOYEES REPORT,那么Header Section应该显示ALL EMPLOYEES REPORT等等..


推荐阅读