简体   繁体   中英

C# ASP.NET using Boolean variable from code behind in ASP.NET

I know how to use a String variable from code to behind and say display that string on the web page. What I want to do is similar except that instead of displaying a string, I want to pass the boolean value from code behind, to the ASP.NET page so that its' true / false value can control the Print button (true / false) in the ReportViewer. My Diagnostic works in that it displays the string "True" or "False", which ever is correct. The "ShowPrintButton" and "ShowExportControls" just don't work though and the buttons are not enabled. What do I need to do here? I think the value is being passed but perhaps it's being passed as a string and I need to do something to make it pass as a Boolean....

Here's the code ...

Code Behind:

    //Variables
    public Boolean exportEnabled { get; set; }
    public Boolean printEnabled { get; set; }

    //Page Load
    protected void Page_Load(object sender, EventArgs e)
    {
        // Add a handler for SubreportProcessing
        reportViewerPrintAndExport.LocalReport.SubreportProcessing +=
            new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

        if (!IsPostBack)
        {
            // Display the report
            DisplayReport(Session[SessionKeys.KEY_CERT_NO].ToString(), (CalibrationType)Session[SessionKeys.KEY_CERT_TYPE]);
        }
        DataBind();
    }

    private void DisplayReport(string certNo, CalibrationType calType)
    {
        string[] rolesList = Roles.GetRolesForUser();

        //manage print and export buttons.
        if ((rolesList.Contains("admin")) || (rolesList.Contains("Admin")))
        {
            exportEnabled = true;
            printEnabled = true;
        }
        else if ((rolesList.Contains("Operator")) || (rolesList.Contains("operator")))
        {
            exportEnabled = false;
            printEnabled = false;
        }
    }

aspx:

<!-- DIAGNOSTIC -->
<asp:label runat="server" text="-" /><asp:label runat="server" text="<%# printEnabled %>" /><asp:label runat="server" text="-" />

<asp:Panel ID="ReportPanelPrintAndExport" runat="server" HorizontalAlign="Left">

    <!--Why does this not work? -->
    <rsweb:ReportViewer ShowPrintButton="<%# printEnabled %>" ShowExportControls="<%# exportEnabled %>" ID="reportViewerPrintAndExport" runat="server" Height="100%" Width="100%" 
        ShowBackButton="False" ZoomMode="FullPage" 
        ShowRefreshButton="False" ProcessingMode="Local">
    </rsweb:ReportViewer>

In your code behind simply set that property wherever you want to

    if ((rolesList.Contains("admin")) || (rolesList.Contains("Admin")))
    {
        reportViewerPrintAndExport.ShowPrintButton = true;
        reportViewerPrintAndExport.ShowExportControls = true;
    }
    else if ((rolesList.Contains("Operator")) || (rolesList.Contains("operator")))
    {
        reportViewerPrintAndExport.ShowPrintButton = false;
        reportViewerPrintAndExport.ShowExportControls = false;
    }

There is no need to try to do this on the client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM