Can you set a parent tag (Label) with a child data within a nested repeater?
<asp:Repeater ID="rpDB" runat="server" OnItemDataBound="rpDB_ItemDataBound" EnableViewState="true">
<ItemTemplate>
<label class="glyphicon glyphicon-leaf" /><label id="rbt" runat="server"></label>
<asp:Repeater ID="rpDB_item" runat="server" OnItemDataBound="rpDB_item_ItemDataBound">
<ItemTemplate>
<div class="row anj">
<div class="col-md-1 col-xs-1 glyphicon glyphicon-off" id="lblboot" runat="server" title=<%# DataBinder.Eval(Container.DataItem,"boot") %>></div>
</div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I would like to set label rbt in the parent repeater rpDB with the value of the lblboot in the child repeater rpDB_Item. Is this possible, and if so how. I see plenty pulling parent into child but not reversed.
Found the answer. I thought I had used this but must have been doing it wrong.
(HtmlGenericControl) mycontrol = (HtmlGenericControl)(e.Item.Parent.Parent.FindControl("myControl"));
This allowed me to set the attributes to a parent control.
Related
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="countryDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="accordion">
<span><h3><%# Eval("key") %>
<div class="panelCountry">
<input type="checkbox" runat="server" class="chbSelectAll"/><label>SelectAll</label>
<asp:CheckBoxList
ID="chbListCountries"
CssClass="chbList"
RepeatColumns="4"
RepeatDirection="Horizontal"
DataTextField ="Name"
DataValueField = "CountryCode"
runat="server">
<asp:ListItem></asp:ListItem>
</asp:CheckBoxList>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
I have a dialog containing a repeater of all the countries in the world grouped by continent. Each continent is separated by a jquery accordion. Inside each accordion there is a checkboxlist of countries, with a checkbox at the top that functions as a select all. I need to have the label associated with the select all, be responsive. By this i mean if i click the label i need to be able to trigger the checkbox to select all.
I considered
<label for="IDHERE">Select all</label>
I cannot however figure out how to correctly target the checkbox so that when i click the specific label it only selects the check boxes in this specific checkboxlist.
Is there anyway to create an ID for each "select all" checkbox that is unique from the other seven so that when the label is clicked on it correclty selects the checkboxes in its respective accordion.
for reference, when the checbox is clicked this javascript runs:
$(document).on('change', '.chbSelectAll', function (e) {
var selectAllValue = $(this).prop('checked')
var panel = $(this).closest('.panelCountry');
var checkboxes = panel.find('input[type=checkbox]:not(.chbSelectAll)');
checkboxes.prop('checked',selectAllValue);
});
Visual view of the problem
If you don't need the runat=server tag you can create your own label for= by using the ItemIndex.
<input type="checkbox" class="chbSelectAll" id="CheckBox_<%# Container.ItemIndex %>" />
<label for="CheckBox_<%# Container.ItemIndex %>">SelectAll</label>
But why not use an aspnet CheckBox? Then you won't have that problem anyway.
<asp:CheckBox ID="CheckBox1" runat="server" Text="SelectAll" CssClass="chbSelectAll" />
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
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!
So I'm really not sure what I'm doing wrong here, but my code is clearly not working. I have my list view doing everything that I need, except that when I use the ListView.FindControl() method, and then set a property on that control, it give me an Object reference not set to an instance of an object. error. Here is my code:
ASPX
<p class="rates-title"><span>
<asp:Literal ID="currentDate" runat="server"></asp:Literal></span><br>
Todays Rates</p>
<span class="rates-arrow sprite"></span>
<asp:ListView ID="RatesList" ItemPlaceholderID="RSSPlaceholder" runat="server">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="RSSPlaceholder" runat="server"></asp:PlaceHolder>
<li>
<asp:HyperLink ID="AllRatesLink" CssClass="all-rates" runat="server">
View All Rates<span></span>
</asp:HyperLink>
</li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<div class="rate-text">
<p><%# Eval("Name") %></p>
<div class="rate">
<p><%# Eval("InitialRate") %>%</p>
</div>
</div>
<%# Eval("Apr") %>
<div class="todays-rates-rollover">
<p><%# Eval("ContentTruncated") %></p>
</div>
</li>
</ItemTemplate>
</asp:ListView>
Code Behind
currentDate.Text = DateTime.Now.ToString("MM.dd.yy");
HyperLink allRatesLink = (HyperLink)RatesList.FindControl("AllRatesLink");
allRatesLink.Text = "hello";
So the weird thing is that currentDate works just fine, the data that I use (elsewhere in my codebehind) works just fine for iterating through the list, but as soon as I set any property on the allRatesLink control, it gives the object reference not set error. Any ideas/help on this one?
allRatesLink is null because it is not being found which leads to the Object reference error when you try to set a property of null.
You need the FindControl to be called after the LayoutTemplate is created such as in the OnLayoutCreated handler or after DataBind()