New to ASP.NET

C

C.C. \(aka Me\)

Ok.. I am quite proficient with C# in terms of non-web apps and have decided
to take a look at ASP.NET. I am familiar with HTML, HTTP and general web
"ideas" but have not done much with ASP (more of a PHP coder myself.)

So.. I have watched quite some of WebCasts that MS has put out and have
played around a little bit on my own machine but am finding it hard to track
down "how to" type of information.

For instance, I tried populating a tree view with the list of running
services on the server.. This was pretty straight forward. Except.. Whenever
I do a postback or click on a entry in the Tree View I end up getting all
the fields added to the tree view again.. and again.. and again.. you get
the idea.

So I moved the code from Page_Load() to Page_Init() thinking that it would
then only populate the tree view when the page was first requested.. but it
didnt.. it called it every time also.

Now, where can I find information about this type of "workflow" or "program
flow" in MSDN or on the web. It is hard to know what to look for when you
are just starting out with something new.

Any good reference sites, articles or other online docs that you could
suggest that goes over the general flow of things would be great.

Thanks!
 
S

sloan

You need to become very famaliar with the


if (!Page.IsPostBack)
{
//do stuff here
}


or VB.net

if (not (Page.IsPostBack)) then

end if
 
C

C.C. \(aka Me\)

I was looking at those just now.. Is that the norm in terms of determing if
you should fill stuff in or not?

Also, I have the following Page_Init() handler on my page. No matter what
the TreeView1.Nodes.Count property value is 0 when it is called. But it ends
up being that the nodes are still left over from the previous time the page
was requested (aka clicking on a node.) If I call TreeView1.Nodes.Clear() I
would think that it would have cleared out the old data but that is not the
case?


protected void Page_Init(object sender, EventArgs e)
{
TreeView1.Nodes.Clear(); //Seems to have no effect?
ServiceController[] svc = ServiceController.GetServices();
foreach (ServiceController aService in svc)
{
TreeNode aNode = new TreeNode(aService.ServiceName);
aNode.ChildNodes.Add(new TreeNode(aService.Status.ToString()));
TreeView1.Nodes.Add(aNode);
}
TreeView1.CollapseAll();
}

Thanks.
 
S

sloan

put your code back in Page_Load

and do somehting like this

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

{

if (!Page.IsPostBack)

{

LoadData();

}

}



private void LoadData()
{

TreeView1.Nodes.Clear(); //Seems to have no effect?
ServiceController[] svc = ServiceController.GetServices();
foreach (ServiceController aService in svc)
{
TreeNode aNode = new TreeNode(aService.ServiceName);
aNode.ChildNodes.Add(new TreeNode(aService.Status.ToString()));
TreeView1.Nodes.Add(aNode);
}
TreeView1.CollapseAll();

}


This is how I start pretty much ~every aspx page I ever write (when its
binding to some datasource).

...






C.C. (aka Me) said:
I was looking at those just now.. Is that the norm in terms of determing if
you should fill stuff in or not?

Also, I have the following Page_Init() handler on my page. No matter what
the TreeView1.Nodes.Count property value is 0 when it is called. But it ends
up being that the nodes are still left over from the previous time the page
was requested (aka clicking on a node.) If I call TreeView1.Nodes.Clear() I
would think that it would have cleared out the old data but that is not the
case?


protected void Page_Init(object sender, EventArgs e)
{
TreeView1.Nodes.Clear(); //Seems to have no effect?
ServiceController[] svc = ServiceController.GetServices();
foreach (ServiceController aService in svc)
{
TreeNode aNode = new TreeNode(aService.ServiceName);
aNode.ChildNodes.Add(new TreeNode(aService.Status.ToString()));
TreeView1.Nodes.Add(aNode);
}
TreeView1.CollapseAll();
}

Thanks.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sloan said:
You need to become very famaliar with the


if (!Page.IsPostBack)
{
//do stuff here
}


or VB.net

if (not (Page.IsPostBack)) then

end if




but
it
 
C

clintonG

Didn't the Mad Hatter tell Alice to start at the beginning?
Start with these documents [1-3]

