• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 837
  • Last Modified:

How to display a literal/label in asp repeater control

Hi All,
I have this code snippet in aspx file

--------------------------------------------------------------------
<asp:repeater id="rptHouses" runat="server" OnItemDataBound="rptHouses_OnItemDataBound">
        <HeaderTemplate>
        <table>
        </HeaderTemplate>
            <ItemTemplate>
                <p>
                             
                    <table>
                   <tr> <td><strong><%# DataBinder.Eval(Container.DataItem, "name") %></strong><br /></td></tr>
                    <tr><td><%# DataBinder.Eval(Container.DataItem, "Address") %><br />
                    <%# DataBinder.Eval(Container.DataItem, "City") %>,
                    <%# DataBinder.Eval(Container.DataItem, "State") %>
                    <%# DataBinder.Eval(Container.DataItem, "zip") %><br /><br /></td></tr>
                  <tr id="trlocal" runat="server"><td> Local: <%# DataBinder.Eval(Container.DataItem, "local") %><br /></td></tr>
                  <tr id="trtollfree" runat="server"><td>Toll Free: <%# DataBinder.Eval(Container.DataItem, "tollfree") %><br /> </td></tr>
                  <tr id="trfax" runat="server"><td>Fax: <%# DataBinder.Eval(Container.DataItem, "fax") %><br /><br /></td></tr>
                   <tr><td> <%# DataBinder.Eval(Container.DataItem, "businesshours") %> </td></tr>
                   <tr id="trhtml" runat="server"><td><%#DataBinder.Eval(Container.DataItem, "html") %></td></tr>
                   
                 
                    </table>
              <%= ViewSpecificInventory.Text %>
               
                </p>
            </ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
        </asp:repeater>
------------------------------------------------------------------------------

And I have this code snippet in code behind file

------------------------------------------------------------------------------
 protected void rptHouses_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
        {
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "local"))))
            {
                HtmlTableRow Trrow = (HtmlTableRow)e.Item.FindControl("trlocal");
                Trrow.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "tollfree"))))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trtollfree");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "fax"))))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trfax");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "html"))))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trhtml");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "location_type_id")).Equals("1"))
            {
                ViewSpecificInventory.Text = "<p><a href=\"/heavy-truck-and-equipment/heavy-truck-part-search-aspx.aspx\">Search Inventory</a></p>";
            }
            else
                ViewSpecificInventory.Text = "<p><a href=\"/search-inventory.aspx\">Search Inventory</a></p>";
-------------------------------------------------------------------------------------------


I want to display ViewSpecificInventory which depends on location_type_id in the database just below every "html" in the repeater control table.

If I define ViewSpecificInventory as literal, I get an error that literal is already in use (since it is repeating). I can display the value of ViewSpecificInventrory (after checking if location_type_id is 1 or something else) in the table. But how do I put the value of ViewSpecificInventory in table then. I am sort of lost.

Thanks.
0
pratz09
Asked:
pratz09
  • 4
  • 2
1 Solution
 
rajvjaCommented:
Hi,

   Instead of <%=ViewSpecificInventoryText%>, place a label.

Read that label in the code behind and update its text.
0
 
pratz09Author Commented:
That is the problem. When i put a label anywhere inside repeater control, it gives error that this label is already in use. May be because the code repeats ?
0
 
pratz09Author Commented:
I have put <asp:Label ID="ViewSpecificInventory" runat="server"></asp:Label>

in place of <%=ViewSpecificInventoryText%>,

And in code behind I do

 protected void rptHouses_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
        {
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "local"))))
            {
                HtmlTableRow Trrow = (HtmlTableRow)e.Item.FindControl("trlocal");
                Trrow.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "tollfree"))))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trtollfree");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "fax"))))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trfax");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "html"))))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trhtml");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "location_type_id")).Equals("1"))
           
                ViewSpecificInventory.Text = "<p><a href=\"/heavy-truck-and-equipment/heavy-truck-part-search-aspx.aspx\">Search Inventory</a></p>";
           
            else
                ViewSpecificInventory.Text = "<p><a href=\"/search-inventory.aspx\">Search Inventory</a></p>";

        }

    }


It says ViewSpecificInventory name does not exist in this context.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
IgorNedCommented:
Just add FindControl :

Label ViewSpecificInventory = (Label)rptHouses.FindControl("ViewSpecificInventory");
if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "location_type_id")).Equals("1"))
      ViewSpecificInventory.Text = "<p><a href=\"/heavy-truck-and-equipment/heavy-truck-part-search-aspx.aspx\">Search Inventory</a></p>";
else
       ViewSpecificInventory.Text = "<p><a href=\"/search-inventory.aspx\">Search Inventory</a></p>";
0
 
pratz09Author Commented:
Now it throws System.NullReferenceException: Object reference not set to an instance of an object.

for assigning ViewSpecificInventory.Text = "..";
0
 
IgorNedCommented:
Sorry the actual assignment should look like this (tested):

Label ViewSpecificInventory = (Label)e.Item.FindControl("ViewSpecificInventory");
0
 
pratz09Author Commented:
That's right. I was going to post it. But yea, we have to use RepeatereventArg to find the asp control (label). It works fine now. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now