How to find a nested control inside of asp repeater - c#

I'm trying to find <span id="source"> inside of my repeater the listControl is coming back null.
<asp:Repeater id="rptRssFeed" runat="server" OnItemDataBound="RssFeedItemDataBound">
<HeaderTemplate><ul></HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li id="socialListItem" runat="server">
<%# GetTitle(Container.DataItem) %>
<span class="source" id="source" runat="server"><%# GetSource(Container.DataItem) %></span>
<p><%# GetTeaser(Container.DataItem) %></p>
</li>
</ItemTemplate>
<AlternatingItemTemplate>
<li class="alt" id="socialListItem" runat="server">
<%# GetTitle(Container.DataItem) %>
<span class="source" id="source" runat="server"><%# GetSource(Container.DataItem) %></span>
<p><%# GetTeaser(Container.DataItem) %></p>
</li>
</AlternatingItemTemplate>
</asp:Repeater>
public void RssFeedItemDataBound(object sender, RepeaterItemEventArgs e)
{
HtmlGenericControl listControl = (HtmlGenericControl)e.Item.FindControl("socialListItem");
if (!ShowSource)
{
HtmlGenericControl spanControl = (HtmlGenericControl)listControl.FindControl("source");
spanControl.Visible = false;
}
listControl.Attributes["class"] += ((XmlFeedItem)e.Item.DataItem).XmlFeedType;
}

You need to check if you are checking for the List Item in the ListItemType.Item or AlternatingItem type.
public void RssFeedItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlGenericControl listControl = (HtmlGenericControl)e.Item.FindControl("socialListItem");
if (listControl != null)
{
if (!ShowSource)
{
HtmlGenericControl spanControl = (HtmlGenericControl)listControl.FindControl("source");
spanControl.Visible = false;
}
listControl.Attributes["class"] += ((XmlFeedItem)e.Item.DataItem).XmlFeedType;
}
}
}

Related

Get values of checkbox in repeater

I have a repeater inner another repeater and this second one i have a list of checkbox and i need to get the value of the checked.
This is my front code:
<asp:Repeater runat="server" ID="rptPerfis" OnItemDataBound="ItemBound">
<ItemTemplate>
<div class="mws-form-row">
<ul class="mws-form-list inline" style="float: none; display: inline;">
<li style="padding-top: 10px;">
<%# rptNome(Container) %></li>
</ul>
<asp:Repeater runat="server" ID="rptUsers">
<ItemTemplate>
<div class="mws-form-item radioPermissoes clearfix" style="float: none;">
<ul class="mws-form-list inline">
<li>
<asp:CheckBox runat="server" Text="<%# rptAdministradorNome(Container) %>" ID="checkUser" CssClass="<%# rptAdministradorPostClass(Container) %>" /></li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<br />
<hr />
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton runat="server" ID="fLnkSalvar" class="mws-ic-16 ic-disk" OnClick="fLnkSalvar_Click">Salvar</asp:LinkButton>
and this is how i fill this repeater:
protected void Page_Load(object sender, EventArgs e)
{
listaAdm = Servicos.AdministradorMySql.ListarEmpresa(denuncia.Empresa).OrderByDescending(x => x.Nome).ToList();
todosPerfis = Servicos.Perfil.ListarTodos().ToList();
rptPerfis.DataSource = todosPerfis.Where(x => x.Ativo).OrderBy(x => x.Nome);
rptPerfis.DataBind();
}
protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
int idPerfil = ((Perfil)args.Item.DataItem).ID;
Repeater childRepeater = (Repeater)args.Item.FindControl("rptUsers");
childRepeater.DataSource = listaAdm.Where(x => x.Perfil > 1 && x.Perfil == idPerfil).ToList();
childRepeater.DataBind();
}
}
protected void fLnkSalvar_Click(object sender, EventArgs e)
{
Administrador usuario = new Administrador();
usuario.Permissoes = new List<string>();
// i need to get this values here to fill this `List<string>` and then save
foreach (var x in usuario.Permissoes)
{
Servicos.Denuncia.InserirUsuarios(denuncia.ID, x);
}
}
I've no idea how can i get this values or if there another easier way without add in the list i think its better
You have to use FindControl on multiple levels. First the correct Item in the parent Repeater, then find the CheckBox in the correct Item of the child Repeater.
var cb = ((Repeater)rptPerfis.Items[i].FindControl("rptUsers")).Items[j].FindControl("checkUser") as CheckBox;
PS you need to wrap the code in Page_Load in an IsPostBack check or you will never retrieve the correct checkbox state in a PostBack.

