Link to home
Create AccountLog in
Avatar of fwsteal
fwsteal

asked on

c# 2.0 how to append to a repeater control

How do I append to two hyperlink controls within a repeater?

My error:
Error 1 'System.Web.UI.WebControls.Repeater' does not contain a definition for 'items'

--------------------------
    <table width="100%" border="0" cellspacing="5" cellpadding="5">
    <tr>
    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <td>
     <asp:HyperLink
      ID="HyperLink1"
      Runat="server"
      Text='<%# Eval("Name") %>'
      NavigateUrl='<%# "../Item.aspx?DepartmentID=" + Eval("departmentId") + "&CategoryID=" + Eval("categoryId") + "&ProductID=" + Eval("ProductID")%>'
      ImageUrl='<%# "../images/productimages/" + Eval("Image1FileName") %>'
      ToolTip='<%# Eval("Name") %>'
      CssClass='<%# Eval("ProductID").ToString() == Request.QueryString["ProductID"] ? "ProductSelected" : "ProductUnselected" %>'
      >
    </asp:HyperLink>
    <br />
    <asp:HyperLink ID="HyperLinkName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' NavigateUrl='<%# "../Item.aspx?DepartmentID=" + Eval("departmentId") + "&CategoryID=" + Eval("categoryId") + "&ProductID=" + Eval("ProductID") %>' >
    </asp:HyperLink>
    <br />
    <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")%>
    <br />
    </td>
    </ItemTemplate>
      <HeaderTemplate>
      <strong>
    Products</strong>
  </HeaderTemplate>
    </asp:Repeater>
    </tr>
    </table>
--------------------------

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string categoryId = Request.QueryString["CategoryID"];
            string ep = Request.QueryString["ep"];
            if (categoryId != null)
            {
                Repeater1.DataSource = CatalogAccess.GetAllProductsInCategory(categoryId, ep);
                Repeater1.DataBind();
                Repeater1.items(0).FindControl("HyperLink1") += "&ep=" + ep; //error
      Repeater1.items(0).FindControl("HyperLinkName") += "&ep=" + ep; //error
            }
        }
    }
----------------------------
Avatar of jbarber2005
jbarber2005

1st): Repeater1 has a property Items (which is case sensitive)
2nd): FindControl returns a type of Control, so you have tell it what type of control.  Here is an example (C#):

HyperLink lnk1 = (HyperLink)Repeater1.Items[0].FindControl("Hyperlink1");
lnk1.NavigateUrl += "&ep=" + ep;

HyperLink lnk2 = (HyperLink)Repeater1.Items[0].FindControl("HyperLinkName");
lnk2.NavigateUrl += "&ep=" + ep;

I am not sure if you are just doing this to Item 0, but if you want it performed on all items, you need to have a Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) event.
Avatar of fwsteal

ASKER

I'm trying to do this to all the hyperlink items in the repeater; I'm not sure how to do that.
Avatar of fwsteal

ASKER

this?

<asp:Repeater ID="Repeater1" runat="server" OnDataBinding="AppendEP">

    protected void AppendEP(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        string ep = Request.QueryString["ep"];

        HyperLink lnk1 = (HyperLink)Repeater1.Items[0].FindControl("Hyperlink1");
        lnk1.NavigateUrl += "&ep=" + ep;

        HyperLink lnk2 = (HyperLink)Repeater1.Items[0].FindControl("HyperLinkName");
        lnk2.NavigateUrl += "&ep=" + ep;
    }
Avatar of fwsteal

ASKER

changed it sort of:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string ep = Request.QueryString["ep"];

        HyperLink lnk1 = (HyperLink)Repeater1.Items[0].FindControl("Hyperlink1"); //errors
        lnk1.NavigateUrl += "&ep=" + ep;

        HyperLink lnk2 = (HyperLink)Repeater1.Items[0].FindControl("HyperLinkName"); //errors
        lnk2.NavigateUrl += "&ep=" + ep;
    }

error:   Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index



ASKER CERTIFIED SOLUTION
Avatar of jbarber2005
jbarber2005

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer