Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Getting StartedGetting StartedRepeater OnItemDataBoundRepeater OnItemDataBound
Previous
 
Next
New Post
4/23/2010 6:01 AM
 

I have a repeater which I want to add some text to after I have clicked a button.

But I keep getting the error:

A critical error has occurred.
Object reference not set to an instance of an object.

The code looks like this.

<table class="productList">
    <asp:Repeater ID="Repeater1" OnItemDataBound="ProductList_OnItemDataBound" runat="server">
        <HeaderTemplate>
            <tr>
                <th>Produkt</th>
                <th>Antal</th>
                <th>Antal tryck</th>
                <th>F&auml;rger</th>
                <th>Pris/st</th>
                <th>Schablonkostnad (<asp:Label ID="lbl_stencilHdr" runat="server" /> kr/f&auml;rg)</th>
                <th>Total (ex moms)</th>
                <th>Moms</th>
                <th>Totalt (ink moms)</th>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><asp:Label ID="lbl_product" Text="&nbsp;" runat="server" /></td>
                <td><asp:Label ID="lbl_quantity" runat="server" Text='<%# Eval("productQuantity")%>' /></td>
                <td><asp:Label ID="lbl_printQuantity" runat="server" /></td>
                <td><asp:Label ID="lbl_productColorQuantity" Height="18" runat="server" Text='<%# Eval("ProductColorQuantity")%>' /></td>
                <td><asp:Label ID="lbl_productPrice" Height="18" runat="server" Text='<%# Eval("ProductPrice")%>' /></td>
                <td><asp:Label ID="lbl_stencil" runat="server" /></td>
                <td><asp:Label ID="lbl_totPriceNoVat" runat="server" /></td>
                <td><asp:Label ID="lbl_vat" runat="server" /></td>
                <td><asp:Label ID="lbl_totPriceVat" runat="server" /></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

 

protected void ProductList_OnItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
            Label product       = (Label)e.Item.FindControl("lbl_product");
            product.Text = "123456";

}

What am I doing wrong?

 
New Post
4/23/2010 7:12 AM
 

The ItemDatabound handler is called for each of the sections or templates defined in your markup. In your case, it is being called for both the header template and the item template. Since the header template does not contain a label with id of "lblProduct", e.Item.FindControl("lblProduct") returns a null object. You need to wrap your code with a check on the ItemType:

protected void ProductList_OnItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
            if  ((e.ItemType==ListItemType.Item) || (e.ItemType==ListItemType.AlternatingItem)) {
                         Label product       = (Label)e.Item.FindControl("lbl_product");
                        product.Text = "123456";
            }
}


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
4/23/2010 10:30 AM
 

Aaah! I know this.

My mind went blank when it didn't work.

Thank you for helping!

 
Previous
 
Next
HomeHomeDevelopment and...Development and...Getting StartedGetting StartedRepeater OnItemDataBoundRepeater OnItemDataBound


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out