Two PostBack Events Problem

X

xeroxero

ASP.NET 2.0 Web Application Project.

I have a class called "SuperPage" that inherits from the
System.Web.UI.Page class. In SuperPage, I
1. Override OnInit (do somecustom Session setup)
2. Override Render (add some controls in the page header, add some
style stuff, etc)

I made a new web page that inherits from SuperPage. I create a
treeview in codebehind, and an empty datagrid in codebehind, and add
them to the Form's Controls collection.

I do not have any markup or control information in the .aspx page
template.

In the Event when a tree node is clicked, I get data and DataBind it
to the grid. However, it appears that the ASP.NET event cycle then
starts all over again, so the Controls and drawn twice, a data query
happens (yes I always get some data back) and the grid is databound.
Worse, the grid always shows zero results.

What could be happening here?

Thanks.
 
O

OHM

havent done too much with 2.0, but it would seem prudent to add a
debug.writeline statement to the prerender or render in the super page to
see if it is being called even though you think you have overriden it. This
could at least be a starting point.
 
G

Guest

Hi,

Follwing might be useful for you to solve your problem.

WHY TO ADD DYNAMIC CONTROLS EVERY TIME?
Please note that the controls added to page dynamically will have to be
added on every trip to the server. You might wonder why do the controls which
have been placed declaritively or at design time does not exhibit this
behaviour but in reality behind the scenes this is what the ASP.Net runtime
is doing by itself. When the page is first requested or its html is modified,
ASP.Net generates a class file that is derived from the requested page and
add the types of declaration like
Page.Controls.Add
in that file.
So everytime when you request the page, declarative controls get added to
the controls collection of the page. Since you are adding the controls by
yourself you have to add them every time the page is requested by yourself.

WHEN TO ADD DYNAMIC CONTROLS (WHY NOT THE RENDER EVENT)?
It always better for any dynamic controls, to be added in the Page Oninit
event. i.e the lines saying
this.Controls.Add should be placed in the Page_OnInit event.
The reason for this is, ASP.Net Loads the View State for the Controls just
after this event is finished.
So it expects all the controls to be there before loading view state.


So dont think the controls are being added twice and Render is not the best
place to add them.


Regards,
Hameer Saleem
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top