How to use GetIndexedPropertyValue? - asp.net

I have the following ListView:
<asp:ListView ID="lv_Announcements" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<%# Eval("Title") %><br />
<%# DataBinder.GetIndexedPropertyValue(Fields, "[\"Body\"]")%><br /><br />
</ItemTemplate>
</asp:ListView>
In my code behind I am doing this:
lv_Announcements.DataSource = cur.Web.Lists["Announcements"].Items;
lv_Announcements.DataBind();
Where Items is a SPListItemCollection. When ever I run my code I get the following error:
The name 'Fields' does not exist in
the current context
Should I be doing something differently here?

I ended up doing it like this:
<%# DataBinder.GetIndexedPropertyValue(Container.DataItem, "[\"Body\"]")%>
DataItem is the SPListItem that the row is being bound to so you can think of it like this:
SPListItem myItem = //whatever;
myItem["Body"];
Before it would have been like this:
SPListItem myItem = //whatever;
myItem.Fields["Body"];
Which does not return the information I wanted.

Related

ASP.NET Linkbutton not getting dynamic value for commandargument value

<%
foreach (training t in traininglist)
{
%>
<tr>
<td><%=t.TrainingId%></td>
<td><%=t.TrainingName%></td>
<td>
<asp:LinkButton runat="server" ID="EditBtn"
Text="Edit" OnCommand="editbtn_OnCommand"
CommandArgument='<%# t.TrainingId %>' CommandName="edit" />
</td>
</tr>
<% } %>
where,
training is the class and traininglist is List<training> defined in Page_Load() function in codebehind.
I am trying to call the
public void editbtn_OnCommand(object sender, CommandEventArgs e)
{
String myeid = e.CommandArgument.ToString();
....
}
Here, myeid is not getting value but always shows <%# t.TrainingId %>
i have already tried all other options like <%: t.TrainingId %> or <%=t.TrainingId %>
The output of Tag "<%= %>" is more likely similar to use Response.Write in your code. so these tags are used to display the value to the response object.
That's why,as per my understanding, You can't used these tags to set commandargument property of controls unless you are using DataBinding. If you are using DataBinding then these tags "<%= %>" are used to set the property of controls.
Because you are here looping through each item in list on html table, my suggestion is to use GridView or Repeater and then Bind through your List Object. if you are using this way, you can get rid of unwanted formatting issues of html tables also.
Refer http://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
If you want to use repeater then you can use these specific tags, and this should be your code(not actual code, just sample one)
<asp:Repeater id="myRepeater" runat="server" >
<ItemTemplate>
<div>
<asp:LinkButton runat="server" id="EditBtn" CommandName="edit"
CommandArgument="<%#Container.DataItem.TrainingId %>" Text="Edit"
OnCommand="editbtn_OnCommand" />
</div>
</ItemTemplate>
</asp:Repeater>

ListView.DataItem is showing null

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.

How to get parent datasource Eval() inside a nested Repeater? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Accessing parent data in nested repeater, in the HeaderTemplate
I have a nested repeater being databound... let's say the top level repeater is an OrderRow and the inner repeaters are bound to LineItem rows from my DB. ASPX is something like this:
<asp:Repeater ID="rptOrder" runat="server">
<ItemTemplate>
<%# Eval("OrderID") %>:<br/>
<asp:Repeater ID="rptLineItems" runat="server">
<ItemTemplate>
<%# Eval("SomeColumn1"); %>
<%# Eval("SomeColumn2"); %>
<%# Eval("SomeColumn3"); %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddLine" runat="server" CommandArgument=<%# ???? %> />
</FooterTemplate>
</asp:Repeater>
</ItemTemplate
</asp:Repeater>
Now the button on the inner footer will be used to add a new line item... but the command argument needs to be the OrderID from the outer repeater, so we know which order to add to. Obviously a regular Eval() call won't work here, because it will have the inner repeater's DataRowView as a source [actually, it won't, since it's in the footer]. How do I get this value? Am I going to have to set this dynamically in the ItemDataBound event of the outside repeater?
Since the call are serial you can use the code behind to save the last order id and use it later.
Here is the idea.
<asp:Repeater ID="rptOrder" runat="server">
<ItemTemplate>
<%#GetOrderID(Container.DataItem)%><br />
<asp:Repeater ID="rptLineItems" runat="server">
<ItemTemplate>
<%# Eval("SomeColumn1"); %>
<%# Eval("SomeColumn2"); %>
<%# Eval("SomeColumn3"); %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddLine" runat="server" CommandArgument=<%=cLastOrderID%> />
</FooterTemplate>
</asp:Repeater>
</ItemTemplate
</asp:Repeater>
and on call behind
public int cLastOrderID = -1;
protected string GetOrderID(object oItem)
{
cLastOrderID = (int)DataBinder.Eval(oItem, "OrderID");
return cLastOrderID.ToString();
}
What I do here is that I call the GetOrderID to return the OrderID and I save it in a global value, and then later in the footer I use this global value. Hope this helps you.

Conditional Display in a ListView

I have the following ListView code:
<asp:ListView ID="lvOrders" runat="server">
<LayoutTemplate>
<ul id="orderList">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<% if (Mode == AdminSingle) { %>
<%# Eval("Offertype")%>
<% } %>
<%# Eval("Something")%>
</li>
</ItemTemplate>
</asp:ListView>
Now, my problem is that Eval("Offertype") is being evaluated in any case. But the sql query is pretty heavy for that part and I would like to only run that part of the query if I really need too. Is there a clean way around this?
I know I could do this:
<%# GetStatusInfo(Container.DataItem)%>
But then I get the display logic into my code....

ASP.NET Repeater bind List<string>

I am binding a List<string> to a Repeater control. Now I want to use the Eval function
to display the contents in ItemTemplate like
<%# Eval("NAME") %>.
But I am not sure what I should use instead of NAME.
Just use <%# Container.DataItem.ToString() %>
If you are worried about null values you may want to refactor to this (.NET 6+)
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<%# Container.DataItem?.ToString() ?? string.Empty%>
</ItemTemplate>
</asp:Repeater>
Note if you are using less than .NET 6 you cannot use the null-conditional operator Container.DataItem?.ToString()
Set the ItemType to System.String
<asp:Repeater ItemType="System.String" runat="server">
<ItemTemplate>
<%# Item %>
</ItemTemplate>
</asp:Repeater>
rptSample.DataSource = from c in lstSample select new { NAME = c };
in the repeater you put
<%# Eval("NAME") %>
This should work just fine:
<ItemTemplate>
<%=this.GetDataItem().ToString() %>
</ItemTemplate>
A more complete example based on the LINQ provided by #RobertoBr:
In code behind:
List<string> notes = new List<string>();
notes.Add("Value1")
notes.Add("Value2")
repeaterControl1.DataSource = from c in notes select new {NAME = c};
repeaterControl1.DataBind();
On page:
<asp:Repeater ID="repeaterControl1" runat="server" >
<ItemTemplate>
<li><%# Eval("NAME") %></li>
</ItemTemplate>
</asp:Repeater>
you have to use the databind syntax here or it will not work.
<%# this.GetDataItem().ToString() %>
Inside Item Template
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>'></asp:Label>
<ItemTemplate>
or Simply Add inside Item Template
<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>

Resources