Problem with refresh after delegate event fires. c# and asp.net.

G

George K

Hi, i have a very irriting problem that i have written a short piece of code
to demonstrate. The problem is that my aspx page is not fully refreshed
after an event, which is delegated to an ImageButton during runtime, is
fired. Any suggestions would be great. This code is the c# behind a webform
with 1 panel and 2 buttons on it. I can see that the Page controls are
correct after the event, but the page does not display it this way.

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (Session["table"] != null)
Panel1.Controls.Add((Table) Session["table"]);
else
DisplayTable(true,true);
}

private void DisplayTable(bool cell1, bool cell2)
{
Table table = new Table();

Panel1.Controls.Clear();

if (cell1)
{
table.Rows.Add(CreateCell(1));
}
if (cell2)
{
table.Rows.Add(CreateCell(2));
}

Panel1.Controls.Add(table);

Session["table"] = table;
}

private TableRow CreateCell(int cellId)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
ImageButton button = new ImageButton();
button.AlternateText = "Button"+cellId;
button.ID = "BTN"+cellId;
button.CommandName = cellId.ToString();
EventInfo eventInfo = button.GetType().GetEvent("Click");
Delegate d = Delegate.CreateDelegate(typeof(ImageClickEventHandler),
this, "NewPostbackEvent");
eventInfo.AddEventHandler(button, d);
cell.Controls.Add(button);
row.Cells.Add(cell);
return row;
}
private void NewPostbackEvent(object sender,
System.Web.UI.ImageClickEventArgs e)
{
ImageButton button = (ImageButton) sender;
if (button.CommandName == "1")
DisplayTable(false, true);
else
DisplayTable(true, false);
}

private void Button1_Click(object sender, System.EventArgs e)
{
DisplayTable(true,true);
}

private void Button2_Click(object sender, System.EventArgs e)
{
// Page.FindControl("BTN1");
// Page.FindControl("BTN2");
}
 
J

John Saunders

George K said:
Hi, i have a very irriting problem that i have written a short piece of code
to demonstrate. The problem is that my aspx page is not fully refreshed
after an event, which is delegated to an ImageButton during runtime, is
fired. Any suggestions would be great. This code is the c# behind a webform
with 1 panel and 2 buttons on it. I can see that the Page controls are
correct after the event, but the page does not display it this way.

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (Session["table"] != null)
Panel1.Controls.Add((Table) Session["table"]);
else
DisplayTable(true,true);
}

I have no idea whether or not this is your problem, but if you put a
reference to the table into Session, and then add it the Controls collection
of the Panel, then table.Parent will point to the Panel control, and
Panel.Parent will point to (for example) the HtmlForm, and form.Parent will
point to the Page.

So, aren't you going to be keeping the entire page around in memory, at
least until the "Panel1.Controls.Add"?

And at that point, I wonder what happens when you take the table out of the
Controls collection of the previous request's Panel1 and put it into the
Panel1 of the current request? Many parts of a Controls lifecycle depend on
which Controls collection the control is in.
 
G

George K

The reason the table is in a session and put back into the panel controls on
page load is, that if you dont do that, the postback event will not fire at
all because the control is no longer on the page. So you have to put the
control back to allow the postback to fire, then do whatever else you want.
But I think you are right, it is probably a problem with the current
Response object.

John Saunders said:
George K said:
Hi, i have a very irriting problem that i have written a short piece of code
to demonstrate. The problem is that my aspx page is not fully refreshed
after an event, which is delegated to an ImageButton during runtime, is
fired. Any suggestions would be great. This code is the c# behind a webform
with 1 panel and 2 buttons on it. I can see that the Page controls are
correct after the event, but the page does not display it this way.

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (Session["table"] != null)
Panel1.Controls.Add((Table) Session["table"]);
else
DisplayTable(true,true);
}

I have no idea whether or not this is your problem, but if you put a
reference to the table into Session, and then add it the Controls collection
of the Panel, then table.Parent will point to the Panel control, and
Panel.Parent will point to (for example) the HtmlForm, and form.Parent will
point to the Page.

So, aren't you going to be keeping the entire page around in memory, at
least until the "Panel1.Controls.Add"?

And at that point, I wonder what happens when you take the table out of the
Controls collection of the previous request's Panel1 and put it into the
Panel1 of the current request? Many parts of a Controls lifecycle depend on
which Controls collection the control is in.
 
J

John Saunders

George K said:
The reason the table is in a session and put back into the panel controls on
page load is, that if you dont do that, the postback event will not fire at
all because the control is no longer on the page. So you have to put the
control back to allow the postback to fire, then do whatever else you
want.

The usual solution to this is to recreate the dynamic controls upon
postback. Unless the overhead of creating a specific control is high, I
would think it would be faster to create the controls each time than to keep
them around in Session state.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top