Groups | Blog | Home
all groups > asp.net building controls > september 2003 >

asp.net building controls : dynamically adding user controls



Dune
9/25/2003 9:37:32 PM
I have a web form that has a button called "Add Blank Row".
Every time this button is pressed a new "blank row" user
control should be added to the web form. This is done
dynamically in the button's event handler by calling
Page.LoadControl(). Everything works ok except that the
web form can't seem to remember the user controls that are
added.

The moment a postback occurs, any user controls that exist
disappear.

With some googling, I found a page which said "you must
load dynamically created controls on EVERY postback".

Is this true?And if so, could someone please explain why
and how to work around it?

(I assume it would require keeping track of the user
controls as they are added but I can't think of a
efficient way to go about doing this)


John Saunders
9/26/2003 8:34:15 AM
[quoted text, click to view]

This is true, and not just for user controls. It's true for all controls.

Each request, whether an initial request (GET) or a postback (POST) creates
a new instance of your page class. This instance has no relationship to the
instance created on the previous request, nor is there a relationship
between the instance created for the initial request and the one created on
PostBack. In particular, controls you added on the initial request will not
exist in the instance created on PostBack - unless you create them.

Now, if ViewState is enabled, once you create the controls and add them to
their parent's Controls collection, the controls will load their own
ViewState. However, this requires that the controls be created in the same
order each time.

[quoted text, click to view]

Actually, ViewState is a good way to do this. On the initial request, when
you decide how many rows you'll have the first time around, set
ViewState["TableRows"] to the number of rows. On PostBack, you can read
ViewState["TableRows"] to find out how many rows you'll need to create. In
the Click event handler for your "New Row" button, you can add the new row
and increment ViewState["TableRows"]. On the next PostBack, you'll read
ViewState["TableRows"] to find out how many rows you'll need to create...
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com

Dune
9/28/2003 3:49:42 PM
thanks :)

got it all working now


[quoted text, click to view]
Vyas Bharghava
9/30/2003 9:33:38 AM
Hi John,

I've couple of questions with respect to adding
WebControls to a page.

I've written a WebControl that encapsulates an client-side
ActiveX control. Now, I want to add multiple instances of
this control to my page.

If I statically bind it with


<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false"
Inherits="ControlLifeCycle.WebForm1" %>
<%@ Register TagPrefix="dial"
Namespace="BroadVu.Util.Widgets" Assembly="Dial" %>
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post"
runat="server">
<Dial:dial id="addedDial"
runat="server"></Dial:dial>
</form>
</body>
</HTML>

it works fine. The output HTML is rendered properly
within the <form> tag.

Issue #1
--------
I'm using Page.RegisterClientScript &
Page.RegisterStartupScript methods inside my WebControl to
emit some JavaScript. They are NOT getting emitted.
Though if I have a custom Render(Page page) method, the
scripts get emitted!

Issue #2
--------
If I add control this way:

Dial dial = new Dial(xml);
this.Controls.Add(dial);

the HTML rendered through Render(HtmlWriter) outputs
OUTSIDE the <html> tag!


<HTML>
</HTML>
<table id="K000004" border="1" style="border:1px solid
#C9E7FA;">
<!--HTML content written by the control out goes here! -->
</table>


Any help in this regard would be deeply appreciated.
Please help.


Regards,


Vyas






[quoted text, click to view]
John Saunders
9/30/2003 7:24:48 PM
[quoted text, click to view]

I don't know of any method with the signature "void Render(Page)". What is
this method?

And,in your "protected override void Render(HtmlTextWriter writer)" method,
are you calling base.Render(writer)"?


[quoted text, click to view]

In the above, what was "this"?

[quoted text, click to view]


There's something very strange about the way that rendering is working in
your case. Please make sure that all of your overloads are calling their
base class versions. If there's still a problem after that, please let us
know more about what events your code is running in.
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com

Vyas Bharghava
10/1/2003 6:12:10 AM
[quoted text, click to view]

This is a method written by me.
If I pass the page object from the ASPX page in the onload event:

Dial dial = new Dial();
dial.Render(this);

Inside this Render I access the underlying response stream thus:

public void Render(Page page)
{
page.Response.Write("Hi there!");
}

[quoted text, click to view]

I wasn't... I'll try this.


[quoted text, click to view]

It's the page object. I'm adding the control to the controls collection
of the page.

Thanks.

Vyas

*** Sent via Developersdex http://www.developersdex.com ***
John Saunders
10/2/2003 11:53:44 PM
[quoted text, click to view]

This is your problem. The Page object is not the HtmlForm. If you look at
Page.Controls, you'll find it contains an HtmlForm control. You need to add
your controls to the HtmlForm's Controls collection.
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com

AddThis Social Bookmark Button