How to manage multilanguage Web Forms solutions in Visual Studio .NET or Visual Studio 2005

Article translations Article translations
Article ID: 315903 - View products that this article applies to.
This article was previously published under Q315903
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

SUMMARY

This article demonstrates how to use an XML data-island and the .NET Xml Web server control to create a small, multilanguage Web Application project that displays Extensible Markup Language (XML) data. The main application is built in Visual Basic .NET or Visual Basic 2005. The application uses a Visual C# Class Library component to illustrate multilanguage references and implementation inheritance.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows XP Professional, or Microsoft Windows XP Server with the Microsoft .NET Framework and Microsoft Visual Studio .NET installed
This article assumes that you are familiar with the following topics:
  • Implementation inheritance
  • XML and Extensible Stylesheet Language Transformation (XSLT)
  • Microsoft .NET Web server controls

Create a Multilanguage ASP.NET Web Application

In the following steps, you create a small ASP.NET Web Application project that allows a customer to select lunch menu items based on calorie count. To do this, the sample uses the Xml Web server control, a simple XML document that contains the menu data, and an XSL stylesheet to transform the XML data. The XsltArgumentList class from the System.Xml.Xsl namespace allows you to use parameters in the transformation.
  1. Create a Visual Basic Windows Application project as follows:
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click New Project.
    2. Under Project Types, click Visual Basic Projects. Under Templates, click ASP.NET Web Application. Rename the project Menu, and then click OK.

      Note In Visual Studio 2005, click Visual Basic under Project Types.
  2. Make sure that the Solution Explorer window is visible. If it is not, press the CTRL+ALT+L key combination.
  3. You now have the shell of a Visual Basic .NET or Visual Basic 2005 Web Application project within a solution named Menu. To make this a multilanguage solution, you must add a Visual C# Class Library.

    In the Solution Explorer window, right-click Menu, point to Add, and then click New Project. Under Project Types, click Visual C# Projects. Under Templates, click Class Library. Keep the default name, ClassLibrary1. Click OK to add this Class Library to the solution.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  4. Class1.cs appears in the Editor window. Add the following namespace declaration to the top of this window:
    using System.Xml.Xsl;
    					
  5. Under public Class1, add the following code:
    public XsltArgumentList CreateArgumentList(
    string ParamName, string NamespaceURI, string ParamValue)
    {
     XsltArgumentList tal = new XsltArgumentList();
     tal.AddParam(ParamName, NamespaceURI, ParamValue);
     return tal;
    }
    					
  6. You now have a Visual C# class that takes XSLT parameter information and that returns an instance of the XsltArgumentList class. You can now build the Visual Basic application that will consume this component.

    WebForm1.aspx should already be open in Design View. If not, double-click WebForm1.aspx in the Solution Explorer window. Click the Design tab below the Editor window to switch to Design View.
  7. Press the F4 key to access the property page. Change the pageLayout property to FlowLayout.
  8. On the Web Form, type Maximum Calories: .
  9. Add a TextBox control, a Button control, and an Xml control to the form as follows:
    1. Press the CTRL+ALT+X key combination to open the toolbox.
    2. In the toolbox, click Web Forms.
    3. Drag a TextBox control and a Button control from the toolbox, and drop these controls onto the Web Form.
    4. Press F4 to access the property page for the Button control.
    5. Change the Text property to Filter Menu.
    6. Position the cursor after the button, and then press ENTER to position the cursor for the Xml control.
    7. Drag an Xml Web server control from the toolbox, and drop this control onto the Web Form.
    8. Press F4 to access the property page of the Xml control.
    9. Change the DocumentSource property to Menu.xml, and then change the TransformSource property to Menu.xslt.
  10. Double-click Filter Menu to create a Click event handler in the code-behind class of the Web Form. Before you add code to the handler, you must add a reference to the Visual C# Class Library as follows:
    1. In the Solution Explorer window, under ClassLibrary1, right-click References, and then click Add Reference.
    2. On the Projects tab, double-click ClassLibrary1. Notice that ClassLibrary1 appears in the Selected Components list box.
    3. Click OK. Notice that ClassLibrary1 appears under References in the Web Application.
  11. Add the following code to the Click event handler:
    Dim myArgList As New ClassLibrary1.Class1()
    Xml1.TransformArgumentList = myArgList.CreateArgumentList("calories", "", TextBox1.Text)
    Xml1.Visible = True
    						
    The argument list that the Visual C# component returns allows you to pass the value of the TextBox control as a parameter to the stylesheet. This stylesheet uses XSLT commands to filter out all lunch menu items that exceed the calorie count that the customer types. The following line of code from the stylesheet illustrates how the parameter is received and used:
    <xsl:for-each select="lunch-menu/food[calories <= $calories]">
    					
  12. Use the code from the Menu.xml and the Menu.xslt sections to create the Menu.xml and Menu.xslt files. In the Solution Explorer window, right-click the Menu project, and then add the .xml and .xslt files.

