Currently i'm familiarizing myself with ASP.NET and i've stuck on a problem with dynamic content inside "ItemTemplate" of "Repeater". Here is my code:
<asp:Repeater ID="sidebarRepeater" runat="server">
<ItemTemplate>
<li>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</li>
</ItemTemplate>
</asp:Repeater>
I'm getting a list of links with href == Id of current item and text of this item.
The problem is, that i need a "Onclick" asp.net handler for each of those links (this is menu of "Master" User control, and on each click i shall change contents of child user control, according to selected item in master).
Any tips?
Edit: I've already tryed to use LinkButton like:
<li>
<asp:LinkButton ID='<%# DataBinder.Eval(Container.DataItem, "Id") %>' runat="server" OnClick="ChangeSomething">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</asp:LinkButton>
</li>
Edit2: Currently my markup looks like this:
<li>
<input type="hidden" value="<%# DataBinder.Eval(Container.DataItem, "Id") %>"
<asp:LinkButton runat="server" ID="LinkButton_Office" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'/>
</li>
In this case, it draws only a "hidden input" element only for first item in repeater.
Edit3: full listing of what i currently have(only <li></li> rendered into actual HTML):
<div class="row">
<div class="twelve column">
<div class="row">
<div class="side-bar">
<nav>
<ul>
<asp:Repeater ID="sidebarRepeater" runat="server">
<ItemTemplate>
<li>
<asp:LinkButton ID="linkSidebar" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' OnClick="linkMenu_Click"></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</nav>
</div>
<div class="page">
<asp:PlaceHolder runat="server" ID="contentPlaceholder" />
</div>
</div>
</div>
</div>
Also i forgot to say, that this is actually a .Net user control for Umbraco project.
Datasource is attached for repeater in protected void Page_Load(object sender, EventArgs e) method.
Please try like this
<asp:Repeater ID="sidebarRepeater" runat="server">
<ItemTemplate>
<asp:LinkButton ID="linkMenu" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' OnClick="linkMenu_Click"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
This will keep "Id" field in CommandArgument and "Name" field in Text properties
In linkMenu_Click event handeler, you will get the Id and Name values as the sample code
protected void linkMenu_Click(object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
string id = link.CommandArgument;
string name = link.Text;
//Implement the different display of each menu here ...
}
Problem solved, by adding <form runat="server"> around my <nav> tag.
Seems like every of <asp:button\linkbutton> needs that in order to work properly.
Related
I have a situation where there is HTML inside an anchor tag. And the author should be able to edit both the anchor (href) & other fields.
aspx:
<asp:Repeater ID="rpEvents" runat="server" ItemType="Sitecore.Data.Items.Item">
<HeaderTemplate>
<div class="col-md-3">
</HeaderTemplate>
<ItemTemplate>
<a href="offers/spring.html">
<sc:Image runat="server" Field="offer image" Item=<%#Container.DataItem%> />
<h3><sc:Text runat="server" Field="Offer Title" Item=<%#Container.DataItem%> /></h3>
</a>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
I would do this by turning the <a> into <asp:Hyperlink> & assigning its NavigateURL property from code behind, but then the author cannot edit it in experience editor.
How is this done in Sitecore.
You can wrap the other HTML elements using the Link field, which will allow all fields to continue to be edited from the Experience Editor:
<ItemTemplate>
<sc:Link Field="Offer Link" Item="<%# Container.DataItem %>" runat="server">
<sc:Image Field="Offer Title" Item="<%# Container.DataItem %>" runat="server" />
<h3><sc:Text Field="offer image" Item="<%# Container.DataItem %>" runat="server" /></h3>
</sc:Link>
</ItemTemplate>
The Link can still be set, the image changed or the separate text field edited:
An option is to use the sc:EditFrame inside a repeater, that looks like:
<asp:Repeater runat="server" ID="AccordionRowRepeater">
<ItemTemplate>
<my:AccordionRow runat="server" ID="AccordionRowItem" RowItem="<%# Container.DataItem %>" />
</ItemTemplate>
</asp:Repeater>
And the Row control
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AccordionRow.ascx.cs" Inherits="mynamespace.Controls.AccordionRow" %>
<sc:EditFrame id="EditAccordionItem" runat="server" Buttons="/sitecore/content/Applications/WebEdit/Edit Frame Buttons/Accordion Item">
<li class="<%= LiClass %>">
<a class="trigger" href="#"><sc:Text runat="server" ID="ItemTitle"/></a>
<div class="collapsible">
<sc:Text runat="server" ID="ItemText" />
</div>
</li>
</sc:EditFrame>
And add a Field Editor Button to the Edit Frame Buttons
More about this Accordion example see User friendly developing with the Sitecore Experience Editor
Or, I often use this simple solution. It also give you the opportunity to display some help text to the content editor.
<div runat="server" id="PageditorDiv" Visible="False">
URl: <sc:Link runat="server" ID="link"/>
</div>
And in the code Behind.
if (Sitecore.Context.PageMode.IsPageEditor)
{
PageditorDiv.Visible = true;
}
Below is my code:
<asp:ListView ID="lViewMenu" runat="server">
<ItemTemplate>
<li class="context-menu-group"><span class="color-bar color-13"></span>
<asp:LinkButton ID="lbtnMenu" runat="server" Text='<%# Eval("MenuName")%>' OnClick="lbtnMenu_Click"></asp:LinkButton>
</li>
</ItemTemplate>
</asp:ListView>
And i need to change the class name of <li> to context-menu-group active when a LinkButton is clicked on the onclick() How?
I have a repeater control that has a LinkButton in it. Using command argument, I perform necessary functions in the code-behind. I want to turn the forecolor of a LinkButton to red after it is clicked and if another LinkButton is clicked in the Repeater, I want to turn off the previously clicked LinkButton to black and the recently clicked one to red. How do I do that? Here is my code:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("divisionlabel") %>'
OnCommand="LinkButton1_Command" CommandName="MyPendingApps" CommandArgument='<%#Eval("divisionid") %>'>
</asp:LinkButton>
</li>
</ItemTemplate>
<FooterTemplate>
<li>
<asp:LinkButton ID="LinkButton_InsReqList" runat="server" OnCommand="LinkButton_InsReqList_Command"
CommandName="InspectionRequests">Inspection Requests List</asp:LinkButton>
</li>
</ul>
</FooterTemplate>
</asp:Repeater>
Add a OnClick="LinkButtons_Click" in every linkbutton on the form
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("divisionlabel") %>'
OnCommand="LinkButton1_Command" CommandName="MyPendingApps" CommandArgument='<%#Eval("divisionid") %>' OnClick="LinkButtons_Click">
</asp:LinkButton>
</li>
</ItemTemplate>
<FooterTemplate>
<li>
<asp:LinkButton ID="LinkButton_InsReqList" runat="server" OnCommand="LinkButton_InsReqList_Command"
CommandName="InspectionRequests" OnClick="LinkButtons_Click">Inspection Requests List</asp:LinkButton>
</li>
</ul>
</FooterTemplate>
</asp:Repeater>
and the code for that function is:
protected void LinkButtons_Click(object sender, EventArgs e)
{
LinkButton link=(LinkButton)sender;
if (link.ID == "yourDesiredID1")
{
link.ForeColor=System.Drawing.Color.Red;
}
else
{
link.ForeColor=System.Drawing.Color.YourDefaultdColor;
}
if (link.ID == "yourDesiredID2")
{
link.ForeColor=System.Drawing.Color.Yellow;
}
else
{
link.ForeColor=System.Drawing.Color.YourDefaultColor;
}
if (link.ID == "yourDesiredID3")
{
link.ForeColor=System.Drawing.Color.Blue;
}
else
{
link.ForeColor=System.Drawing.Color.YourDefaultColor;
}
}
I would store a unique identifier (value, Id, ItemIndex etc) from the the ItemCommand event server side in ViewState and then in the ItemDataBound event check if unique indentifier equals that of the value in ViewState, if it does apply the class that changes the text colour to red, if not carry on binding.
Obviously there are some suttleties here that you need to think about but its a start.
i am trying to display a list of products
i am using a repeater, the repeater is working perfectly but its not showing the information as i want. i want to display in columns each colums has 6 products and it repeat depending on the number of products.
this is a sample of the code that i am using
page.aspx.cs
AllProducts = pm.GetProductOfMerchantByCat(ID, catid);
ProductRepeater.DataSource = AllProducts;
ProductRepeater.DataBind();
page.aspx
<asp:Repeater id="ProductRepeater" runat="server" Visible="true">
<HeaderTemplate>
<ul id="ProductsContent" class="jcarousel-skin-tango">
</HeaderTemplate>
<ItemTemplate>
<li>
<div class="product">
<h4><%# DataBinder.Eval(Container.DataItem, "Name")%></h4>
<asp:HiddenField ID="HiddenFeildQuantity"
Value='<%# Eval("Quantity") %>'
runat="server" />
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
any help would be great .. thank you
Hey as per my Understanding you need to use DataList Instead of Repater.
Check This property
RepeatColumns
RepeatDirection
MSDN : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.aspx
I have nested repeaters like this:
<asp:Repeater ID="rptQuestoes" runat="server">
<HeaderTemplate>
<ol class="orderedList">
</HeaderTemplate>
<ItemTemplate>
<li>
<%#DataBinder.Eval(Container.DataItem, "QuestionName")%>
<asp:Repeater ID="rptAlternativas" DataSource='<%# Container.DataItem.Row.GetChildRows("Questionario") %>' runat="server">
<HeaderTemplate>
<ul style="list-style-type: none">
</HeaderTemplate>
<ItemTemplate>
<li>
<input id="rb" type="radio" name='ITEM_INDEX_HERE' value='<%#Container.DataItem("AlternativeID")%>' /><%#Container.DataItem("AlternativeName")%>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate>
</ol>
</FooterTemplate>
</asp:Repeater>
I want to print the parent item index inside the child repeater (rptAlternativas) in VB right in ITEM_INDEX_HERE marker. How can I achieve this?
I hope someone can give you a better answer than this, but I would consider adding a property to your "Quentionario" sub elements.
<asp:Repeater ID="rptAlternativas"
DataSource='<%# Container.DataItem.Row.GetChildRows("Questionario") %>'
runat="server">
Can you add a question id to each of these objects? You're already binding to their AlternativeId in your radio button, adding a question id to these objects would let you do simply:
<input id="rb" type="radio" name='<%#Container.DataItem("QuestionId")%>' value='<%#Container.DataItem("AlternativeID")%>' />
Even if these objects are auto-generated from an ORM, you should be able to add the property in via a partial class, and then just set the appropriate values in a simple loop.