Datalist in User Control and Dynamic Load it.

Y

Yan Wang

Hi!:

I encounter this problem when I do some tests:
I have one user control with datalist in it. The ID for
datalist is "dlTest". Then in this control code behind class, I
declare a protected attribute like "DataList dlTest"
Then I create a test asp page to load my control dynamically.
by using "LoadControl("controls\MyTest.ascx")"
The load is fine and work perfectly. But dlTest
in the Page_Load method of my control class is always null.
If I do not use dynamic loading, it works fine. Does anyone
know the reason?

Thanks for any help!

Yan
 
J

Jos

Yan Wang said:
Hi!:

I encounter this problem when I do some tests:
I have one user control with datalist in it. The ID for
datalist is "dlTest". Then in this control code behind class, I
declare a protected attribute like "DataList dlTest"
Then I create a test asp page to load my control dynamically.
by using "LoadControl("controls\MyTest.ascx")"
The load is fine and work perfectly. But dlTest
in the Page_Load method of my control class is always null.
If I do not use dynamic loading, it works fine. Does anyone
know the reason?


Don't use just dlTest, but use
CType(FindControl("dlTest"),DataList)
 
N

Natty Gur

Hi,

Are you loading the control on postback ? if you use IsPostback to
prevent loading the user control on postback remove it. dynamic controls
should be add every time the page load.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Y

Yan Wang

Which means dynamic loading can not map ASP ID to the variable in
code-behind class?
But why? Is it a bug of Microsoft or they have good reason to do so?

Thanks!

Yan
 
Y

Yan Wang

No, I load it always. Actually, I put loading code in Handler of OnPageInit.

Thanks!

Yan
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top