Postback and dynamically loadiing user controls. What am I doing wrong?

J

John Smith

I still have not gotten this damn thing figured out and I'm asking for help
one last time before I give up on it. I have a user control that contains a
paged gridview control. The master page contains a menu control What I
want to happen is that, whenever a menu item is selected, the appropriate
user control will load. Simple enough? Well, not for me.

I finally got the gridview paging and postback issue resolved thanks to
http://scottonwriting.net/sowblog/posts/2129.aspx . But now I have another
problem. I need the master page to load different controls based on which
menu item is clicked. For example, my menu has "Item1" and "Item2". When
I click "Item1" the paged gridview user control loads. When I click "Item2"
another user control will load. However, I cannot make this work.

On the master page is the <asp:Menu> named "mnuTicketMan". Inside the
menuItemClick event handler, I have the following code:

mvTicketMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
strDBView =
mvTicketMan.Views[Int32.Parse(e.Item.Value)].ID.ToString().Replace("vw",
"").Replace("_", " ");
ViewState["DBView"] = strDBView;

What I originally tried to do was load the controls in that event handler.
Something like:

if (strDBView == "Item1")
// load gridview control
// add gridview control to a placeholder
// bind data to gridview
if (strDBView == "Item2")
// load the item2 control
// add item2 control to a placeholder

Problem there was that the controls wouldn't work correctly because they
were only being loaded when the menu is clicked. If I clicked on a data
page of the gridview control, it would send a postback and not reload the
control----because the menu wasn't clicked.

So, I eventually got the paging working by loading the control on the master
page's Page_Load event handler. Something like this:

void Page_Load {
// load user gridview control
// add user gridview control to placeholder

if (!IsPostBack)
// load the menu items
// bind the gridview data
}

But you'll notice one thing above. I am loading that gridview user control
everytime. That's not what I want. No problem. I'll just create a global
string variable, assign its value in the menuItemClick event handler, and
then check it in the Page_Load event handler. Something like this:

//menuItemClick handler
mvTicketMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
strDBView =
mvTicketMan.Views[Int32.Parse(e.Item.Value)].ID.ToString().Replace("vw",
"").Replace("_", " ");
ViewState["DBView"] = strDBView;
globalVar = strDBView;

// Page_Load handler
if (globalVar == "Item1")
// load user gridview control
// add user gridview control to placeholder
if (globalVar == "Item2")
// load another control
// add another control to placeholder

if (!IsPostBack)
// load the menu items
// bind the appropriate control to data

Nope. No prize for me. "globalVar" gets set when the menu is clicked but
it gets reset in the Page_Load handler. Basically, "globalVar" never ==
Item1 or Item2 when the page is loaded. I have tried placing the control
loading code in various places...like OnInit, OnPreRender, etc. "globalVar"
does keep its value inside the Init and PreRender but loses it in Load. I
have also tried using ViewState to store the value but the same thing
happens.

So, how can I make this work? I need globalVar to keep its value after it
gets set upon the menu item click. Or, if someone knows another way of
going about this, I'd appreciate that as well. I hope someone responds
because I spent 35 minutes typing this post! :) I will provide the actual
code if need be.

TIA
John
 

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top