开发者

Microsoft ReportViewer control (Web) & displaying error messages

开发者 https://www.devze.com 2023-03-28 19:48 出处:网络
I have a Microsoft ReportViewer control on my web page. However, if someone types in an invalid input for one of the parameters then i开发者_开发知识库t displays a rather unfriendly error message wher

I have a Microsoft ReportViewer control on my web page. However, if someone types in an invalid input for one of the parameters then i开发者_开发知识库t displays a rather unfriendly error message where the report should go. For example: The value provided for the report parameter 'pToDate' is not valid for its type. (rsReportParameterTypeMismatch)

The control prompts the user for the information with "To Date" and "pToDate" is the internal name of the parameter. The users won't know this, nor will they likely react well to "rsReportParameterTypeMismatch" (what ever that means!? [while thinking like a user])

As I couldn't find somewhere in the ReportViewer control to put any error or custom message, my solution was to create a label in which to put a more friendly error message. This works insofar as the friendly error message is displayed.

My problem is that once the user has corrected their mistake and clicks "View Report" the report is displayed but the error message is still visible. I've set the label text to string.Empty, I've set the label to Visible = false. I've tried this in various places, ensured the code is hit, but to no avail.

So, is there any way to get custom messages to appear and disappear with a ReportViewer control?


Okay - I've got something that works

Previous I had this:

<asp:Label runat="server" ID="ReportErrorMessage" Visible="false" CssClass="report-error-message">
</asp:Label>

which I was updating in the code behind like this:

ReportErrorMessage.Text = GetErrorMessage(reportException);
ReportErrorMessage.Visible = true;

and then removing like this:

ReportErrorMessage.Visible = false;
ReportErrorMessage.Text = string.Empty;

The latter part didn't work.

I eventually realised that the ReportViewer control is using partial rendering and so wasn't actually changing the label at all (and consdering that, I've still not quite figured out how the initial display actually worked, but anyway...)

The solution was to wrap the label in an update panel like this:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Label runat="server" ID="ReportErrorMessage" Visible="false" CssClass="report-error-message">
        </asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

UPDATE

I've also added a full explanation onto my blog: Friendly Error Messages with Microsoft Report Viewer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号