Creat dynamic menu in asp.net - asp.net

I am trying to create a side menu with all the categories (and later on sub menu with subcategories)
found this code online but I am not exactly sure what is going on
asp:Repeater id="MenuRepeater" runat="server">
<headertemplate>
<ul>
</headertemplate>
<itemtemplate>
<li></a></li>
</itemtemplate>
<footertemplate>
</ul>
</footertemplate>
</asp:Repeater>
what should I enter between the li tags and what code is needed using LINQ?

Repeater is DataBound Control so it needs IDataSource to bind the data in/out it, you can write your LINQ query to get the menu from the database and call .ToList() method and bind it to the repeater control.
For example:
<asp:Repeater id="MenuRepeater" runat="server">
<headertemplate>
<ul>
</headertemplate>
<itemtemplate>
<li><%# Bind("MenuItem") %></li>
</itemtemplate>
<footertemplate>
</ul>
</footertemplate>
</asp:Repeater>
in the code behind you'll create your IQueryable object and convert it to IList by calling .ToList() like this:
var query = from m in Context
select m.MenuItem;
MenuRepeater.DataSource = query.ToList();
MenuRepeater.DataBind();

Related

Nested CheckBoxList using DataSet and Repeaters

I'm trying to wire up some CBL's using a repeater and dataset. The dataset contains 2 tables with the same schema and a (one, single) relation (in SQL land think of it as a self join).
When the control is rendered if I set my DataSource = the relationship, I am able to get the children elements to show; so I know the model is good, although misplaced (on purpose to test the model - see code below).
The problem is:
I am having difficulty getting the parent elements to show up at all
Not all parents have children, and the ones that don't still need to show up
Am I approaching this in the right frame of mind? i.e. Am I missing something fundamental? Implementation outside of CBL(plain text) works fine per this article
<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ParentCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem,"Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ChildCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Page Load is nothing spectacular
DataSet ds = Foo.foo();
ParentRepeater.DataSource = ds.Tables["Parent"];
ParentRepeater.DataBind();
In your code you don't bind ChildRepeater
If you want just change plain text to Checkbox in according to https://support.microsoft.com/en-us/kb/306154
then you don't need to use CheckBoxList. But you should only use single checkbox inside repeater.

Using ItemType for strongly typed repeater control?

Okay, so I looked up some cool stuff about strongly typed repeater controls... the only issue is that it won't work. I have a List<Entry> that I've bound my repeater to. I just want to display the data. Normally I use ((Entry)(Container.DataItem)), but from what I've read I can just declare the type in the ItemType.
Well... that's what I tried to do, but I get nothing. What am I messing up here?
<asp:Repeater ID="UserRptr" ItemType="HROpenEnrollment.Classes.Entry" runat="server">
<ItemTemplate>
<ul class="UserList">
<li class="CompoundField">
<%# ???? I can't use Item here. %>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
I would guess that it's not finding my entry class... how do I get that in there? It's in the same namespace, in a separate folder.
You need to bind data in code behind like below:
using (MicroGOVEntities entities = DataEntitiesFactory.GetInstance())
{
var getGovernments = from g in entities.S_Government
orderby g.DateCreated descending
select g;
rpData.DataSource = getGovernments.ToList();
}
rpData.DataBind();
And the ASP.NET code is:
<asp:Repeater ID="rpData" runat="server"
ItemType="MicroGOV.Entity.S_Government"
OnItemCommand="rpData_ItemCommand">
<ItemTemplate>
<td><%#:Item.GovernmentID %></td>
</ItemTemplate>
</asp:Repeater>

Insert a User Control within an ASP:Repeater

