LoadControl and the PostBack problem


J

jack

Hi folks,
Consider a page consisting of a treeview, and a panel. The panel is
supposed to be refreshed based on treeview node selection.

protected void tree_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode node = this.tree.SelectedNode;
if (node != null)
LoadMyControls(node);
}

Where LoadMyControls simply loads the associated control based on the
node and adds it to the panel's controls collection. The loaded
control, on the other side, is supposed to have a DropDownList
control, which based on user selection updates a gridview. However,
when a new item is selected in the combo box, the entire panel is
vanished and a blank page appears.

AFAIK, This indicates that the control has to be loaded again, hence,
the LoadMyControls has to be invoked. So I added the following lines
to the Page_Load function:

Boolean isTreeThePostBackSource =
this.scriptManager.AsyncPostBackSourceElementID.Equals(this.tree.UniqueID);
if (!isTreeThePostBackSource)
{
TreeNode node = this.tree.SelectedNode;
if (node != null)
LoadMyControls(node);
}

This way, everything works fine.

However, I've noticed that IsPostBack is always true, when the User
Control gets loaded. So, I've got no mechanism to learn whether the
user control should be initialized for the first time. How am I
supposed to do this?

Any help would be highly appreciated,

Thanks
Jack
 
Ad

Advertisements

M

Munna

Hi,

"However, I've noticed that IsPostBack is always true,"

Page.IsPostback Is false when the page is requested for the first
time...
after each subsequent submit from any control the value is this
variable is false

regards

Munna
 
Ad

Advertisements

J

jack

Hi,

"However, I've noticed that IsPostBack is always true,"

Page.IsPostback Is false when the page is requested for the first
time...
after each subsequent submit from any control the value is this
variable is false

regards

Munna

That's not the case when you use the LoadControl to load a user
control dynamically!
 

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

Top