Complete Code Listing

Menu.xml

<?xml version='1.0'?>
<lunch-menu>
  <food>
    <name>Cheese Pizza</name>
    <price>$6.95</price>
    <description>Individual deep-dish pizza with lots of mozzarella cheese</description>
    <calories>800</calories>
  </food>
  <food>
    <name>Pepperoni Pizza</name>
    <price>$7.95</price>
    <description>Individual deep-dish cheese pizza with thick-cut pepperoni slices</description>
    <calories>950</calories>
  </food>
  <food>
    <name>The "Everything" Pizza</name>
    <price>$9.95</price>
    <description>Individual deep-dish pizza with all our toppings. House specialty!</description>
    <calories>800</calories>  </food>
  <food>
    <name>Hungarian Ghoulash</name>
    <price>$4.50</price>
    <description>Large serving in a sourdough bread bowl. A_local delight!</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Maisey's Pork Sandwich</name>
    <price>$6.95</price>
    <description>A fresh pork fillet, deep-fried to perfection. Served with fries.</description>
    <calories>950</calories>
  </food>
</lunch-menu>
				

Menu.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="calories">1500</xsl:param>
  <xsl:template match="/">
    <HTML>
      <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt;
            background-color:#EEEEEE">
        <xsl:for-each select="lunch-menu/food[calories <= $calories]">
          <DIV STYLE="background-color:blue; color:white; padding:4px">
            <SPAN STYLE="font-weight:bold; color:white"><xsl:value-of select="name"/></SPAN>
            - <xsl:value-of select="price"/>
          </DIV>
          <DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt">
            <xsl:value-of select="description"/>
            <SPAN STYLE="font-style:italic">
              (<xsl:value-of select="calories"/> calories per serving)
            </SPAN>
          </DIV>
        </xsl:for-each>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>
				

WebForm1.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="menu.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body >

    <form id="Form1" method="post" runat="server">
<P>Maximum Calories: 
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Filter menu"></asp:Button></P>
<P>
<asp:Xml id="Xml1" runat="server" DocumentSource="menu.xml" TransformSource="menu.xslt"></asp:Xml></P>

    </form>

  </body>
</HTML>
				

Class1.cs Non-Inheritance Example

using System;
using System.Xml.Xsl;

namespace ClassLibrary1
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1
    {
        public XsltArgumentList CreateArgumentList(
        string ParamName, string NamespaceURI, string ParamValue)
        {
            XsltArgumentList tal = new XsltArgumentList();
            tal.AddParam(ParamName, NamespaceURI, ParamValue);
            return tal;
        }
    }
}
				

WebForm1.vb Inheriting System.Web.UI.Page

Imports System.Xml.Xsl

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Xml1 As System.Web.UI.WebControls.Xml

#Region " Web Form Designer Generated Code "

    'The Web Form Designer requires this call.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: The Web Form Designer requires this method call.
        'Do not use the Code editor to modify it.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        'Insert user code to initialize the page here.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim tal As New XsltArgumentList()
        tal.AddParam("calories", "", TextBox1.Text)
        Xml1.TransformArgumentList = tal
        Xml1.Visible = True
    End Sub
End Class
				

Class1.cs Inheritance Example

using System;
using System.Xml.Xsl;

namespace ClassLibrary1
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1 : System.Web.UI.Page
    {
        public XsltArgumentList CreateArgumentList(
        string ParamName, string NamespaceURI, string ParamValue)
        {
            XsltArgumentList tal = new XsltArgumentList();
            tal.AddParam(ParamName, NamespaceURI, ParamValue);
            return tal;
        }
    }
}
				

WebForm1.vb Inheriting ClassLibrary1

