Microsoft ReportViewer control (Web) & displaying error messages - asp.net

I have a Microsoft ReportViewer control on my web page. However, if someone types in an invalid input for one of the parameters then it 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

Related

using postbackurl to pass variables without referencing ctl00$MainContent

I'm hoping there's a cleaner way of doing this. My source page markup has some simple inputs and a submit button:
<asp:TextBox runat="server" ID="TBPostDateFrom" placeholder="From" />
<asp:TextBox runat="server" ID="TBPostDateTo" placeholder="Present" />
...
<asp:Button ID="BtnDetailedResults" PostBackUrl="~/Auth/ResultsDetail.aspx" runat="server" Text="View Detailed Results" />
On my target page, I'm trying to reference those controls and use them as datasource select parameters. So far the only way I've found to do that is to use the long asp generated names "ctl00$MainContent$TBPostDateFrom" and "ctl00$MainContent$TBPostDateTo":
SDSDetailedResults.SelectParameters.Add("PDFrom", Request.Form["ctl00$MainContent$TBPostDateFrom"]);
SDSDetailedResults.SelectParameters.Add("PDTo", Request.Form["ctl00$MainContent$TBPostDateTo"]);
Is there a way I can reference those controls without using the long ct100$...? Or a way to reference the controls directly? I'm guessing if sometime down the road I change my master page, or content controls, these references would get messed up.
I've tried adding using adding the ClientIDMode=Static to the inputs like:
<asp:TextBox runat="server" ID="TBPostDateFrom" placeholder="From" ClientIDMode="Static" />
But that appears to only change the ID. On my target page, I'm still unable to reference it without using the ct100$....
I've also tried using the Page.PreviousPage method, but the objects end up empty:
if (Page.PreviousPage != null)
{
//post date
TextBox PostDateFrom = (TextBox)Page.PreviousPage.FindControl("TBPostDateFrom");
TextBox PostDateTo = (TextBox)Page.PreviousPage.FindControl("TBPostDateTo");
//at this point both PostDateFrom and PostDateTo are empty, if I do this:
SDSDetailedResults.SelectParameters.Add("PostDateFrom", PostDateFrom.Text);
SDSDetailedResults.SelectParameters.Add("PostDateTo", PostDateTo.Text);
// I get an IIS error saying the object references dont' exist, or are null
}
}
Thanks in advance, any help or guidance is much appreciated!
For a search page, I would recommend using the QueryString to pass information to your later page, rather than trying to reference the controls from the previous page.
This will be especially useful if you want to use this functionality from different technologies. You won't have to worry about where the request came from.
SearchPage.aspx:
//Button Click:
var page = "ResultsDetail.aspx";
var url = String.Format("{0}?TBPostDateFrom={1}&TBPostDateTo={2}", page, TBPostDateFrom.Text, TBPostDateTo.Text);
Response.Redirect(url);
ResultDetails.aspx:
var from = DateTime.Parse(Request.QueryString["TBPostDateFrom"]);
var to = DateTime.Parse(Request.QueryString["TBPostDateTo"]);
//Do search based on parameters
For more information: MSDN - How to: Pass Values Between ASP.NET Web Pages

Read validation control error messages from local resource files

I need to read error message of validation controles (RequiredFeildValidator) from resource files in my App_LocalResource folder as my web app is multilingual....
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="ddlTicketType" ErrorMessage="Ticket type required."
ForeColor="White" InitialValue="Select" SetFocusOnError="True"
ValidationGroup="tkt" meta:resourcekey="RequiredFieldValidator4Resource1">*</asp:RequiredFieldValidator>
and the key "RequiredFieldValidator4Resource1" is existed in resource file as some text...but it is not picking up the exact value, rather it is showing some unreadable content......
I changed my ValidationSummary to ShowMessageBox="False" & ShowSummary="True" and it works, it showed me desired result.....i want them to be work in Message Box too.....
Every other thing are working fine, like text in labels and in other controles like buttons, hyper links are coming correctly.....
after scracthing my head for so many days, finally i found answer for this...the javascript alert and validation control message box uses your local computer language & cultural...i have enables it for the culture i want and it worked like a charm...thanks

Strange behaviour of asp.net button on Firefox of pressing twice

