Converting ASP.NET Page from VB to C#

K

k.vanderstarren

Hi All,

I'm trying to convert some ASP.NET code that I found at
http://weblogs.asp.net/dannychen/archive/2005/12/02/432190.aspx from VB
to C#.

I've managed to convert the portion that is in the <script></script>
section of the file fine (I think) but I've been unable to convert the
code in each of the <asp:Label /> tags so far. Can anyone point me in
the right direction?

The only thing that the code is supposed to do is allow for a custom
CSS tags to be added to the sitemap file for the site so that each menu
item can have its own CSS. The original code along with the section
that I have converted is shown below.

Thanks,
Kris


Original Code
===========
<%@ Master Language="VB" CodeFile="MasterPage.master.vb"
Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script runat="server">
REM http://weblogs.asp.net/dannychen/archive/2005/12/02/432190.aspx
Shared styles As New System.Collections.Generic.Dictionary(Of
String, String)
Protected Sub Menu1_MenuItemDataBound(ByVal sender As Object, _
ByVal e As
System.Web.UI.WebControls.MenuEventArgs)
styles(e.Item.ValuePath) = CType(e.Item.DataItem,
SiteMapNode)("style")
End Sub</script>

<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server"
DataSourceID="SiteMapDataSource1" Orientation="Horizontal"
StaticDisplayLevels="2"
OnMenuItemDataBound="Menu1_MenuItemDataBound">
<StaticItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles( eval("valuepath") ) %>' />
</StaticItemTemplate>
<DynamicItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles( eval("valuepath") ) %>' />
</DynamicItemTemplate>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
/>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

Converted Section
==============
<script runat="server">
public static System.Collections.Generic.Dictionary<string, string>
styles = new System.Collections.Generic.Dictionary<string, string>();

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs
e)
{
styles[e.Item.ValuePath] =
((SiteMapNode)e.Item.DataItem)["style"];
}
</script>
 
Q

q

You don't have to change anything in the ASP.NET controls.
eval("valuepath") may look like VB, but it's ASP.NET.
 
K

k.vanderstarren

Thank you for the response q. When I do the following:

<DynamicItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%# Eval("Text") %>'
Style='<%# styles( Eval("valuepath") ) %>' />
</DynamicItemTemplate>

I receive the following error:

Error 3 'ASP.masterpage_master.styles' is a 'field' but is used like a
'method' D:\Visual Studio\CustomMenuItems.CS\MasterPage.master 24

I've included the entire source for the new page (the one that genrates
the error) below along with the contents of my sitemap file in case it
helps. Thanks again.

MasterPage.master
===============
<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script runat="server">
public static System.Collections.Generic.Dictionary<string, string>
styles = new System.Collections.Generic.Dictionary<string, string>();

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs
e)
{
styles[e.Item.ValuePath] =
((SiteMapNode)e.Item.DataItem)["style"];
}
</script>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div id="Head">
<asp:Menu ID="Menu1" runat="server"
DataSourceID="SiteMapDataSource1"
OnMenuItemDataBound="Menu1_MenuItemDataBound" Orientation="Horizontal"
StaticDisplayLevels="2">
<DynamicItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles( Eval("valuepath") ) %>' />
</DynamicItemTemplate>
<StaticItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles( Eval("valuepath") ) %>' />
</StaticItemTemplate>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1"
runat="server" />
</div>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="Default.aspx" title="Home" description="Main
page">
<siteMapNode url="Page1.aspx" title="Page1" description="Page1
Description" style="background-color:Blue; color:White;">
<siteMapNode url="Page1.1.aspx" title="Page1.1"
description="Page1 Description" style="background-color:Yellow;
color:Black;" />
</siteMapNode>
<siteMapNode url="Page2.aspx" title="Page2" description="Page2
Description" style="background-color:Black; color:Yellow;" />
</siteMapNode>
</siteMap>
You don't have to change anything in the ASP.NET controls.
eval("valuepath") may look like VB, but it's ASP.NET.

Hi All,

I'm trying to convert some ASP.NET code that I found at
http://weblogs.asp.net/dannychen/archive/2005/12/02/432190.aspx from VB
to C#.

