简体   繁体   English

C#ASP.NET使用来自ASP.NET背后代码的布尔变量

[英]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. 我想做的事情与之类似,除了不显示字符串,而是要将布尔值从后面的代码传递到ASP.NET页,以便其true / false值可以控制“打印”按钮(true / false)。在ReportViewer中。 My Diagnostic works in that it displays the string "True" or "False", which ever is correct. 我的诊断程序的工作原理是显示正确的字符串“ True”或“ False”。 The "ShowPrintButton" and "ShowExportControls" just don't work though and the buttons are not enabled. “ ShowPrintButton”和“ ShowExportControls”只是不起作用,并且按钮未启用。 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: 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. 无需尝试在客户端上执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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