namespace not allowed in script code declaration block?

S

Steve Richter

I have a .cs file that contains the c# script of a page:
<script language="c#" runat=server src="item.cs"/>

The file item.cs contains c# code that contains a namespace:

namespace ItemPages
{
public class ShowItemToCust
{
public static void Redirect(System.Web.HttpResponse InResponse,
int InItemId, string InOnlineId,
string InLoadId)
{
StringBuilder sb = "item_ShowToCust.aspx ";
sb.Append("?OnlineId=" + InOnlineId);
sb.Append("&ItemId=" + InItemId.ToString());
sb.Append("&Referer=" + InLoadId);
InResponse.Redirect(sb.ToString());
}
}
}

I get an error with I run the page. a namespace is not permitted in a
c# script code block in an asp.net page?

thanks,
-Steve

c:\Inetpub\wwwroot\lmartin\Controls\item.cs(6,1): error CS1519: Invalid
token 'namespace' in class, struct, or interface member declaration
c:\Inetpub\wwwroot\lmartin\Controls\item.cs(7,1): error CS1519: Invalid
token
 
B

Brock Allen

The <script runat=server> essentially pulls this code inside of the class
definition generated by ASP.NET from the ASPX file. So it ends up being something
like this:

public class item_aspx : System.Web.UI.Page
{
// your <script runat=server> starts here
namespace ItemPages
{
// your other stuff in here
}

// the rest of the ASPX generated class
}

See how the namespace nests inside the class definition? Can't do that. What's
put in the <script runat=server> is code that auguments the ASPX generated
class definition -- so methods, fields, properties, etc.

HTH
 
S

Steve Richter

Brock said:
The <script runat=server> essentially pulls this code inside of the class
definition generated by ASP.NET from the ASPX file. So it ends up being something
like this:

public class item_aspx : System.Web.UI.Page
{
// your <script runat=server> starts here
namespace ItemPages
{
// your other stuff in here
}

// the rest of the ASPX generated class
}

See how the namespace nests inside the class definition? Can't do that. What's
put in the <script runat=server> is code that auguments the ASPX generated
class definition -- so methods, fields, properties, etc.

HTH

as usual Brock, it helps a lot. :) I did not realize you could nest
classes in C#. I replaced namespace with static class and all is right
with the world!

-Steve
 
B

Brock Allen

as usual Brock, it helps a lot. :) I did not realize you could nest
classes in C#. I replaced namespace with static class and all is right
with the world!

Actually you can nest classes inside one another, you just can't embed a
namespace inside of a class.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top