I need to insert a user control into a repeater
<asp:Repeater runat="server" ID="rptAdditionalPages">
<ItemTemplate>
<div id="<%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).DivID %>" class="tab_content">
<h1><%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).Title %></h1>
<%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).Body %>
<uc:EnrollmentMethod ID="EnrollmentMethod2" runat="server" />
</div>
</ItemTemplate>
</asp:Repeater>
When the user control is inserted with this method the code behind cannot find EnrollmentMethod2. Is it possible to add user controls in repeaters?
You can't find it because it's nested in the repeater. You will need to dig into the repeater to find it.
foreach (RepeaterItem item in Repeater1.Items)
{
EnrollmentMethod control = (EnrollmentMethod )item.FindControl("EnrollmentMethod2");
}

Handling a DropDownList in a Nested ListView

I have two nested .net ListViews. I need to filter the contents of the inner listview by selecting from a dropdown list in the outer listview. The layout of the control is like this:
<asp:listview id="lvOuter" runat="server" onitemdatabound="lvOuter_OnItemDataBound"
onitemcommand="lvOuter_OnItemCommand" onitemcreated="lvOuter_ItemCreated" onselectedindexchanged="lvOuter_SelectedIndexChanged">
<layouttemplate>
<ul>
<asp:placeholder id="itemPlaceholder" runat="server" />
</ul>
</layouttemplate>
<itemtemplate>
<asp:dropdownlist id="ddlTest" runat="server" onselectedindexchanged="ddlTerm_SelectedIndexChanged" autopostback="true" >
<asp:listitem text="6" value="6"/>
<asp:listitem text="9" value="9"/>
</asp:dropdownlist>
<asp:listview id="lvInner" runat="server" onselectedindexchanging="lvInner_SelectedIndexChanging"
onitemdatabound="lvInner_ItemDataBound" onitemcommand="lvInner_OnItemCommand">
<layouttemplate>
<asp:placeholder id="itemPlaceholder" runat="server" />
</layouttemplate>
<itemtemplate>
<!--Results displayed here-->
</itemtemplate>
</asp:listview>
</itemtemplate>
</asp:listview>
On the page load I bind data to the outer listview and on OnItemDataBound I bind the inner listview. This is all working fine, but what I need to do now is to re-bind the inner listview with a new query that includes the parameter from the drop down list. This should happen when the user selects a new value in the drop downlist. (OnSelectedIndexChanged) I can access the value easily enough through the sender, as so:
protected void ddlMyDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlMyDropDown = sender as DropDownList;
string value = ddlMyDropDown.SelectedValue;
}
but I'm unable to then find the inner listview relating to that dropdown in order to bind the results of the new query with the added where clause. I'm sure this must be a pretty common requirement. Any pointers would be greatly appreciated.
Have you tried using something like <%# Eval("value", "{0}") %> as seen here
I seem to recall doing something akin to yours with this. It has been awhile since I have done this so sorry if this is not accurate.

Moving repeater in form removes serverside declaration

I have two repeaters bound to object collections. They both work fine. But when I move repeater B to repeater A's FooterTemplate the serverside declaration of repeater B disappear.
This is not a parent/child relation.
Any ideas why this happens?
Update:
<asp:Repeater ID="myOuterRepeater" runat="server">
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
<asp:Repeater ID="myInnerRepeater" runat="server">
<ItemTemplate>
<asp:Button OnCommand="btnRemove_Click" ID="btnRemove" RunAt="server" />
<%#Eval("ItemId")%>
<%#String(Eval("Amount"))%>
<ItemTemplate>
</asp:Repeater>
</FooterTemplate>
</asp:Repeater>
When I bind:
myOuterRepeater.DataSource = myCollection
myOuterRepeater.DataBind()
Dim innerRepeater As Repeater =
myOuterRepeater.Controls(myOuterRepeater.Controls.Count - 1).Controls(0).FindControl("myInnerRepeater")
innerRepeater.DataSource = myInnerCollection
innerRepeater.DataBind()
When nesting a control (including a second repeater) inside a repeater, it is not longer part of the page but part of the enclosing repeater.
You will need to use FindControl(string) to get a reference to the nested repeater:
Repeater nested = enclosingRep.FindControl("nestedRepeaterId");

Resources