Imports System.Xml.Xsl

Public Class WebForm1
    Inherits ClassLibrary1.Class1
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Xml1 As System.Web.UI.WebControls.Xml

#Region " Web Form Designer Generated Code "

    'The Web Form Designer requires this call.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: The Web Form Designer requires this method call.
        'Do not use the Code editor to modify it.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        'Insert user code to initialize the page here.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Xml1.TransformArgumentList = CreateArgumentList("calories", "", TextBox1.Text)
        Xml1.Visible = True
    End Sub
End Class
				

Verify That It Works

  1. Press the F5 key to run the application in debug mode.
  2. The entire menu appears by default. Take note of the calorie counts for each item.
  3. In the text box, type a number of calories, and then click Filter Menu. Notice that only those menu items that are less than or equal to this calorie amount appear.
  4. Press the SHIFT+F5 key combination to stop debugging and to return to Visual Studio.

Steps to Demonstrate Multilanguage Inheritance

To illustrate multilanguage inheritance, you must modify the Web Application project. Instead of creating an instance of the Class Library component, the application inherits from the Visual Basic .NET Web Form code-behind class.

The code-behind class currently inherits the System.Web.UI.Page class. Because Microsoft .NET does not support multiple inheritance, you must move Page class inheritance to the Visual C# class.
  1. Add a reference to the System.Web namespace in the Class Library before you modify the Visual C# code.
  2. In the Solution Explorer window, under ClassLibrary1, right-click References, and then click Add Reference.
  3. On the .NET tab, double-click System.Web.dll. Notice that System.Web.dll appears in the Selected Components list box.
  4. Click OK. Notice that System.Web appears under References in your Class Library.
  5. Modify the Class1 declaration to inherit the Page class:
    public class Class1 : System.Web.UI.Page
    					
  6. The Visual Basic .NET or Visual Basic 2005 code-behind class is now ready to inherit Class1. Because Class1 inherits the Page class, the code-behind class also inherits the Page class through its inheritance of Class1.

    In the Editor window, click WebForm1.aspx.vb. Under the class declaration, replace System.Web.UI.Page with ClassLibrary1.Class. The updated code should appear as follows:
    Inherits ClassLibrary1.Class1
    					
  7. Comment out the first line in the Click event handler as follows:
    'Dim myArgList As New ClassLibrary1.Class1()
    					
  8. Modify the second line in the Click event handler by calling CreateArgumentList without an object reference:
    Xml1.TransformArgumentList = CreateArgumentList("calories", "", TextBox1.Text)
    					
  9. Repeat the steps in the Verify That It Works section.

Troubleshooting

  • When you work with two languages, it is easy to confuse syntax. Visual C# syntax does not allow a wide margin of error. Furthermore, Visual C# .NET error messages are not always as prompt or as intuitive as Visual Basic .NET error messages. To troubleshoot these difficulties, refer to the "Language Equivalents" section of the Microsoft .NET Framework Software Development Kit (SDK):
    Language Equivalents
    http://msdn2.microsoft.com/en-us/library/czz35az4(vs.71).aspx
  • If you want to use implementation inheritance, study object-oriented principles of design, and consider application architecture in advance. A common mistake is to put code in a main application that you need to inherit in a Class Library. In this scenario, you cannot inherit the main application in a Class Library because of circular dependencies. For example, try to add a reference to the Menu project in the ClassLibrary1 project. You receive the following error message:
    A reference to 'Menu' could not be added. Adding this project as a reference would cause a circular dependency.

REFERENCES

For more information, refer to the following Microsoft Web sites:
Basic Instincts: Using Inheritance in .NET, Part 1
http://msdn.microsoft.com/en-us/magazine/cc135806.aspx

Basic Instincts: Using Inheritance in .NET, Part 2
http://msdn.microsoft.com/en-us/magazine/cc301744.aspx

XML Core
http://msdn.microsoft.com/en-us/xml/bb291077.aspx

Xml Web Server Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gnconxmlwebservercontrol.asp

XML Data-Islands
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/comsrv2k/htm/cs_tp_bizdesk_nqok.asp

Properties

Article ID: 315903 - Last Review: February 28, 2014 - Revision: 5.8
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbnosurvey kbarchive kbvs2005swept kbvs2005applies kbhowtomaster KB315903

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com