<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>phdesign &#187; asp</title>
	<atom:link href="http://www.phdesign.com.au/tag/asp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phdesign.com.au</link>
	<description>phdesign web and application development by Paul Heasley</description>
	<lastBuildDate>Thu, 08 Jul 2010 00:08:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>No Validation Messages when Disabling &amp; Enabling ASP.Net Validator in Javascript</title>
		<link>http://www.phdesign.com.au/programming/no-validation-messages-when-disabling-enabling-asp-net-validator-in-javascript/</link>
		<comments>http://www.phdesign.com.au/programming/no-validation-messages-when-disabling-enabling-asp-net-validator-in-javascript/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 06:21:31 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://www.phdesign.com.au/?p=152</guid>
		<description><![CDATA[Need to validate a form field based on the value of another field in ASP.Net? The .Net Framework provides a javascript function to do just that: ValidatorEnable(val, enable) This function takes the validator control (val) and a boolean to determine whether it should be enable or disabled. The problem with this function is that it [...]]]></description>
			<content:encoded><![CDATA[<p>Need to validate a form field based on the value of another field in ASP.Net? The .Net Framework provides a javascript function to do just that:</p>
<pre><code>ValidatorEnable(val, enable)</code></pre>
<p>This function takes the validator control (val) and a boolean to determine whether it should be enable or disabled. The problem with this function is that it triggers the validator to validate itself immediately, showing any validation error messages before the user has submitted the form, which may not always be want you want.  The alternative is to set the validator&#8217;s enabled property to trigger this validator to validate itself when the user submits the form, and by using the Page_IsValid variable you can test whether the user has triggered a validation or not, then use ValidatorEnable function only after the validation has occurred when the user expects to see validation messages.<span id="more-152"></span></p>
<p>Consider a scenario where you want the user to enter their address only if they select Australia as the country, you also need to validate the address fields. Two ways to approach this are:</p>
<ol>
<li>For each address field you need to validate use a CustomValidator control, and in the ClientValidationFunction first check to see if the country is set to Australia and if it&#8217;s not then consider any response for this control as valid. The problem with this is you will have to recrete all of the validator functionality in your client validation function, which means more work.</li>
<li>Use standard validator controls for the address fields, and enable or disable them in javascript.  The code you call to enable or disable the validators could be a CustomValidation control on the country field, or a standard javascript call when the user selects a country. If the Page_IsValid variable is set to true then assume that the page has just been loaded and no validation has occured, in this case just set the validator controls enabled property so it will be validated (or not) when the user submits the form.<br />
If Page_IsValid is false then assume the user has tried to submit but failed, in this case we expect validation messages to show / hide as the user changes data on the form &#8211; this is the normal ASP.Net behaviour.</li>
</ol>
<p>Here&#8217;s an example:</p>
<pre><code>// The ClientValidationFunction of a custom validator for country
function validateCountry(source, args) {
    // We we haven't submitted the form yet
    if (Page_IsValid) {
        // Turn the postcode validator on / off, but don't validate
        requiredFieldValidatorPostcode.enabled = enable;
    } else {
        // Turn the postcode validator on / off, and immediately validate
        ValidatorEnable(requiredFieldValidatorPostcode, enable);
    }

    // Validate country is not empty
    args.IsValid = (args.Value.length &gt; 0);
}</code></pre>
<p>Don&#8217;t forget to include server side code to mimic this enabling / disabling of validators. To do this override the Validate() function, enable or disable validators depending on the country selected and call the base.Validate() function.</p>
<pre><code>public override void Validate()
{
    // Check if we need to validate the postcode.
    bool requireAddress = (dropDownListCountry.Text == "Australia");
    requiredFieldValidatorPostcode.Enabled = requireAddress;

    // Call the normal validation methods.
    base.Validate();
}

protected void customValidatorCountry_ServerValidate(object source, ServerValidateEventArgs args)
{
    // Implement the server side custom validation for country.
    args.IsValid = (args.Value.Length &gt; 0);
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phdesign.com.au/programming/no-validation-messages-when-disabling-enabling-asp-net-validator-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a PDF using the .Net ReportViewer control</title>
		<link>http://www.phdesign.com.au/programming/create-a-pdf-using-the-net-reportviewer-control/</link>
		<comments>http://www.phdesign.com.au/programming/create-a-pdf-using-the-net-reportviewer-control/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 23:48:48 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://www.phdesign.com.au/apps/staging/?p=61</guid>
		<description><![CDATA[Need to generate a pdf programmatically in dot net? You could purchase an HTML to PDF converter and worry about getting the page to format nicely for paper, or you could use the freely available ReportViewer control from Microsoft. Microsoft offers SQL Server Reporting Services (SSRS) with SQL Server 2005 and later editions to generate [...]]]></description>
			<content:encoded><![CDATA[<p>Need to generate a pdf programmatically in dot net? You could purchase an HTML to PDF converter and worry about getting the page to format nicely for paper, or you could use the freely available ReportViewer control from Microsoft.</p>
<p>Microsoft offers SQL Server Reporting Services (SSRS) with SQL Server 2005 and later editions to generate and publish reports using the Report Definition Language (RDL), as an alternative to Crystal Reports. SSRS requires a Report Server to publish reports to so they can be useful, however they also offer a ASP.Net or Windows Forms component called the <a title="ReportViewer Controls on MSDN" href="http://msdn.microsoft.com/en-us/library/ms251671(VS.80).aspx">ReportViewer</a> which we can use in our code without the need for any SQL database to generate reports, and these reports can be saved as Excel files, PDF&#8217;s or images.<span id="more-61"></span></p>
<p>The ReportViewer uses a modified version of RDL called RDLC, which does not save any information about the data source along with the report, which makes it perfect for generating reports from objects that aren&#8217;t a database. For instance, I recently needed to generate PDF letters with custom information in them, so this solution proved perfect.</p>
<p>I&#8217;m going to demonstrate how to pass parameters to an ASP.Net page and return a PDF with that information in it, this could just as easily be done in a Windows Forms project. I&#8217;m using Visual Studio 2005, in my preferred language of c#.</p>
<p>Start by creating a new Web Site in Visual Studio. We are going to use a collection of custom business objects as the data source for our report, so add a new class to our project and call it Person.cs. Copy the following code into your Person.cs file.</p>
<pre><code>using System;
using System.Collections.Generic;

/// &lt;summary&gt;
/// The business object we want to use in our report.
/// &lt;/summary&gt;
public class Person
{
    /// &lt;summary&gt;
    /// Some fields to hold information about this person.
    /// &lt;/summary&gt;
    private string _title;
    private string _firstName;
    private string _lastName;

    /// &lt;summary&gt;
    /// The person properties are only able to be set at instantiation.
    /// After the object is created the properties are read only.
    /// &lt;/summary&gt;
    public string Title
    {
        get { return _title; }
    }

    public string FirstName
    {
        get { return _firstName; }
    }

    public string LastName
    {
        get { return _lastName; }
    }

    /// &lt;summary&gt;
    /// The person constructor.
    /// &lt;/summary&gt;
    /// &lt;param name="title"&gt;The person's title&lt;/param&gt;
    /// &lt;param name="firstName"&gt;Their first name&lt;/param&gt;
    /// &lt;param name="lastname"&gt;Their last name&lt;/param&gt;
    public Person(string title, string firstName, string lastname)
    {
        _title = title;
        _firstName = firstName;
        _lastName = lastname;
    }
}

/// &lt;summary&gt;
/// Creates a data source for our report.
/// &lt;/summary&gt;
public class PersonDataSource
{
    /// &lt;summary&gt;
    /// This method creates a new person object and adds them to a new collection that
    /// can be used as a data source for the report.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;A collection of person objects.&lt;/returns&gt;
    public static List&lt;Person&gt; GetPerson(string title, string firstName, string lastName)
    {
        List&lt;Person&gt; people = new List&lt;Person&gt;();
        people.Add(new Person(title, firstName, lastName));
        return people;
    }
}</code></pre>
<p>Now that we have our business object we can create the report. Click add item on the project and select Report. Call this item Report.rdlc.</p>
<p>In the left hand toolbar you should see <em>Website Data Sources</em> (if not select <em>Data &gt; Show Data Sources</em> from the file menu) and in the data sources you should see our Person object. You can now build a report using the fields from our person object and the tools in the toobox. For simplicity just drag some Textboxes onto the report from the toolbox and enter any static text in them that you want, then drag on the Person&#8217;s title, firstName and lastName and arrange them appropriately.</p>
<p><img class="alignnone size-full wp-image-68" title="ReportViewer business object data source" src="http://www.phdesign.com.au/wp-content/uploads/2009/11/report_viewer_demo_1.jpg" alt="ReportViewer business object data source" width="251" height="126" /></p>
<p>Finally we need to write the code which will create the person data source and generate the report. Before we do this we need a reference to the ReportViewer control, right click on the project and <em>Add Reference</em>, in the .Net tab, select <em>Microsoft.ReportViewer.WebForms</em>.Now copy the following code into the Default.aspx source.</p>
<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
&lt;head runat="server"&gt;
	&lt;title&gt;Untitled Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;form id="form1" runat="server"&gt;
	&lt;div&gt;
	&lt;asp:Label ID="lblTitle" runat="server" Text="Title: "&gt;&lt;/asp:Label&gt;
	&lt;asp:TextBox ID="txtTitle" runat="server"&gt;Mr&lt;/asp:TextBox&gt;&lt;br /&gt;
	&lt;asp:Label ID="lblFirstName" runat="server" Text="First Name: "&gt;&lt;/asp:Label&gt;
	&lt;asp:TextBox ID="txtFirstName" runat="server"&gt;John&lt;/asp:TextBox&gt;&lt;br /&gt;
	&lt;asp:Label ID="lblLastName" runat="server" Text="Last Name: "&gt;&lt;/asp:Label&gt;
	&lt;asp:TextBox ID="txtLastName" runat="server"&gt;Smith&lt;/asp:TextBox&gt;&lt;br /&gt;
	&lt;asp:Button ID="Button1" runat="server" Text="Submit" /&gt;&lt;/div&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>And copy the following into the Code View of Default.aspx (Default.aspx.cs).</p>
<pre><code>using System;
using System.Web;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Text;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			LocalReport report = new LocalReport();
			string reportName = "CompletionCertificate";
			string deviceInfo =
				"&lt;DeviceInfo&gt;" +
				"  &lt;OutputFormat&gt;EMF&lt;/OutputFormat&gt;" +
				"  &lt;PageWidth&gt;8.5in&lt;/PageWidth&gt;" +
				"  &lt;PageHeight&gt;11in&lt;/PageHeight&gt;" +
				"  &lt;MarginTop&gt;0.25in&lt;/MarginTop&gt;" +
				"  &lt;MarginLeft&gt;0.25in&lt;/MarginLeft&gt;" +
				"  &lt;MarginRight&gt;0.25in&lt;/MarginRight&gt;" +
				"  &lt;MarginBottom&gt;0.25in&lt;/MarginBottom&gt;" +
				"&lt;/DeviceInfo&gt;";
			Warning[] warnings;
			string[] streamids;
			string mimeType;
			string encoding;
			string extension;

			report.ReportPath = "Report.rdlc";
			report.DataSources.Add(new ReportDataSource("Person", PersonDataSource.GetPerson(txtTitle.Text, txtFirstName.Text, txtLastName.Text)));
			byte[] bytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

			Response.Clear();
			Response.ContentType = mimeType;
			Response.AddHeader("Content-Disposition", "attachment; filename=" + reportName + "." + extension);
			Response.OutputStream.Write(bytes, 0, bytes.Length);
			Response.End();
		}
	}
}</code></pre>
<p>Now run your project, enter some person data into the form and click submit. Open the PDF file and verify that the report is addressed to your person.</p>
<p><a href="http://www.phdesign.com.au/wp-content/uploads/2009/11/ReportViewerDemo.zip"><img class="alignnone size-full wp-image-126" title="Download the source for this tutorial" src="http://www.phdesign.com.au/wp-content/uploads/2009/11/button_download_source.gif" alt="Download the source for this tutorial" width="149" height="25" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phdesign.com.au/programming/create-a-pdf-using-the-net-reportviewer-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