If I have this asp.net button:
<asp:Button ID="Button_Save" Width="150px" OnClick="Button_Save_Click" runat="server" Text="Save" />
In order to prevent the user to press the button twice to insert two data records, I added this in the code behind:
Button_Save.Attributes.Add("onclick", "this.disabled='true';" + ClientScript.GetPostBackEventReference(Button_Save_Data, null) + ";");
It works fine with IE and Chrome. However, in Firefox, every time the user presses the button once, I got two data records inserted.
After some time googling, I modified the button this way by adding: UseSubmitBehavior="false":
<asp:Button ID="Button_Save" Width="150px" UseSubmitBehavior="false" OnClick="Button_Save_Click" runat="server" Text="Save" />
Then it also works with Firefox, only one data record inserted. But then I will have to add that setting for every button on my web application. It requires a lot of work.
However, I think this is really a big problem if that is the case always with asp.net app running on Firefox. Or did I implement something wrong?
Thanks in advance.
You would be better off adding checks to your SAVE code to prevent duplicate POSTs. I've used code like yours and still had duplicate records because someone hit the Refresh button at the wrong time.
EDIT:
Here is the code I use. Key is to match a value on the server with a value coming back in the postback.
In Page_Load:
If Not IsPostBack Then
Session("rcdupdate") = Server.UrlEncode(System.DateTime.Now.ToString)
End If
In Page_Prerender
ViewState("rcdupdate") = Session("rcdupdate")
And in the Save routine
If Session("rcdupdate").ToString = ViewState("rcdupdate").ToString Then
... write the data to the database ...
Session("rcdupdate") = Server.UrlEncode(System.DateTime.Now.ToString)
End If
You can extend asp:button and add your new functionality, then fine/replace all of your asp:buttons. Going forward you can then change all your buttons in once place.

Use HtmlEncode in Details View TemplateItem Control

I have details view control in my asp.net web form, which on of its item template gets it is value from database, and show this into a richtextbox :
<FTB:FreeTextBox id="txtDescription" runat="Server" AllowHtmlMode="false" Text='<%# (Eval("Description") )%>'
>
</FTB:FreeTextBox>
but when i click on insert or update button, i get the following error :
A potentially dangerous Request.Form value was detected from the client ....
i tried this :
Text='<%# HttpUtility.HtmlDecode((string)Eval("Description"))%>'
bu it did not work ethier, and i got the error again.
is there any way except turning validateRequest off.
Would you please help me?
No, there isn't a way to get this to work aside from turning Validate Request off. Which isn't a bad thing if you write your database functionality correctly and implement strict custom form validation.

asp.net validation control causing "random" error in javascript

I have an asp:FormView on a control (in an ascx file) which is loaded onto a portal page along with some other controls.
I need to add validation to the form, but when I add any of the validation controls I get an apparently random error in the generated JavaScript when the page is loaded, and the validation doesn't fire when I need it to.
This is the code that I've got:
<asp:TextBox ID="FPITextBox4" runat="server" Text='<%# Bind("SomeNumber") %>'></asp:TextBox>
<asp:RangeValidator ID="RangeValidator4" runat="server" ControlToValidate="FPITextBox4" Text="*"
ErrorMessage="The number must be a whole number between 0 and 100,000" Type="Integer"
MaximumValue="100000" MinimumValue="0" ToolTip="Must be between 0 and 100,000"></asp:RangeValidator>
This is the error I get when the page is loaded:
Message: Expected ';'
Line: 1159
Char: 60
Code: 0
When I look at line 1159 in the generated code it looks like this:
var ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4 = document.all ?
document.all["ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4"] :
document.getElementById("ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-
47aa-a089-fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4");
The code immediately above this is:
<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById(
"ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d
-47aa-a089-fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4"));
//]]>
</script>
The same code works when the control is just instantiated onto a test page all by itself.
It appears that the error only happens in IE 8. I've tested the same page in Firefox 3.6 and it all works as expected.
Correction
It appears that the validation is working. I had a ValidationSummary control set to show a message box and that wasn't appearing which led me to believe that the validation wasn't firing. When I changed that to ShowSummary="true" ShowMessageBox="false" the error message did appear and the data isn't saved.
So all it means now is that I've got a random script error that (at the moment) doesn't appear to be affecting the workings of the page.
However, I would still like to remove the error - just in case it's hiding something else.
you really found a bug in .Net client generation code - have a look at
http://haacked.com/archive/2006/07/14/ASP.NET2.0ClientValidationJavascriptBug.aspx
But no resolution from M$ - you'll have to generate IDs in a different way...
I've found that this type of error results because of an error in one of my scripts, where there is a missing ;. Do you have JS code in your page and is all of that correct, no missing semi-colons?
HTH.

Resources