how to access a header template in c#

I have a template:
<asp:Repeater ID="litFolder" runat="server" OnItemDataBound="litFolder_ItemDataBound">
<HeaderTemplate>
<ul class="test" id="currentLink">
</HeaderTemplate>
<ItemTemplate>
<div class="leftNav">
<li>
<asp:HyperLink ID="innerHyperLink" runat="server"></asp:HyperLink>
</li>
</div>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
and I'm trying to set the to display block when a link is selected. I can set the link to display block, but how do I set the ul to display block ( only using C# )
protected void litFolder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// if the child from the first repeater has children, it will grab them here
Item innerItem = (Item)e.Item.DataItem;
if (innerItem != null)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// this creates a link to the page in sitecore once clicked
HyperLink topNavigation = (HyperLink)e.Item.FindControl("innerHyperLink");
topNavigation.NavigateUrl = LinkManager.GetItemUrl(innerItem);
topNavigation.Text = innerItem["Title"];
if (topNavigation != null) {
//this is where I think I need to define the ul to display block
}
}
}
}
I need to make sure that the current link sets the ul that it is in and not all the ul's with the class test.
In order to prevent the Ul from being broken, place the <div class="leftNav"> inside the <li> as shown below:
<asp:Repeater ID="litFolder" runat="server" OnItemDataBound="litFolder_ItemDataBound">
<HeaderTemplate>
<ul class="test" id="currentLink">
</HeaderTemplate>
<ItemTemplate>
<li>
<div class="leftNav">
<asp:HyperLink ID="innerHyperLink" runat="server"></asp:HyperLink>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
To set the display block style, you can find the control based on the ID, then set the style to it.
Thanks

Fill megamenu dynamically

I have this functions that gets categories in three levels.
One that gets ParentCategories
One that gets categories by ParentCategoryID
One that gets Subcategories by categoriesID.
I want to build a megamenu looking like this site:
http://www.potterybarn.com/
HTML Markup
<div>
<asp:Repeater ID="HorizMenuRepeater" runat="server">
<HeaderTemplate>
<ul id="mega-menu"></HeaderTemplate>
<ItemTemplate>
<li><a id="mBox" href="javascript:;">'<%#Eval("ParentCatName")%>'</a></li></ItemTemplate>
<FooterTemplate></ul></FooterTemplate></asp:Repeater>
</div>
<script>
window.addEvent('domready', function () {
new mBox.Tooltip({
content: 'MegamenuUC',
setStyles: { content: { padding: 15, lineHeight: 20 } },
position: {
x: 'right',
y: 'bottom'
},
attach: 'mBox',
closeOnMouseleave: true
});
});
</script>
<div id="MegamenuUC" style="display:none">
<uc1:Megamenu ID="Megamenu1" runat="server" />
</div>
result
Code of the usercontrol
<ul>
<h3>Category</h3>
<ul>
<li>Sub category</li>
</ul>
</ul>
I want to make this dynamically, now it's just the parentcategory that is dynamic.
I need somehow pass the id to know witch parentcategory you hovered over tho pas the right id to my function that gets the categories so I can populate my usercontrol with it
Thanks
I solved it and i used a repeater to create a clean list and a jquery plugin to create megamenu
Link to jquery plugin http://www.designchemical.com/lab/jquery-mega-drop-down-menu-plugin/options/
Code for databind repeater with linq
<div>
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_OnItemBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><a><%# DataBinder.Eval(Container.DataItem, "ParentCatName") %></a>
<asp:Repeater ID="ParentCatRepeater" runat="server" OnItemDataBound="ChildRepeater_OnItemBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><a><%# DataBinder.Eval(Container.DataItem, "CategoryName") %></a>
<asp:Repeater ID="ChildRepeater" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><a><%# DataBinder.Eval(Container.DataItem, "ProductName") %></a></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
LinqtoDBDataContext db = new LinqtoDBDataContext();
ParentRepeater.DataSource = db.GetParentCategories();
ParentRepeater.DataBind();
}
protected void ParentRepeater_OnItemBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dynamic cat = e.Item.DataItem as dynamic;
int parentcatid = Convert.ToInt32(cat.ParentCatID);
LinqtoDBDataContext db = new LinqtoDBDataContext();
//var cats = from c in db.Categories
// where c.ParentCatID == parentcatid
// select c;
Repeater ParentCatRepeater = e.Item.FindControl("ParentCatRepeater") as Repeater;
ParentCatRepeater.DataSource = db.GetCategories(parentcatid);
ParentCatRepeater.DataBind();
}
}
protected void ChildRepeater_OnItemBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dynamic prod = e.Item.DataItem as dynamic;
int catid = Convert.ToInt32(prod.CategoryID);
LinqtoDBDataContext db = new LinqtoDBDataContext();
Repeater ChildRepeater = e.Item.FindControl("ChildRepeater") as Repeater;
ChildRepeater.DataSource = db.GetProductsInCategory(catid);
ChildRepeater.DataBind();
}
}
}

