ListView.DataItem is showing null - asp.net

Listview binding code
<asp:Content ID="Content3" ContentPlaceHolderID="leftColumnPlaceHolder" runat="server">
<asp:ListView ID="lvQuestions" runat="server" OnItemDataBound='lvQuestions_ItemDataBound'>
<LayoutTemplate>
<div id="itemPlaceholder" runat="server">
</div>
<asp:Button ID="btnSubmitAnswers" runat="server" Text="Submit Answers" OnClick="btnSubmitAnswers_Click" />
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Container.DataItemIndex + 1 %>:<%# Eval("Body") %>
</div>
<asp:RadioButtonList ID="rdlAnswers" runat="server" DataSource='<%#Eval("ExamAnswer") %>' DataTextField='Body' DataValueField="AnswerId">
</asp:RadioButtonList>
</ItemTemplate>
</asp:ListView>
</asp:Content>
While fetching the listview items on submit button click..like below,
we are getting qsnItem.DataItem as NULL.
foreach (ListViewDataItem qsnItem in lvQuestions.Items)
{
}
Please suggest what is going wrong here.

The DataItem of all databound web-controls in ASP.NET are null on postbacks when you don't DataBind the control again, which is unnecessary when ViewState is enabled(default).
So you can use the controls in your templates to get the values:
foreach (ListViewDataItem qsnItem in lvQuestions.Items)
{
RadioButtonList rdlAnswers = (RadioButtonList)qsnItem.FindControl("rdlAnswers");
}
If you need to old values you need to load them from database or use the ListViewUpdatedEventArgs.OldValues Property.

Related

Binding ListView Field to Value in Code-Behind

I'm displaying fields from a DB call in a ListView. I had to re-code my database query because of an issue with repeating groups. I also didn't want to have to deal with nested ListViews. So now I am trying to set the header to the value of radio button list selection. I was trying to write a custom method to return that value but could not remember the syntax.
How can I set the header in the LayoutTemplate from the code-behind so that it doesn't repeat?
<LayoutTemplate>
<div class="resultsGrid">
<strong><%# GetHeader() %></strong>
<asp:PlaceHolder runat="server" ID="resultsPlaceHolder">
</asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
// rest of data
</ItemTemplate>
I was able to solve this as follows:
<asp:ListView ID="resultsList" ItemPlaceholderID="resultsPlaceHolder"
OnItemCreated="ResultsList_ItemCreated" runat="server">
<LayoutTemplate>
<div class="resultsGrid">
<strong><asp:Label ID="headerLabel" runat="server"></asp:Label></strong>
<asp:PlaceHolder runat="server" ID="resultsPlaceHolder">
</asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
// rest of data
</ItemTemplate>
</asp:ListView>
And in the code-behind:
protected void ResultsList_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item is ListViewItem)
{
Label tempLabel = resultsList.FindControl("headerLabel") as Label;
tempLabel.Text = ViewState["ParkName"].ToString();
}
}

UserControl not loading in ListView

