Monday, March 26, 2012

Simple C# question - beginner

Consider:

<%@dotnet.itags.org. Page Language="c#" autoeventwireup="false" %>
<%@dotnet.itags.org. Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<%@dotnet.itags.org. import Namespace="System.Data" %>
<%@dotnet.itags.org. import Namespace="System.Data.ODBC" %>
<%@dotnet.itags.org. import Namespace="System.Web.UI.WebControls" %>
<script runat="server"
// Create the first TreeNode
TreeNode tvFirst = new TreeNode();
tvFirst.Text = "First Tree Node";

// Create the second TreeNode
TreeNode tvSecond = new TreeNode();
tvSecond.Text = "Second Tree Node";

// Add the second TreeNode as a child of the first
tvFirst.Nodes.Add(tvSecond);

// Add the first TreeNode to the TreeView's root TreeNodes
TreeView1.Nodes.Add(tvFirst);

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<iewc:TreeView id="TreeView1" runat="server">
<iewc:TreeNodeType></iewc:TreeNodeType>
</iewc:TreeView>
</form>
</body>
</html
I get:

Compiler Error Message: CS1519: Invalid token '=' in class, struct, or interface member declaration

for:

Line 20: tvFirst.Text = "First Tree Node";

I figure I simply have the namespaces set up wrong or something. Was hoping you could point me in the right direction. Thanks.Should you declare some event like Page_load or something?
How do I get Matrix to put the Page_load in there? I don't know the syntax. I could search for it, but was hoping Matrix would to it for me. Thanks again.
I'am sorry. I 'am not very familiar with Matrix.
But one thing is certain: you MUST declare page_load event somehow.

P.S. I think there is a forum on asp.net dedicated to webmatrix issues
here's a page load:

private void Page_Load(object sender, System.EventArgs e) {

}

;)

put your code between that
After <script runat="server"> add the line

void Page_Load(object sender, EventArgs e) {

then after all your initialisation code, add a closing brace

}

Everything in a <script runat="server"> section needs to be within a method (function or subroutine) -- in this respect ASP.NET is different from classic ASP. The framework automatically wires up Page_Load so that it runs as soon as all the user controls on the page are loaded. You put all your initialisation code for your controls into the Page_Load method.

0 comments:

Post a Comment