I always create an ItemDataBound event so I can do it in codebehind. It
seems to be the best way. All I do in that case is, within the itemdatabound
You can then set any properties you want without problem. Of course, you
events to. If you're using PostBackUrl though I'm not sure if it will
"Wozza" <warrenroscoe@hotmail.com> wrote in message
news:O1yzUCvGHHA.5104@TK2MSFTNGP03.phx.gbl...
> Hi,
> I'm trying to create a variable number of LinkButtons using an
> ItemTemplate within a Repeater control. This is being done from code ...
>
> I am trying to recreate something similar to the following ... which is in
> a user control, only I'm doing it in a web part:
> <ul id="middlenavigationMenu" runat="server">
> <asp:Repeater ID="rptContractList" runat="server"
> OnItemCommand="rptContractList_ItemCommand">
> <ItemTemplate>
> <li id="middlenavigationMenuListItem" runat="server">
> <asp:LinkButton ID="lkbContractList" runat="server"
> PostBackUrl='<%# DataBinder.Eval(Container.DataItem, "PostBackUrl") %>'
> Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>'
> CommandArgument='<%# DataBinder.Eval(Container.DataItem,
> "CommandArgument") %>'>
> </asp:LinkButton>
> </li>
> </ItemTemplate>
> </asp:Repeater>
> </ul>
>
> THE ISSUE is. that when the web page is being rendered, the <%# bits don't
> do anything ... they seem to be rendered as tezt (in the html page) and
> therefore ignored in the page output ...
> the end result is that although I get some listitems ... there is no
> LinkButton???
>
> Where am I going wrong ... ???
>
> thanks
> Wozza
>
> =====
>
> My code is below:
>
>
> protected override void CreateChildControls()
> {
> ...
> // Test LinkButton !!!
> linkButtonRepeater = new Repeater();
> linkButtonRepeater.ItemTemplate = new
> LinkButtonItemTemplate(ListItemType.Item);
> HtmlGenericControl linkButtonUl = new HtmlGenericControl(@"ul");
> linkButtonUl.Controls.Add(linkButtonRepeater);
> this.placeHolderTop.Controls.Add(linkButtonUl);
> // Test LinkButton !!!
> ...
> }
>
>
> protected override void Render(HtmlTextWriter writer)
> {
> ...
> // Test LinkButton !!!
> DataTable dt = new DataTable();
> // Define the columns of the table.
> dt.Columns.Add(new DataColumn("PostBackUrl", typeof(String)));
> dt.Columns.Add(new DataColumn("Text", typeof(String)));
> dt.Columns.Add(new DataColumn("CommandName", typeof(String)));
> dt.Columns.Add(new DataColumn("CommandArgument", typeof(String)));
> DataRow dr;
> dr = dt.NewRow();
> dr[0] = @"http://www.google.com";
> dr[1] = @"!!! PRESS ME !!!";
> dr[2] = @"XXX";
> dr[3] = @"
http://linkupv2.external/pages/productpicker.aspx"; > dt.Rows.Add(dr);
> dr = dt.NewRow();
> dr[0] = @"http://www.bbc.co.uk";
> dr[1] = @"!!! PRESS ME !!!";
> dr[2] = @"XXX";
> dr[3] = @"
http://linkupv2.external/pages/productpicker.aspx"; > dt.Rows.Add(dr);
> DataView dv = new DataView(dt);
> linkButtonRepeater.DataSource = dv;
> linkButtonRepeater.DataBind();
> linkButtonRepeater.ItemCommand += new
> RepeaterCommandEventHandler(linkButtonRepeater_ItemCommand);
> // Test LinkButton !!!
> ...
> }
>
>
> namespace TestWebPart
> {
> /// <summary>
> /// <example>
> ///<ItemTemplate>
> /// <asp:LinkButton runat="server" ID="xxx" PostBackUrl="Default.aspx"
> /// Text='<%# DataBinder.Eval(Container.DataItem, "TextNameField") %>'
> /// CommandArgument='<%# DataBinder.Eval(Container.DataItem,
> "CommandArgumentField") %>'
> /// >
> /// </asp:LinkButton>
> ///</ItemTemplate>
> /// </example>
> /// </summary>
> public class LinkButtonItemTemplate : ITemplate
> {
> static int itemcount = 0;
> static string controlNamePrefix = Guid.NewGuid().ToString();
> ListItemType templateType;
> public LinkButtonItemTemplate(ListItemType type)
> {
> templateType = type;
> }
>
> // <asp:LinkButton ID="linkButtonList" runat="server"
> // PostBackUrl='<%# DataBinder.Eval(Container.DataItem, "PostBackUrl") %>'
> // Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>'
> // CommandName='<%# DataBinder.Eval(Container.DataItem, "CommandName")
> %>'>
> // CommandArgument='<%# DataBinder.Eval(Container.DataItem,
> "CommandArgument") %>'>
> // </asp:LinkButton>
> public void InstantiateIn(System.Web.UI.Control container)
> {
> Literal lc = new Literal();
> switch (templateType)
> {
> case ListItemType.Header:
> throw new ArgumentException(@"This template type is not designed for use
> in a header.");
> break;
> case ListItemType.Item:
> lc.Text = String.Format
> (
> @"<li id=""li{0}{1}"" runat=""server"">"
> + @"<asp:LinkButton runat=""server"" ID=""{0}{1}"""
> + @" PostBackUrl='<%# DataBinder.Eval(Container.DataItem, ""PostBackUrl"")
> %>'"
> + @" Text='<%# DataBinder.Eval(Container.DataItem, ""Text"") %>'"
> + @" CommandName='<%# DataBinder.Eval(Container.DataItem, ""CommandName"")
> %>'"
> + @" CommandArgument='<%# DataBinder.Eval(Container.DataItem,
> ""CommandArgument"") %>'"
> + @">{0}{1}</asp:LinkButton>"
> + @"</li>"
> , controlNamePrefix
> , itemcount
> );
> lc.Text = String.Format
> (
> @"<li id=""li{0}{1}"" runat=""server"">"
> + @"<asp:LinkButton runat=""server"" ID=""{0}{1}"""
> + @" PostBackUrl=""HTTP://www.bbc.co.uk"""
> + @">BBC</asp:LinkButton>"
> + @"</li>"
> , controlNamePrefix
> , itemcount
> );
> break;
> case ListItemType.AlternatingItem:
> lc.Text = "<TR><TD bgcolor=lightblue>Item number: " + itemcount.ToString()
> + "</TD></TR>";
> break;
> case ListItemType.Footer:
> throw new ArgumentException(@"This template type is not designed for use
> in a header.");
> break;
> }
> container.Controls.Add(lc);
> itemcount += 1;
> }
> }
> }
>