I've create my own UserControl called 'ReevooBadge' and have placed it in my listview by registering my UserControl. However, my UserControl isn't visible in the ListView. Any sugestions for this problem?
This is my User Control
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ReevooBadge.ascx.cs" Inherits="KiaEurope2010.Website.controls.Addons.Widgets.ReevooBadge" %>
<a class="reevoomark <%= this.ClassName %>" href="//mark.reevoo.com/partner/<%= this.CountryCode %>/series:<%= this.SelectionIdentifier %>"><%= this.ReviewsTitle %></a>
I need to implement this in my ListView by the following code implementation
<asp:ListView ID="lvItemCarousel" ItemPlaceholderID="carrouselitems"OnItemDataBound="lvItemCarousel_ItemDataBound"
runat="server">
<layouttemplate>
<asp:PlaceHolder ID="carrouselitems" runat="server" />
</layouttemplate>
<itemtemplate>
<li>
<asp:HyperLink runat="server" ID="hlItem">
<asp:Image runat="server" ID="imItem" />
<div class="modelTitle">
<asp:PlaceHolder runat="server" ID="phReevoo" Visible="false">
<rhm:ReevooBadge runat="server" ID="ReevooBadge" />
</asp:PlaceHolder>
</div>
</asp:HyperLink>
</li>
</itemtemplate>
</asp:ListView>
Above I register my custom User Control
<%# Register Src="~/controls/Addons/Widgets/ReevooBadge.ascx" TagPrefix="rhm" TagName="ReevooBadge" %>
And in the Item Data Binding
protected void lvItemCarousel_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//...
phReevoo.Visible = true;
ReevooBadge.ModelItem = this.Item;
ReevooBadge.CountryCode = KiaEurope.SitecoreLayer.Reevoo.Settings.GetReevooKey(LanguageCountryCultureHelper.CurrentCountryCode, Sitecore.Context.Language);
}
However, the user control isn't visible in the ListView.
I'm not sure how you're getting your code to compile because I couldn't reference phReevoo (the placeholder control) directly in the ItemDataBound event.
I needed to use the following:
e.Item.FindControl("phReevoo").Visible = true;
The same would apply if you tried to reference ReevooBadge.
To make the testing easier I created a web user control and used this as a substitute for your custom user control. After binding to a simple List<string>, the control showed without issue.
I would suggest checking if your user control is actually rendering outside of the ListView.

CallendarExtender is not a known element

