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!
Related
I have a page where i am displaying videos with their title above them. I am currently using a Repeater which renders out a div with the video title and a div with the video iframe html. Everything is coming from a database table where i store the video title and video iframe html.
<asp:Repeater ID="RptVideos" runat="server" OnItemDataBound="RptVideos_ItemDataBound">
<ItemTemplate>
<div class="TrainingVideosVideoContainer">
<div class="Title">
<asp:Label ID="LblVideoTitle" runat="server" />
</div>
<div class="Video">
<asp:Literal ID="LitYoutubeVideo" runat="server" />
</div>
</div>
</ItemTemplate>
My problem being this renders them out one below the other in a list format. I wish to render my videos out in almost a grid format, so two side by side etc. How is best to go about this? HTML Rendering? Repeater? GridVIew?
I found that #Crowcoder was correct in this suggestion to use a DataList control. With this i was able to specify the layout being in a table format with the RepeatColumns being set to 2
<asp:DataList ID="DltVideos" runat="server" OnItemDataBound="DltVideos_ItemDataBound"
RepeatColumns="2" RepeatLayout="Table" Width="100%">
<ItemTemplate>
<div class="TrainingVideosVideoContainer">
<div class="Title">
<asp:Label ID="LblVideoTitle" runat="server" />
</div>
<div class="Video">
<asp:Literal ID="LitYoutubeVideo" runat="server" />
</div>
</div>
</ItemTemplate>
</asp:DataList>
I want to make a slideshow with divs in repeater items. there are two divs with different classes and cannot show page properly with it. Could you help me please to fix the problem?
Design cs is like this:
<div id="wowslider-container1">
<asp:Repeater ID="rpSlideshow" runat="server" >
<ItemTemplate>
<div class="ws_images">
<ul>
<li id="<%# Container.ItemIndex + 1 %>">
<img src="Yazar/data1/images/<%#Eval("Resmi") %>" alt="<%#Eval("AdSoyad") %>" title="<%#Eval("AdSoyad") %>"
id="<%#Eval("SlideID") %>" />
</li>
</ul>
</div>
<div class="ws_bullets">
<div>
<a href="#" title="<%#Eval("SlideID") %>"><span>
<img src="Yazar/data1/tooltips/<%#Eval("Resmi") %>" alt="<%#Eval("AdSoyad") %>" /><%#Eval("SlideID") %></span></a>
</div>
</div>
<div class="ws_shadow">
</div>
</ItemTemplate>
</asp:Repeater>
</div>
PS: I tried to combine divisions like below but it did not work proper.
((HtmlGenericControl)(e.Item.FindControl("myDiv"))).
Attributes["class"] = "id" + indexi++;
You need runat="server" attribute, if you want to access it at code behind.
<div id="myDiv" runat="server">...</div>
TIP: For that purpose, we normally use Panel server control which basically renders html div tag. The advantage is you can use strongly type CssClass property.
<asp:Panel id="MyPanel" runat="server">...</asp:Panel>
Usage: ((Panel)e.Item.FindControl("MyPanel")).CssClass = "id" + indexi++;
Please note that I rename the control id to MyPanel.
I'm Completely new at ASP.net
My database model is here
As it shows in the above picture I have a table it's name is Tours and it has a column named TourId.
I have an object Data Source in my Webform and a repeater the code is below:
<asp:ObjectDataSource ID="ODSTTitle" runat="server" SelectMethod="GetById" TypeName="ATourRep">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="Id" QueryStringField="CID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
</asp:ObjectDataSource>
<div class="container" id="TourDetail">
<asp:Repeater ID="RptTourDetail" runat="server" DataSourceID="ODSTTitle" ItemType="Tour" EnableViewState="false">
<ItemTemplate>
<li>
<div class="row">
<div class="col-md-3">
<%-- NestedRepeater --%>
<asp:Repeater ItemType="TourDate" ID="RptTourNested" runat="server" EnableViewState="false">
<ItemTemplate>
<h2>
<%-- What Should I write Here to have a list of each TourDate --%>
</h2>
</ItemTemplate>
</asp:Repeater>
</div>
<%-- This div works correctly --%>
<div class="col-md-6">
<h4>
<%# Item.TName %>
</h4>
<p>
<%# Item.TDes %>
</p>
</div>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
As The code Shows there is a nested repeater that it has h2 tag. i need to show a list of each TourDates in h2 tag.
I dont know how to bind ObjectDataSource1 to use TourId of each Tour and then Get StartDates of that tour. and also what I should write in h2 tag of nested repeater to show satrtdates of that tour
((There is more than one startdate for each tour))
and i have this method in my repository also
public IQueryable<TourDate> GetById(int Id)
{
return model.TourDates.Where(e => e.TourId == Id);
}
I used Default value of ObjectDataSource and then bind it by eval but it dosent work.
do you have any suggestion or am I wrong in structre or do you think is there any better than nested repeaters there?
In the parent repeater you need to implement a Repeater.ItemDataBound event as specified here https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound%28v=vs.110%29.aspx, then inside that event you need to get for each item (row) the information you need and send the datasource to the inner Repeater, and then call for its databind function.
This Article was Helpful. that was exactly what i needed
https://msdn.microsoft.com/en-us/library/bb510145.aspx#aspnettut32vbnesteddata_topic4
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!
Master Page
<div id="header" style="height: 150px; width: 750px;">
<asp:Label ID="Label3" runat="server" Text="LoggedInUser:"></asp:Label>
<asp:Label ID="lblLoggedInUser" runat="server" Text=""></asp:Label>
</div>
<div id="leftMenu" class="leftmenu">
<br />
<asp:DropDownList ID="ddlFamilyMembers" runat="server"
style="height: 25px; width: 125px" DataTextField="FullName"
DataValueField="MembershipGen"
onselectedindexchanged="ddlFamilyMembers_SelectedIndexChanged"
AutoPostBack="True" >
</asp:DropDownList>
<br /><br />
<asp:Image ID="imageMember" class="space" runat="server" Height="150px" Width="125px" />
<br /><br /><br />
<asp:Label ID="Label1" runat="server" class="space" Text="MembershipID:"></asp:Label>
<asp:Label ID="lblMembershipID" runat="server" Text=""></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" class="space" Text="Name:"></asp:Label>
<asp:Label ID="lblMemberName" runat="server" Text=""></asp:Label>
<br /><br /><br />
<asp:LinkButton ID="lbInformation" class="space" runat="server" onclick="lbInformation_Click">Member Information</asp:LinkButton><br />
<asp:LinkButton ID="lbAddress" class="space" runat="server"
onclick="lbAddress_Click">Member Address</asp:LinkButton>
</div>
<div id="divRight" class="divright">
<asp:ContentPlaceHolder ID="CPHMain" runat="server">
</asp:ContentPlaceHolder>
</div>
I posted my masterpage code so you can better understand my issue. Since these controls are in my master page I needed to create public properties for all the controls in my masterpage to databind my content pages. Maybe I am approaching this wrong but this is how I was planning on achieving this.
ContentPage
Default.aspx page_load event would call my method to retrieve the data and set the dropdownlist, image, 2 label fields. On selected index change of dropdownlist it would grab the new values from another method and populate those controls accordingly.
To access these controls in my master page i read you can do it a couple of different ways one is to <%#MasterType VirtualPath="~/Member.Master" %> and then create a strongly typed connection. Or you could create a loosely type connection.
My problem with these ways as I am going to have over 15 content pages and I would hate to have to rebind the DropDownList every time one of the content pages is called. Also I would have to reinstantiate the controls in every method of my content pages which I assume I am doing this wrong.
Can someone give me some advice on what is the proper way to achieve this without so much repetitive code?
You've got the right idea, trying to decouple the parent from the child, but I think the way you want to do it may be clunky.
Assuming you can do this, what I'd do is as follows:
Make a public method BindFamilyMembers(string parameter) on the Master page's code-behind.
From the child page, pass necessary unique parameters to this BindFamilyMembers Master method.
This way you don't need to expose the controls themselves, you expose a method that modifies those controls based on parameters.
edit
You want to notify the child page that the DDL SelectedIndex has changed. This is a great case for custom events.
You can configure an event on the Master page that the child page listens for. When DDL.SelectedIndex changes, you can fire the custom event (and pass the selected information) via the custom event, and any child page that is listening can handle that event.
There are a lot of examples of custom events online. Here's one you can start with: http://www.marten-online.com/csharp/simple-custom-event-handling.html