Asp.Net Repeater not showing its element - c#

I am using ASP.NET repeater and I want to display simply heading
like this:
<body>
<form id="form1" runat="server">
<h3>Example1</h3>
<asp:Repeater ID="rp1" runat="server" OnItemCommand="rp1_ItemCommand">
<ItemTemplate>
<h4>Repeater Data</h4>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
But when I run my code its shows only heading and avoid inner heading. I know Its quiet simple to solve this but believe me I googling after an hours but not found any solution. Kindly help me.

ItemTemplate is shown only when Repeater has data.
What you want to use is HeaderTemplate instead, which is also visible when no data is present.
In your previous ItemTemplate include only the data you want to display per item.
<asp:Repeater ID="rp1" runat="server">
<HeaderTemplate>
<h4>Repeater Data</h4>
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>

Related

ASP.Net Repeater Responsive Controls Binding

I have ASP.Net controls for search attributes with check box options and ASP.NET repeater. For Responsive Design, I have same bindings in two different places to match the design.
Here, CheckBoxList ID=Options1 & ID=Options2 both having same binding but the code will repeat. I have 400 lines to check the conditions for single binding. i have to duplicate all the codes again for the another view. Is there any way to optimize the code to single bind with both views handling. ( Avoid duplicate binding and checking)?
// Desktop View
<div class="hidden-sm hidden-xs narrowSearch">
//Content
<asp:Repeater ID="rptAttributes1" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options1" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
// Mobile view
<div class="sec_left hidden-lg hidden-md">
<div class="moremenu narrowSearch">
//Content
<asp:Repeater ID="rptAttribute2" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options2" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
Code Behind:
CheckBoxList chklOptions1 = item.FindControl("Options1") as CheckBoxList;
CheckBoxList chklOptions2 = item.FindControl("Options2") as CheckBoxList;
You can place your repeater (including the declarative code as well as what you have in the code-behind of your page) into a custom user control. Then you put the user control in each section (desktop and mobile) of your page. This is the simplest way to avoid repeated code. Having said this, since you've only provided part of the code that's in the code-behind class of your page, I can't speak to what changes might be required when you move the imperative code you have in your page's code-behind class to the user control.
Good luck!

Adding 'Show More' functionality to a ASP Repeater

To put it as simple as I can. I have a website and the homepage is populated with an ASP Repeater control. This shows details of people you follows activities on the site within the last 30 days.
My problem being I dont want my homepage to be too long on loading if you follow lots of people and they have been participating in lots of recent activities, meaning the repeater will be extremely long and hence my page being stretched.
What I want is for the repeater to show say 15 items/rows then below this is a 'Show more' link which will show either show the rest or each time you click shows another 15 (either/or I will be happy with!)
Here is my repeater code...
<asp:Repeater ID="RptNewsFeed" runat="server"
onitemdatabound="RptNewsFeed_ItemDataBound">
<ItemTemplate>
<div class="NewsFeedItem">
<div class="ProfileImageContainer">
<asp:HyperLink ID="HlkProfileImageLink" runat="server" EnableViewState="false">
<asp:Image ID="ImgProfileImage" runat="server" EnableViewState="false" />
</asp:HyperLink>
</div>
<div class="ProfileLinkContainer">
<asp:HyperLink ID="HlkProfileLink" runat="server" EnableViewState="false" CssClass="ActionDoneBy"></asp:HyperLink>
<asp:Label ID="LblAction" runat="server" EnableViewState="false" CssClass="Action"></asp:Label>
<asp:HyperLink ID="HlkActionObjectLink" runat="server" EnableViewState="false" CssClass="ActionDone"></asp:HyperLink>
</div>
<div class="TimeContainer">
<asp:Label ID="LblActionTime" runat="server" EnableViewState="false"></asp:Label>
</div>
</div>
</ItemTemplate>
Thanks in advance for any help provided!

Accessing child control in ASP.NET

I am trying to find out if it's possible to have a control inside another control in ASP, like this:
<asp:FormView ID="FormView1" runat="server" Width="630px" Height="496px">
<ItemTemplate>
<asp:Literal ID="ID" runat="server">Idnumber: </asp:Literal><%#Eval("ID") %>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:HyperLink ID="ID" runat="server"> <%# Eval("FILE") %> </asp:HyperLink>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:FormView>
Can I access DataList1 control? I have been trying, but I can't figure it out, I should be able to access nested controls, but I cant get it to do it.
Controls inside Template tags cannot be directly accessed in code behind. Instead you should use FindControl method:
var dataList1 = (DataList)FormView1.FindControl("DataList1");
Note that this might not work in early stages of page life cycle (not until Page_Load I believe).

stopping page refresh at each click

I have a page name ask.aspx
I have 4 list views in that page which are binding at the time of page load event.
now when ever i click on the items of list view i get the whole page refreshed.
I have used the update panel but for some reasons it is not working
The code for the same is given below.
aspx page
enter code here
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<legend><asp:Label ID="Label1" runat="server"></asp:Label></legend>
<ul class="tags">
<asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand" >
<ItemTemplate>
<li><asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Eval("CategoryId") %>'><%# Eval("Name") %></asp:LinkButton></li>
</ItemTemplate>
</asp:ListView>
</ul>
</fieldset>
</ContentTemplate>
<Triggers></Triggers>
</asp:UpdatePanel>
ending the content template and update panel.
similarly for the other three listviews.
at cs page
some calculations are taking place.
Now i dont want the page to refresh every time.
what should be done.
please suggest.
Remove childrenastrigger property and set updatemode=always

Nested repeater yields "malformed server tag"?

I'm trying to do a nested repeater as described here but it's erroring out.
My repeater is as follows:
<asp:Repeater ID="HouseholdRepeater" runat="server">
<ItemTemplate>
<div><b><%# DataBinder.Eval(Container.DataItem,"Name") %></b></div>
<div>
<asp:Repeater ID="ApplicationRepeater" runat="server" DataSource="<%#((DataRowView)Container.DataItem).Row.GetChildrows("Applications") %>"> <!-- error here -->
<ItemTemplate>
<div>
<%# DataBinder.Eval(Container.DataItem,"Description") %>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
The error message I get is "The server tag is not well formed."
It looks exactly like the example to me. I'm not seeing what is wrong with it. Any ideas how to make this work?
Also, I databind it in the code using an anonymous object from a Linq query.
You are using double quotes to specify your DataSource property, but your DataSource contains double quotes itself. Try enclosing the DataSource in single quotes:
DataSource='<%#((DataRowView)Container.DataItem).Row.GetChildrows("Applications") %>'
Change
Row.GetChildrows("Applications")
to
Row.GetChildrows(""Applications"")
Also change
<a href="<%# DataBinder.Eval(Container.DataItem,"Link")
to
<a href="<%# DataBinder.Eval(Container.DataItem,""Link"")
The DataSource section looks incorrect... the DataSource should be something like this
'<%#DataBinder.Eval(Container, "DataItem.ChildTable") %>'
Change
DataSource="<%#((DataRowView)Container.DataItem).Row.GetChildrows("Applications") %>"
to
DataSource='<%#((DataRowView)Container.DataItem).Row.GetChildrows("Applications") %>'

Categories