I have two pages with the same code for ´UpdatePanel, ScriptManager and CalendarExtender, in one of them, theCalendarExtender` is working fine, but in the other, it's giving me this error:
CalendarExtender is not a known Element
Here's my code on aspx
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolKit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<div id="conteudo" style="text-align: left">
<fieldset id="fieldset" style="width:730px; margin-left: -200px">
<legend style="text-align:center;"><b>Detalhes do Chamado</b></legend>
<div id="DetalhesChamado">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
//Here is a gridview
<asp:CalendarExtender runat="server"></asp:CalendarExtender>
But still giving the error...
--Update 2--
Now i'm getting this error:
The TargetControlID of 'CalendarExtender1' is not valid. A control with ID 'TxtPrevisao' could not be found.
My New code:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TxtPrevisao" runat="server" Width="115px"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format=" dd ,MMMM ,yyyy" TargetControlID="TxtPrevisao" PopupButtonID="TxtPrevisao" CssClass="MyCalendar">
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
Change the Tag-Prefix of the Ajax-Kit an see if it's working
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
And also:
<act:CalendarExtender runat="server"></act:CalendarExtender>
asp is used for Asp.Net components like the UpdatePanel
------------ Update 2 -------------------
To your new error, you can't assign the PopupButtonID to the textbox, you need a ImageButton
<asp:ImageButton ID="btnCalenderPopup" runat="server" ImageUrl="App_Themes/Standard/Img/calendar.png" CausesValidation="False" />
Still, I insist, you will need to assign the Tag-Prefix for the Took-Kit correctly and you will need to reference the assembly correctly as SanjeevRai said
You have to add reference of 'Ajaxtoolkit' dll into your project. It seems like it is not getting the proper reference of 'AjaxToolkit' DLL.
You can check references of your project by right clicking on website and select 'Property Pages' option. then it will show you all the references. from here you can check whether 'Ajaxtoolkit' reference exit in your project or not.
If Ajaxtoolkit reference does not exist in your project then you will have to add this reference first!
------------ Update 2 -------------------
After looking at your code I got the issue.
Actually you are using calenderextender and its TargetControlID textbox inside a gridview, so it is not getting the id of textbox.It will never identify the child control id directly on aspx page.
you need to attach 'TargetControlID' and 'PopupButtonID' at run time inside RowDataBound event of gridview.
Here is a sample of code that you can use in you aspx.cs file:
void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = (TextBox)e.Row.FindControl("TxtPrevisao");
CalendarExtender Calendar1 = (CalendarExtender)e.Row.FindControl("CalendarExtender1");
Calendar1.TargetControlID=txt.ID;
Calendar1.PopupButtonID=txt.ID;
}
}
And in .aspx page attach the rowdatabound event to grid and remove 'TargetControlID' and 'PopupButtonID' attributes from calenderextender i.e.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TxtPrevisao" runat="server" Width="115px"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format=" dd ,MMMM ,yyyy" CssClass="MyCalendar">
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>

ASP.NET MultiView with multiple repeaters

I have a problem and I need your opinion. I have a control with a MultiView and each view will be a different render for the control's output, it will be about 10 different views. Inside each view I will have a repeater that will repeat a few rows, this repeater will be the same for all views and since the MultiView only allows 1 view to be show at any one time, I though to have the repeater with the same ID, so I don't have to make 10 bindings and create 10 OnItemDataBound events with the exact same code in it, but ASP.NET don't let me have the repeater with the same ID (it should be smarter than that for this case). I am using C# and v4 of the framework.
Here's the MultiView code:
<asp:MultiView id="MultiView" runat="server">
<asp:View id="h400" runat="server">
<div class="latest_Wide">
<h3>Wide</h3>
<asp:Repeater id="rptLatest" runat="server" OnItemDataBound="rptLatest_OnItemDataBound">
<ItemTemplate>
<p>• <asp:Literal id="litPostTitle1" runat="server" /></p>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:View>
<asp:View id="h200" runat="server">
<div class="latest_Narrow">
<h3>Narrow</h3>
<asp:Repeater id="rptLatest" runat="server" OnItemDataBound="rptLatest_OnItemDataBound">
<ItemTemplate>
<p>• <asp:Literal id="litPostTitle2" runat="server" /></p>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:View>
...
</asp:MultiView>
How can I solve this problem. Remember I don't want to make 10 repeater bindings and have 10 OnItemDataBound events with the exact same code, for the case you suggest to give the repeaters different IDs.
This isn't pretty... but:
Assume your ASCX:
<asp:MultiView id="mvPostDisplay" runat="server">
<asp:View id="h400" runat="server">
<div class="latest_Wide">
<h3>Wide</h3>
<asp:Repeater runat="server" OnItemDataBound="rptLatest_OnItemDataBound">
<ItemTemplate>
<p>• <asp:Literal id="litPostTitle1" runat="server" /></p>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:View>
<asp:View id="h200" runat="server">
<div class="latest_Narrow">
<h3>Narrow</h3>
<asp:Repeater runat="server" OnItemDataBound="rptLatest_OnItemDataBound">
<ItemTemplate>
<p>• <asp:Literal id="litPostTitle2" runat="server" /></p>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:View>
...
</asp:MultiView>
In your codebehind:
private void DataBindRepeater()
{
View activeView = this.mvPostDisplay.GetActiveView();
Repeater myRepeater = this.FindRepeater(activeView);
if (myRepeater != null)
{
myRepeater.DataSource = this.GetDataSourceFormSomewhere();
myRepeater.DataBind();
}
}
private Repeater FindRepeater(Control container)
{
if (container is Repeater)
{
return container as Repeater;
}
else
{
foreach (Control c in container.Controls)
{
Repeater retVal = this.FindRepeater(c);
if (retVal != null)
{
return retVal;
}
}
return null;
}
}
And simply call DataBindRepeater after you've determined the appropriate view. Please note rptLatest will not be set, therefor you will need to get a reference to your repeater in your handler using either the sender, FindRepeater, or possibly something less hackish.
Have you considered making the repeater a user control? This way you have all the repeater binding code once in your user control. Each view will have it's own instance of the usercontrol.
Is this a direction you are willing to take?

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");
}

Resources