custom control viewstate problem - please help so i can sleep

W

Walter

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.

Walter


(BOX1)
InfoChannelsListViewer.cs

public event MyEventHandler SelectedInfoChannelChanged;

public InfoChannelsListViewer() {} //ctor

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

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
EventHandler(lbtnInfoChannel_Click);

infoChannelsPanel.Controls.Add(lbtnInfoChannel);
}
}

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);
}
}
}

(Box2)
InfoObjectListViewer.cs

private int _infoChannelId;

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

private void LoadInfoObject(int infoChannelId)
{
Panel panel2 = new Panel();
this.Controls.Add(panel2);

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
EventHandler(infoObjectButtonSendID_Click);
}
}

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top