Repeater which Event to use to find a control inside the repeater

I use asp.net and c#4.
I have a repeater within inside a HyperLink Control.
I need to Find the HyperLink Control and change some of its properties with some logic before rendering it on the page.
With my code here posted I get a null value for the control so I'm not able to get it.
Any idea what I'm doing wrong? Thanks for your time on this.
<asp:Repeater ID="RepeaterEditorsChoice" runat="server" DataSourceID="ObjectDataSourceEditorsChoice"
OnItemCreated="RepeaterEditorsChoice_ItemCreated" OnItemDataBound="RepeaterEditorsChoice_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="uxLink" runat="server"></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
CODE BEHIND:
protected void RepeaterEditorsChoice_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HyperLink myLink = (HyperLink)((Repeater)sender).FindControl("uxLInk"); // ERROR: it is null
dynamic o = e.Item.DataItem;
if (o.TypeContent == "AR")
{
myLink.Text = #"'<%# Eval(\""Title\"") %>'";
myLink.NavigateUrl = #"'<%# GetRouteUrl(""ArticleDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
}
if (o.TypeContent == "BP")
{
myLink.Text = #"'<%# Eval(\""Title\"") %>'";
myLink.NavigateUrl = #"'<%# GetRouteUrl(""BlogPostDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
}
}
}
On item data bound
then just (YourClass)e.Item.FindControl("its name");

C# Pass textbox content via CommandArgument in Repeater

<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
<ItemTemplate>
<p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
<hr class="hr4" />
<p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
<div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton></div>
</ItemTemplate>
</asp:Repeater>
How can I pass the textbox value within CommandArgument? Sorry totally lost...
Did you try: CommandArgument='<%#Eval("cartID") %>'
this is different from yours as it is surrounded by a single quote, I guess this is the correct syntax.
Use FindControl to get other items in the repeater Item. For Example:
protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
LinkButton lb = (LinkButton)e.CommandSource;
string textBoxValue = ((TextBox)lb.Parent.FindControl("cartQty")).Text;
}
you need to bind the cartId to the linkbutton onItemDataBound and then access it onItemCommand, I have modified code for you, give this a go
<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Repeater_OnItemCommand" OnItemDataBound="Repeater_OnItemDataBound">
<ItemTemplate>
<p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
<hr class="hr4" />
<p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
<div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" Text="Buy"></asp:LinkButton></div>
your onItemdatabound should look like this
protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//your code...
LinkButton add = (LinkButton)e.Item.FindControl("buy");
add.CommandArgument = cartID.ToString();
}
and then you can access the text box on item command like this
protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "AddtoCart")
{
LinkButton btnEdit = (LinkButton)e.CommandSource;
if (btnEdit != null)
{
string editId = btnEdit.CommandArgument;
string text = ((TextBox)e.Item.FindControl("cartQty")).Text;
//do some stuff with your cartid and quantity
}
}
}
You can also extend your code with edit/delete command arguments by adding more linkbuttons and binding them to the correct command and then accessing them in on item command
Thanks

Categories