I've managed to convert the portion that is in the <script></script>
section of the file fine (I think) but I've been unable to convert the
code in each of the <asp:Label /> tags so far. Can anyone point me in
the right direction?

The only thing that the code is supposed to do is allow for a custom
CSS tags to be added to the sitemap file for the site so that each menu
item can have its own CSS. The original code along with the section
that I have converted is shown below.

Thanks,
Kris


Original Code
===========
<%@ Master Language="VB" CodeFile="MasterPage.master.vb"
Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script runat="server">
REM http://weblogs.asp.net/dannychen/archive/2005/12/02/432190.aspx
Shared styles As New System.Collections.Generic.Dictionary(Of
String, String)
Protected Sub Menu1_MenuItemDataBound(ByVal sender As Object, _
ByVal e As
System.Web.UI.WebControls.MenuEventArgs)
styles(e.Item.ValuePath) = CType(e.Item.DataItem,
SiteMapNode)("style")
End Sub</script>

<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server"
DataSourceID="SiteMapDataSource1" Orientation="Horizontal"
StaticDisplayLevels="2"
OnMenuItemDataBound="Menu1_MenuItemDataBound">
<StaticItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles( eval("valuepath") ) %>' />
</StaticItemTemplate>
<DynamicItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles( eval("valuepath") ) %>' />
</DynamicItemTemplate>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
/>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

Converted Section
==============
<script runat="server">
public static System.Collections.Generic.Dictionary<string, string>
styles = new System.Collections.Generic.Dictionary<string, string>();

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs
e)
{
styles[e.Item.ValuePath] =
((SiteMapNode)e.Item.DataItem)["style"];
}
</script>
 
J

Juan T. Llibre

There's several VB --> C# free online converters.

http://www.eggheadcafe.com/articles/cstovbweb/converter.aspx

http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

http://www.carlosag.net/Tools/CodeTranslator/default.aspx

http://www.dotnettaxi.com/Tools/Converter.aspx

SharpDevelop ( the free C# IDE for .Net ) also has a code translator:
http://www.icsharpcode.net/OpenSource/SD/Download/

Stick your code into them ( just the code, not the HTML )
and see which one does the best job for you.

When you're done, please tell us which one worked best, OK ?
 
K

k.vanderstarren

Great links Juan - I've been looking for some good converters! The only
one I've used so far is the demo of Instant C#: VB to C# Converter from
Tangible Software (http://www.tangiblesoftwaresolutions.com) but I
don't have enough experience with it yet to give it a positive or
negative review.

I managed to get the code working this morning without using a
converter. Here's the final code. The only major change from what I
posted above is in the <asp:Label> portion. I had to change the
late-binding code for the Style attribute to the following:

<asp:Label runat="server" ID="Label1" Text='<%# Eval("Text") %>'
Style='<%# styles[(string)Eval("ValuePath")] %>' />


<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage"
Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">
// Original source (VB.NET)
//
http://www.dotnetslackers.com/Microsoft/re-29612_Customizing_Individual_Menu_Items.aspx

// Articles on adding custom icons to menu using sitemap
//
http://www.gotdotnet.com/Community/...mpleGuid=61b73f00-1062-45a6-bd76-4df7cad7b028

public static System.Collections.Generic.Dictionary<string, string>
styles = new System.Collections.Generic.Dictionary<string, string>();

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs
e)
{
styles[e.Item.ValuePath] =
((SiteMapNode)e.Item.DataItem)["style"];
}
</script>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div id="Head">
<asp:Menu ID="Menu1" runat="server"
Orientation="Horizontal" StaticDisplayLevels="2"
DataSourceID="SiteMapDataSource1"
OnMenuItemDataBound="Menu1_MenuItemDataBound">
<StaticItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles[(string)Eval("ValuePath")] %>' />
</StaticItemTemplate>
<DynamicItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#
Eval("Text") %>' Style='<%# styles[(string)Eval("ValuePath")] %>' />
</DynamicItemTemplate>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1"
runat="server" />
</div>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top