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()
Related
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 want to add glyphicons to my custom built menu that I built using an <asp:Repeater> and a web.sitemap file as a data source.
Here is what I've tried:
<ul class="nav navbar-nav">
<asp:Repeater runat="server" ID="rptMenu" DataSourceID="smdsMain">
<ItemTemplate>
<li>
<a runat="server" href='<%# Eval("url") %>'>
<span class='glyphicon glyphicon-<%# Eval("glyphicon") %>'></span>
<%# Eval("title") %>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<asp:SiteMapDataSource ID="smdsMain" runat="server" ShowStartingNode="false" SiteMapProvider="XmlSiteMapProvider" />
And here's an example of a node in my web.sitemap:
<siteMapNode url="~/Secure/Home/Default" title="Home" description="Home" glyphicon="home" />
Here is the error I get.
System.Web.HttpException: DataBinding: 'System.Web.SiteMapNode' does not contain a property with the name 'glyphicon'.
I everything else works fine without the glyphicon implementation.
I know it is possible to have a custom attribute in your sitemap nodes because someone implemented it in this Stack Overflow post.
So I must be doing something wrong in my customer implementation. What is the correct way to implement this?
After some research I found that you can access custom attributes in this way:
<%# Eval("[attributeName]") %>
Using that knowledge I have implemented into my code like this to get it working:
<ul class="nav navbar-nav">
<asp:Repeater runat="server" ID="rptMenu" DataSourceID="smdsMain">
<ItemTemplate>
<li>
<a runat="server" href='<%# Eval("url") %>'>
<span class='glyphicon glyphicon-<%# Eval("[glyphicon]") %>'></span>
<%# Eval("title") %>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<asp:SiteMapDataSource ID="smdsMain" runat="server" ShowStartingNode="false" SiteMapProvider="XmlSiteMapProvider" />
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.
I hope i make sense this, i have a repeater and on click of a button, the corresponding div should open up, I can do this when it is hardcoded however since its a repeater there will be different IDs. Here is my repeater;
<asp:Repeater ID="rptProfileStatus" OnItemDataBound="rptProfileStatus_OnItemDataBound"
runat="server">
<ItemTemplate>
<li class="bar1">
<a name="jh"></a>
<div align="left" class="post_box">
<div class="itembottom">
<span class="date">
<asp:Label ID="lblDate" Font-Underline="false"
ForeColor="white" runat="server"></asp:Label>
</span>
<span class='feed_link'>
</span>
<span class="commentsno">
2 comments
</span>
</div>
<span class="delete_button">
</span>
</div>
<div id="fullbox" class="fullboxahrefReply"></div>
<div id="commentload"></div>
</li>
</ItemTemplate>
</asp:Repeater>
Here is the jquery which i am trying to get to call it, it works now however when i start changing the ID's i will have a problem, how do i solve this;
//Comment Box Slide
$("[id$=ahrefReply]").live("click", function ()
{
var ID = $(this).attr("id");
$(".fullbox" + "ahrefReply").show();
$("#c" + "ahrefReply").slideToggle(300);
});
In the anchor's click event, you can do something like,
click="YourFunction(<%# ((YourClass)Container.DataItem).Id %>)"
Then handle the id in your function. You will have to modify this to work with your situation as well.
Try just walking up the DOM. Something like this:
$(this).parent().parent().find('fullboxahrefReply').show()
In an ASP:ListView I want to pass an entire object to a child control within the ItemTemplate, not just a single property of the current object.
Here is the code I want to work:
<asp:ListView ID="answers" runat="server">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div>
<uc2:DocumentHeader runat="server" Document="Eval(%# Eval("this") %> />
<p><%# Eval("Body") %></p>
</div>
</ItemTemplate>
</asp:ListView>
The Document property of the DocumentHeader expects the entire Document object, whereas "Body" is a property of Document.
Obviously, I could just create a new property within Document or use a LINQ query to generate a new class with the property I want, I just want to know if there is an easier, more direct way to get what I want.
You can bind the context object using <%# Container.DataItem %>. You probably will need to cast it to whatever "Document" expects.
<asp:ListView ID="answers" runat="server">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div>
<uc2:DocumentHeader runat="server" Document="<%# Container.DataItem %>" />
<p><%# Eval("Body") %></p>
</div>
</ItemTemplate>
</asp:ListView>