By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

Converting ASP.NET Page from VB to C#

P: n/a
Hi All,

I'm trying to convert some ASP.NET code that I found at
http://weblogs.asp.net/dannychen/arc...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/arc...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>

Sep 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.*************@gmail.com wrote:
Hi All,

I'm trying to convert some ASP.NET code that I found at
http://weblogs.asp.net/dannychen/arc...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/arc...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>
Sep 26 '06 #2

P: n/a
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>

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

k.*************@gmail.com wrote:
Hi All,

I'm trying to convert some ASP.NET code that I found at
http://weblogs.asp.net/dannychen/arc...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/arc...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>
Sep 26 '06 #3

P: n/a
Does anyone else out there have any ideas? Thanks.

Sincerely,
Kris

Sep 27 '06 #4

P: n/a
There's several VB --C# free online converters.

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

http://www.developerfusion.co.uk/uti...sharptovb.aspx

http://www.carlosag.net/Tools/CodeTr...r/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 ?


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<k.*************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Does anyone else out there have any ideas? Thanks.

Sincerely,
Kris

Sep 27 '06 #5

P: n/a
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:Labelportion. 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/Micros...enu_Items.aspx

// Articles on adding custom icons to menu using sitemap
//
http://www.gotdotnet.com/Community/U...6-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>
Juan T. Llibre wrote:
There's several VB --C# free online converters.

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

http://www.developerfusion.co.uk/uti...sharptovb.aspx

http://www.carlosag.net/Tools/CodeTr...r/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 ?


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<k.*************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Does anyone else out there have any ideas? Thanks.

Sincerely,
Kris
Sep 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.