Code Behind Form

P

Prince

I have strange problem. If in Design Mode and I drag a
Web Server Control onto the form, Visual Studio
automatically declares the control for me in the code
behind page.

However, if I'm working in HTML mode and I manually enter
the the Web Server Control, nothing is written in the
code behind page. If I declare the control myself and
attempt to use it, I get an error stating something
like, "ojbect not ininialize".

I have a DataList control on my page and inside the
<ItemTemplate> I inserted (by typing) a <asp:Label ID="x"
runat="server" />. I then declared the label myself
inside the Code Behind Page as:

protected System.Web.UI.WebControls.Label x;

if I then try to set the Text property of the label, I
get the error mentioned above. Can someone please help
me out here and first explain what is going on then give
me the solution or at least where I can find the solution.

thanks,
Prince
 
K

Kevin Spencer

The change in the CodeBehind is triggered when you go to Design view.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
M

Mark Fitzpatrick

Prince,
You can't declare a control inside an itemtemplate like that. That
doesn't work because the datalist will need to create numerous copies of
them so how would it know which one to set.

What you have to do is create a function to handle an event such as
ItemDataBound of the datalist. Then you can access the label in that
particular item as so:

Label lblX = (Label) e.Item.FindControl("lblX");

Then you can access it, though you may want to do a quick check to
see if the label is null before you access it's properties.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
P

Prince

Good explanation. Thanks.
-----Original Message-----
Prince,
You can't declare a control inside an itemtemplate like that. That
doesn't work because the datalist will need to create numerous copies of
them so how would it know which one to set.

What you have to do is create a function to handle an event such as
ItemDataBound of the datalist. Then you can access the label in that
particular item as so:

Label lblX = (Label) e.Item.FindControl("lblX");

Then you can access it, though you may want to do a quick check to
see if the label is null before you access it's properties.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage




.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top