[RESOLVED] Loading a usercontrol into a webpage

Guys: asp.net c# question

I ma trying to load a usercontrol into a table. My usercontrol contains some literal controls.

I can't set the text of these literal controls to anythign, as in my code-behind of my control, whenever I try to access my controls, they come back as null.

My user control code:

<div style="color: Black; background-color: White;" mce_style="color: Black; background-color: White;">
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OfferControl.ascx.cs"
Inherits="EOwner.Site.French.Controls.OfferControl" %>
<link rel="Stylesheet" href="../styles/OfferControl.css" type="text/css" />
<div id="all" style="background-color: #FFFFF0; height:100%;">
<div id="LeftPanel" style="float: left; width: 65%;">
<div id="title">
<asp:Literal ID="litTitle" runat="server" Text="TitleLabel"></asp:Literal>
<div id="Blurb">
<asp:Literal ID="litBlurb" runat="server" Text="BlurbLabel" ></asp:Literal>
<div id="Button">
<asp:ImageButton ID="btnViewOffer" runat="server"
ImageUrl="~/images/ViewOfferButton.jpg" ImageAlign="AbsMiddle" />
<div id="RightPanel" style="float: right; width: 35%;">
<div id="address">
<asp:Literal ID="litAddress" runat="server" Text="AddressLabel"></asp:Literal>
<div id="map">


and the .cs

<div style="color: Black; background-color: White;" mce_style="color: Black; background-color: White;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EOwner.Contracts.Entities;

namespace EOwner.Site.French.Controls
public partial class OfferControl : System.Web.UI.UserControl
protected void Page_Load(object sender, EventArgs e)


public Literal lit_Title
get { return litTitle; }
set { litTitle = value; }

public Literal lit_Blurb
get { return litBlurb; }
set { litBlurb = value; }

public Literal lit_Address
get { return litAddress; }
set { litAddress = value; }

public ImageButton btn_ViewOffer
get { return btnViewOffer; }
set { btnViewOffer = value; }

And the code that creates the control



<div style="color: Black; background-color: White;" mce_style="color: Black; background-color: White;">
foreach (OfferEntity o in offers)
OfferControl oc = new OfferControl();
oc.LoadControl("~/Controls/OfferControl.ascx");//GridViewRow gr = new GridViewRow(offers.IndexOf(o), offers.IndexOf(o), DataControlRowType.DataRow, DataControlRowState.Normal);
oc.ID = "oc_" + o.OfferId.ToString();
oc.Attributes.Add("OfferId", o.OfferId.ToString());
oc.lit_Title.Text = o.Title; //Request.QueryString["Title"].ToString();
oc.lit_Blurb.Text = o.Blurb;//Request.QueryString["Blurb"].ToString();
oc.btn_ViewOffer.Attributes.Add("OfferId", o.OfferId.ToString());
//address town postcode
oc.lit_Address.Text = o.Address + Environment.NewLine + o.Town + Environment.NewLine + o.Postcode + Environment.NewLine + o.WebAddress;

TableRow tr = new TableRow();
TableCell tc = new TableCell();
//pass in offer id, list is stored in the session, use the id in the usercontrol to get the data


//tbl.Rows.AddAt(gvOrderDetail.Rows.Count + 1, row);


Despite having the get/sets for every control, I can't seem to set the values of the controls, keep getting a nullreferenceexception. Even if I try to set the values of the controls in the usercontrol itself, I still can't access the controls.

Any pointers?

Andrew Berry - Software analyst/Developer

thoughts on "[RESOLVED] Loading a usercontrol into a webpage"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>