Dynamically adding Web User Controls to ASP page

J

Jason

I am trying to dynamically add a web user control - ctrlBlogEntry.ascx
- to a page - default.aspx - (via an ASP:placeHolder). This web user
control has two ASP:Label controls and I'm accessing their "Text"
properties via public properties. However, after I have instantiated
the ctrlBlogEntry control in the PageLoad method of default.aspx, it is
failing when I try to set the text of the label controls in the
ctrlBlogEntry control. Here is some code:

There is probably a very simple, obvious fix for this, I just can't
seem to see it. Can someone help?

default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="be" TagName="ctrlBlogEntry"
Src="/blog/controls/ctrlBlogEntry.ascx" %>
<be:ctrlBlogEntry id="ctrlBE" BlogTitle="Test" runat="server" />
<!--This one works just fine-->
<asp:placeholder id="phBlogEntries" runat="server" />

default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
controls_ctrlBlogEntry ctrlBE = new controls_ctrlBlogEntry();
ctrlBE.BlogTitle = "Test Title"; //Fails here.....
ctrlBE.BlogBody = "Test Body";
phBlogEntries.Controls.Add(ctrlBE);
}

ctrlBlogEntry.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ctrlBlogEntry.ascx.cs" Inherits="controls_ctrlBlogEntry" %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="tableHeader">
<asp:Label ID="lblTitle" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblBody" runat="server" />
</td>
</tr>
</table>

ctrlBlogEntry.ascx.cs:
public partial class controls_ctrlBlogEntry : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

public string BlogTitle
{
set
{
lblTitle.Text = value;
}
}

public string BlogBody
{
set
{
lblBody.Text = value;
}
}
}
 
R

Rajesh CKR

Try using

controls_ctrlBlogEntry ctrlBE = LoadControl(path);

instead of

controls_ctrlBlogEntry ctrlBE = new controls_ctrlBlogEntry();

HTH

Raj
 
J

Jason

When I use that statement:
controls_ctrlBlogEntry ctrlBE =
LoadControl("/blog/controls/ctrlBlogEntry.ascx");
I get the following error:

Cannot implicitly convert type 'System.Web.UI.Control' to
'controls_ctrlBlogEntry'. An explicit conversion exists (are you
missing a cast?)
 
J

Jason

Actually, if I cast the LoadControl(path) to the control I wish to use,
it works.

controls_ctrlBlogEntry ctrlBE =
(controls_ctrlBlogEntry)LoadControl("/blog/controls/ctrlBlogEntry.ascx");


Thanks for the direction!
 
J

Jason

Just when I thought I was done, I start getting the following error!
It seems to happen after every other compile...

Unable to cast object of type 'ASP.controls_ctrlblogentry_ascx' to type
'ASP.controls_ctrlblogentry_ascx'.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object
of type 'ASP.controls_ctrlblogentry_ascx' to type
'ASP.controls_ctrlblogentry_ascx'.

Source Error:


Line 25: foreach (DataRow dr in dtBlogEntries.Rows)
Line 26: {
Line 27: controls_ctrlBlogEntry ctrlBE =
(ASP.controls_ctrlblogentry_ascx)LoadControl("/blog/controls/ctrlBlogEntry.ascx");
Line 28: ctrlBE.BlogTitle =
dr["blog_entry_title"].ToString().Trim();
Line 29: ctrlBE.BlogBody =
dr["blog_entry_body"].ToString().Trim();
 
R

Rajesh CKR

Are you using ASP.Net 2.0?

In that case you may need to use the @Reference keyword on the page for the
cast to work

Let me know

Rajesh CKR
 
J

Jason

Yes, I am using ASP.NET 2.0. I have the following in default.aspx:
<%@ Register TagPrefix="be" TagName="ctrlBlogEntry"
Src="/blog/controls/ctrlBlogEntry.ascx" %>

I haven't used @Reference before. How should it be used and how is it
different from @Register?
 
R

Rajesh CKR

Add the following line to the .ascx file at the top

<%@ Reference Control="path to ascx file you are loading using load
control"%>

Let me know if it works

The following post may help you understand it better

http://forums.asp.net/thread/1192237.aspx

Also look for MSDN help on @Reference tag


HTH

Raj
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top