As a PHP coder do you know anything about MediaWiki? [4]

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://www.microsoft.com/mspress/books/sampchap/6667a.asp
[2] http://msdn2.microsoft.com/en-us/library/ms178472.aspx
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/internals.asp
[4] http://www.mediawiki.org/wiki/MediaWiki
 
C

C.C. \(aka Me\)

Thanks for the links.. I will check them out.

As for MediaWiki sorry.. dont know about it.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

clintonG said:
Didn't the Mad Hatter tell Alice to start at the beginning?
Start with these documents [1-3]

As a PHP coder do you know anything about MediaWiki? [4]

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://www.microsoft.com/mspress/books/sampchap/6667a.asp
[2] http://msdn2.microsoft.com/en-us/library/ms178472.aspx
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/internals.asp
[4] http://www.mediawiki.org/wiki/MediaWiki



C.C. (aka Me) said:
Ok.. I am quite proficient with C# in terms of non-web apps and have
decided to take a look at ASP.NET. I am familiar with HTML, HTTP and
general web "ideas" but have not done much with ASP (more of a PHP coder
myself.)

So.. I have watched quite some of WebCasts that MS has put out and have
played around a little bit on my own machine but am finding it hard to
track down "how to" type of information.

For instance, I tried populating a tree view with the list of running
services on the server.. This was pretty straight forward. Except..
Whenever I do a postback or click on a entry in the Tree View I end up
getting all the fields added to the tree view again.. and again.. and
again.. you get the idea.

So I moved the code from Page_Load() to Page_Init() thinking that it
would then only populate the tree view when the page was first
requested.. but it didnt.. it called it every time also.

Now, where can I find information about this type of "workflow" or
"program flow" in MSDN or on the web. It is hard to know what to look for
when you are just starting out with something new.

Any good reference sites, articles or other online docs that you could
suggest that goes over the general flow of things would be great.

Thanks!

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
E

Erik Funkenbusch

For instance, I tried populating a tree view with the list of running
services on the server.. This was pretty straight forward. Except.. Whenever
I do a postback or click on a entry in the Tree View I end up getting all
the fields added to the tree view again.. and again.. and again.. you get
the idea.

So I moved the code from Page_Load() to Page_Init() thinking that it would
then only populate the tree view when the page was first requested.. but it
didnt.. it called it every time also.

There are several ways to handle this. By default ASP enables a controls
viewstate. Viewstate is a hidden input control that other controls
serialize their data to in order to maintain state from one postback to the
next. If you ahve a lot of controls, this can greatly bloat your page.
You should disable viewstate on any controls that don't need it, and you
should seriously consider whether you need it on controls you think might.

Now, what's happening here is that your tree control is serializing it's
data to viewstate, then when you load the page on postback, it's reloading
its state and then you are reloading it as well in your page_load.

So, the two basic solutions are:

1) use viewstate and then check for postback in your page_load, and then
don't load it if it's a postback.

2) turn off viewstate and load the control yourself every time.

The 1st method can really bloat your html if your tree contains a lot of
data. It also can explose properties of your tree state you might not want
exposed, since it serializes *ALL* of the properties of the control.

The 2nd method can put a large strain on your data source, but with
cacheing and other techniques, you can allieviate that.

I prefer the 2nd method for most things, unless it's a VERY expensive query
that isn't cacheable because I don't like large viewstates.
 
E

Erik Funkenbusch

Also, I have the following Page_Init() handler on my page. No matter what
the TreeView1.Nodes.Count property value is 0 when it is called.

This is likely because the de-serialization code for the viewstate occurs
after the point in which your code is run. So, when your code runs, the
tree is empty, but ASP.NET will fill it with your viewstate info
afterwards.

You really need to familiarize yourself with the method and event flow of
asp.net, and understand what it's doing behind the scenes. You might want
to start with these articles:

http://www.devx.com/codemag/Article/30082/0/page/1
http://www.ondotnet.com/pub/a/dotnet/2003/09/22/debugaspnet.html
http://www.oracle.com/technology/pub/articles/hull_asp.html
 
C

C.C. \(aka Me\)

Very good info. This type of info is what I am looking for - but it is hard
to search for what you dont know!

Thanks for you input!
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top