472,141 Members | 1,020 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

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.

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

Oct 11 '06 #1
0 1553

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Emrah Gozcu | last post: by
2 posts views Thread by Pham Nguyen | last post: by
3 posts views Thread by Chris Newby | last post: by
1 post views Thread by Pavan | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.