custom control viewstate problem - please help so i can sleep



Hi, can someone please help me with my custom control viewstate problem....I
haven't slept for hours trying to get this fixed.

I am making two custom controls which will be included on a single page. For
now, I will call them box1 and box2. Each box has a panel and will display a
list of link buttons on the panel.
When the page loads for the first time, box1 will call the database and
create a list of linkbuttons and each link button has a CommandArgument
which represents an ID that will be passed on to box2.

I defined a custom event in box1 which will be fired when a linkbutton is
clicked. In my default.aspx, I capture that event and extract the
commandargument, and then set the ID property of box2 and load another set
of linkbuttons in box2 for that id.

it kinda works for the first half right now, i can load box2's linkbuttons
for the correct id on page postback, however, my problem is that if a
linkbutton in box2 is clicked, when the page reloads, i lose all the
linkbuttons in box2.

I know I need to recreate all the controls in box2 on every postback. But I
just can't get it to work.

My questions are:

First, Is it a good or bad idea to use custom event + property setter to
pass data between controls and trigger the loading of the second box?

I have tried to override the OnInit, CreateChildControls(), OnPrerender(),
etc in box2, to check if the ID is in the viewstate from box1, if it is
there, I call the LoadInfoObject() method directly. But it didn't work,
viewstate was always null.
so the second question is how and when should I store the ID from box1 in
the viewstate? in which method in box2 can i access that viewstate?

I have attached my code below.

Please enlighten me.



public event MyEventHandler SelectedInfoChannelChanged;

public InfoChannelsListViewer() {} //ctor

protected override void CreateChildControls()
Panel infoChannelsPanel = new Panel();
infoChannelsPanel.EnableViewState = true;

foreach (InfoChannel channel in Db.GetInfoChannels()
LinkButton lbtnInfoChannel = new LinkButton();
lbtnInfoChannel.CommandName = "SelectInfoChannel";
lbtnInfoChannel.CommandArgument = channel.ID.ToString();
lbtnInfoChannel.Text = channel.Name;
lbtnInfoChannel.Click += new


void lbtnInfoChannel_Click(object sender, EventArgs e)
if (sender is LinkButton)
LinkButton btn = sender as LinkButton;
if (btn.CommandName == "SelectInfoChannel")
//store channel id in viewstate
ViewState["InfoChannelId"] = btn.CommandArgument;

//raise Channel Changed event
MyEventArgs arg = new MyEventArgs
(MyEventType.InfoChannelEvent, Convert.ToInt32(btn.CommandArgument));
SelectedInfoChannelChanged(this, arg);


private int _infoChannelId;

public int InfoChannelId
get { return _infoChannelId; }
_infoChannelId = value;
if (_infoChannelId > 0)

private void LoadInfoObject(int infoChannelId)
Panel panel2 = new Panel();

foreach (InfoObject obj in Db.GetInfoObjects(infoChannelId)
LinkButton infoObjectButton = new LinkButton();
panel2.Controls.Add(infoObjectButton );

infoObjectButton.CommandName = "SelectInfoObject";
infoObjectButton.CommandArgument = obj.ID.ToString();
infoObjectButton.Text = obj.Title;
infoObjectButton.Click += new

void infoObjectButtonSendID_Click(object sender, EventArgs e)
if (sender is LinkButton)
LinkButton btn = sender as LinkButton;
if (btn.CommandName == "SelectInfoObject")
this.Page.Session["InfoObjectID"] = btn.CommandArgument;
//TODO: further update


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