views:

1058

answers:

3

Hi All,

I have a user control that displays a list of categories. In that user control I have a Label control that I would like to write to from the code behind file. This is my Label

I have tried this code:

Label lblCount = (Label)this.Page.FindControl("Label1");
lblCount.text = "some text";

How can I get access to write to the label from the user control code behind page? What code would I need. I keep getting this error: Object reference not set to an instance of an object.

Any help would be greatly appreciated.

A: 

Here is my full user control ascx code:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="homepagelinks.ascx.cs" Inherits="Controls_homepagelinks" EnableViewState="false" %>
 <div class="browseheaderbox">
        <asp:Image ID="Image3" runat="server" ImageUrl="~/App_Themes/TLP/images/icons/search_main.gif"
            AlternateText="Browse Business Categories" Width="34px" Height="31px" Style="float: left;" />
        <h2 class="pad0">
            Browse Business Categories</h2>
    </div>
 <asp:ListView ID="lvHomePageLinks" runat="server">
    <LayoutTemplate>
        <div id="homepagelinks">
            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            <div class="clearer">&nbsp;</div>
        </div>
    </LayoutTemplate>
    <ItemTemplate>
        <div class="linkcatsHome">
            <h5>
            <asp:Image ID="Image1" runat="server" ImageUrl="~/App_Themes/TLP/images/icons/icon_alltrades.png" AlternateText='<%# Eval("txtCategory") %>' />
            <%# String.Format("<a href=\"directory/{0}.aspx\">{1}</a>", Eval("txtCategoryURL").ToString(), g.CapitalizeWords(Eval("txtCategory").ToString()))%></h5>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

            <%# GetHomePageSubCats(Convert.ToInt32(Eval("intCategoryID")), Convert.ToInt32(g.homepagesubcatamount))%>
        </div>
    </ItemTemplate>
    <EmptyDataTemplate>
        Sorry No Data To Display
    </EmptyDataTemplate>
    </asp:ListView>

here is the code benhid file:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;


public partial class Controls_homepagelinks : System.Web.UI.UserControl
{
    public CategoryBLL CategoryBLL = new CategoryBLL();
    public ListingBLL ListingBLL = new ListingBLL();
    public general g = new general();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlDataReader reader;
            reader = CategoryBLL.GetRandomCategories_dr();

            Label lbl = (Label)this.Page.FindControl("Label1");
            lbl.Text = "some text";

            lvHomePageLinks.DataSource = reader;
            lvHomePageLinks.DataBind();
            reader.Close();
            reader.Dispose();
        }
    }
}
Jason
+1  A: 

i usually do it like this

<asp:Label ID="Label1" runat="server" Text='<%# GetAmount() %>'></asp:Label>

then have a method like this in the code behind.

protected string GetAmount()
{
    return "some text";
}

The GetAmount() method will be called for every row in the page. You can pass parameters from the datasoure using Eval("ColumnName") if you need them.

Al W
Awesome AI W, that worked great. I didn't think it would be so much trouble writing to a label in a user control. Thank You.
Jason
A: 

The reason you get that error is because you are trying to set the value too early in the page lifecycle. At Page_Load the ListView hasn't made it's items yet and so you can't get to them.

For controls that use templates, ListViews, Repeaters etc I use the "OnItemCreated" event - it fires for each item that's been made and you have access to all kinds of things.

So use the following:

<asp:ListView ID="lvHomePageLinks" OnItemCreated="ListItems_Created" runat="server">

Then in your code-behind:

protected void ListItems_Created(object sender, ListViewItemEventArgs e)
{
 ((Label) e.Item.FindControl("Label1")).Text = "some text";
}