Display content in Repeater - c#

I must have done so that I can print anything into my Repeater, but it is such that I can not really for it to write the content that I really want to do.
I have written the code like this
forum.aspx
<asp:Repeater ID="RepeaterKommentar" runat="server">
<ItemTemplate>
<div class="kommentarBox">
<%--print content here--%>
</div>
</ItemTemplate>
</asp:Repeater>
forum.aspx.cs
var UsersKommentar = from KommentarA in db.forumkommentars
join BrugereA in db.brugeres on KommentarA.brugerid equals BrugereA.Id
where KommentarA.forumid == getid
orderby KommentarA.Id descending
select new
{
UserName = BrugereA.fornavn + " " + BrugereA.efternavn
};
RepeaterKommentar.DataSource = UsersKommentar;
RepeaterKommentar.DataBind();
The problem, is I'm not entirely sure how to access the object of Enumerable through my data source.

I'm not sure what data exist within your object, but you've bound it to the repeater and called data bind. So all you need to do now is call:
<%# Eval("...") %>
The ... would represent the name of the column, or property.

Related

query string on link button

In a GridView I have the list of 2 bed and breakfasts (ShowAllBeb.aspx) which are in DB, one with idname = 0 and the other with idname = 1. I've created LinkButtons that will refer to the B&B detail (DisplayBeb.aspx)
Code aspx:
<asp:LinkButton ID="Label4" runat="server" Text='<%# Eval("name") %>'
ForeColor="#0066cc"
PostBackUrl='<%# "~/site/DisplayBeb.aspx?idname={0}" + Eval("name") %>'>
</asp:LinkButton>
code aspx.cs:
using (dcDataContext dc = new dcDataContext())
{
DataListBeb.DataSource = from beb in dc.beb
select beb;
string fullname1 = Request.QueryString["idname"];
DataListBeb.DataBind();
With this code, if I click on all 2 B & Bs, the detail page of the idname = 0 comes out for both of them. How can I make the other detail page of the B&B visible?
It looks like the query string itself is correct, so you're focusing on the wrong place for the problem. You are successfully getting the query string value:
string fullname1 = Request.QueryString["idname"];
You're just not using that value for anything. I suspect you probably want to use it in a where clause on your query:
string fullname1 = Request.QueryString["idname"];
DataListBeb.DataSource = from beb in dc.beb
where beb.name == fullname1
select beb;
or:
string fullname1 = Request.QueryString["idname"];
DataListBeb.DataSource = dc.beb.Where(b => b.name == fullname1);
Of course I don't know what your beb model looks like or what its properties are called, so beb.name was entirely a guess based on the use of Eval("name") above. Simply use whatever property is appropriate in your query.

Sorting a list with asp.net usercontrols by property value

I am populating a div with many usercontrols, right now they are being added in the order that they are being read from the db. The UserControls markup looks like this:
<div class="Content_Thumb">
<div class="Video_pic">
<a href="tutorialname.aspx">
<asp:Label ID="TutInfo" CssClass="CoverLbl_top" runat="server" Text="Label"></asp:Label>
<asp:Label ID="TutInfo2" CssClass="CoverLbl_bottom" runat="server" Text="Description..."></asp:Label>
<asp:Image ID="ThumbPic" CssClass="Thumb_Pic" runat="server" ImageUrl="/Images/Video_Thumb.png" />
</a>
</div>
<div class="Thumb_Info">
<asp:Label ID="Views" CssClass="ViewsLbl" runat="server" Text="680"></asp:Label>
<asp:Label ID="Comments" CssClass="CommentsLbl" runat="server" Text="11"></asp:Label>
<asp:Label ID="Likes" CssClass="LikesLbl" Style="TEXT-ALIGN: right" runat="server" Text="133"></asp:Label>
</div>
<div class="Thumb_Border">
<asp:Image ID="UserPic" CssClass="Thumb_UploaderPic" runat="server" ImageUrl="/Images/Profile_Placeholder.png" />
<a href="google.com">
<asp:Label ID="UserLbl" CssClass="Thumb_UploaderInfo" runat="server" Text="Label"></asp:Label>
</a>
</div>
Then i've got these properties that are bound to the labels (some removed since i dont want to clutter the post).
public string TutorialInfo
{
get;
set;
}
public int TutorialViews
{
get;
set;
}
public override void DataBind()
{
TutInfo.Text = TutorialInfo;
Views.Text = TutorialViews.ToString();
Comments.Text = TutorialComments.ToString();
Likes.Text = TutorialLikes.ToString();
UserLbl.Text = TutorialUploader;
base.DataBind();
}
Now, what I want to do with these controls are to sort them by the property values. (highest number of views loads first etc). I'm currently at the stage where i've added them to a list, and now I'm trying to sort that list using LINQ's OrderBy like I saw on a post here.
List<UserControl> controls = new List<UserControl>();
foreach (var Tutorial in dataconnection.Tutorial)
{
var control = LoadControl("~/WebUserControl1.ascx") as WebUserControl1;
control.TutorialInfo = Tutorial.Title;
control.TutorialComments = (int)Tutorial.Comments;
control.TutorialViews = (int)Tutorial.Views;
control.TutorialLikes = (int)Tutorial.Likes;
control.TutorialUploader = Tutorial.Uploader;
control.DataBind();
//base.OnPreRender(e);
ThumbTest.Controls.Add(control);
controls.Add(control);
var testcount = controls.Count();
control.Visible = false;
}
var SortedList = controls.OrderBy(o => o.TutorialViews).ToList();
However i'm not able to access the properties even though they are public? "o.TutorialViews" is throwing an error. So how can i sort these controls?
I'm Fairly new to programming in general so my apologies if this turns out to be a silly mistake.
TLDR: How do I sort Usercontrols by property value.
Your list is a list of UserControl objects, which is the generic user control provided by ASP.NET:
List<UserControl> controls = new List<UserControl>();
The UserControl object doesn't have a property called TutorialViews, so you can't access that property on that type. However, you're adding custom user controls to the list:
var control = LoadControl("~/WebUserControl1.ascx") as WebUserControl1;
If every item in the list is going to be of type WebUserControl1 then you can declare the list itself with that type:
List<WebUserControl1> controls = new List<WebUserControl1>();
Since WebUserControl1 does define that property, you'll be able to access it.
Another approach might be to cast it when you access it, like this:
controls.OrderBy(o => ((WebUserControl1)o).TutorialViews)
But that's kind of messy and shouldn't be necessary. It's better to use the correct type in the first place.
Essentially, this is a symptom of the statically typed nature of C# (and many other languages). Type B inherits from Type A, so every instance of B is an instance of A. But not every instance of A is an instance of B, so you can't call a B property on an instance of A.
Think of an analogy... You have a list of Shape objects, and you populate it with Circle objects. This works, because Circles are Shapes. But then you try to access the Radius property on an element in that list, and you get an error. This is because it's a list of Shapes and not every shape has a Radius. You'd need to either make it a list of Circles or cast that element to a Circle for the compiler to know that there's a Radius.
For lots of further reading on the subject, take a look at Polymorphism and the Liskov Substitution Principle.

how to use eval function to get data from string array

I have listview and String array datasource. How can I bind them using eval ?
<telerik:RadListView ID="lvDevSampTableSelection" runat="server" AllowMultiItemSelection="true">
<ItemTemplate>
<p><%# Eval("??") %></p>
</ItemTemplate>
</telerik:RadListView>
here is code behind
ResultDto<String[]> result = client.GetTableNames("IMAPPUSER");
var source = new BindingSource();
source.DataSource = result.Data;
lvDevSampTableSelection.DataSource = source;
lvDevSampTableSelection.DataBind();
I use <%# Container.DataItem %> instead of eval and get data from string to listview
You can't, it needs to evaluate field names from the object that is the datasource (be it columns in datatable or fields in some List). Thus, I think your best option is to create a custom class with a couple of fields, create a List<> from that class and bind to that list.

If inside html code with entity framework

I am using asp.net webform 4.5.1 code first with entity framework. I used one repeater and bind it to my entity class. I want use if statement to decide show one DIV in this repeater or not. my code is :
<asp:Repeater ID="ProductRepeater" runat="server"
ItemType="Models.Product"
SelectMethod="ProductRepeate_GetData">
<ItemTemplate>
<% if(Item.Rank > 5 && Item.X != null && Item.Y != null){%>
<div>I want show this div just if my if statement is True</div>
<%}%>
<div >
<%# Item.Name%>
</div>
</ItemTemplate>
</asp:Repeater>
I want show the first div just when the result of if statement is True. the error is : The name 'Item' does not exist in the current context
This isn't the kind of calculation that you would want to include inline; not only will it be very difficult to read, it will also be very difficult to debug.
Instead, create a label <asp:Label ID="outputLabel" runat="server" ></asp:Label> and set the value of the label from the ItemDataBound Event on the repeater.
protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Label output = (Label)item.FindControl("outputLabel");
Product product = (Product)item.DataItem;
if (product.Rank > 5 && product.X != null && product.Y != null)
{
output = "I want show this div just if my if statement is True";
}
else
{
output = product.Name;
}
}
I know you already got an answer, you can do it in markup also like this:
<%# (Item.Rank > 5 && Item.X != null && Item.Y != null)? "<div>I want show this div just if my if statement is True</div>" : "" %>

Unable to bind Header Template with L2S IQueryable list

I have method that is returning IQuerable given below:
internal IQueryable<TradeLeads> GetLeadsByCategory(int categoryId)
{
return
_context.BuySells.Where(bs => bs.CategoryId == categoryId).OrderBy(bs => bs.CreationDate).Select(
bs => new TradeLeads
{
Id = bs.Id,
BuySellTypeId = Convert.ToInt32(bs.BuySellTypeId.ToString()) ,
Description = bs.Description,
Flag = bs.Company.Country1.Flag,
MembershipType = bs.Company.MembershipType,
IsUsingSmsNotifications = bs.Company.IsUsingSMSNotifications,
IsVerified = bs.Company.IsVerified,
ProductImagePath = bs.ProductImagePath,
ProductName = bs.ProductName,
CompanyName = bs.Company.CompanyName,
CompanyId = Convert.ToInt32(bs.CompanyId.ToString()),
PostedDate = bs.CreationDate
});
}
All fields are having values. I am binding BuySellTypeId in the header template of the repeater control. ASPX is given below, which is in Usercontrol.
<HeaderTemplate>
<div class="grdheader">
<div class="hdrText">
<h3 id="h3TypeName">
</h3> <asp:HiddenField runat="server" ID="HidTypeId" Value='<%#Eval("BuySellTypeId") %>'/>
</div>
<div class="hdrMore">
<a href='<%#string.Format("ViewAll.aspx?t={0}",Eval("BuySellTypeId"))%>'>
<img src="cdn/images/more.png" />
View More </a>
</div>
</div>
</HeaderTemplate>
I am binding repeater from its parent page something like this. First I changed the protection level of the repeater from protected to public, so that I can access it from any where, without casting or finding from parent page.
private void BindAllBuyAndSellLeads(int categoryId)
{
var repo = new LeadsRepository();
var list = repo.GetLeadsByCategory(categoryId);
BindGrid(1, Leads1.LeadsGrid, list);
BindGrid(2, Leads2.LeadsGrid, list);
}
private static void BindGrid(int leadTypeId, Repeater gv, IQueryable<Core.Helper.TradeLeads> list)
{
var query = (from p in list
where p.BuySellTypeId == leadTypeId
select p).ToList();
Common.BindGrid(query, gv);
}
here Leads1 and Leads2 are the user control Leads.ascx. That is same usercontrol is placed at two places on page. But i am getting empty while binding. Please help , where and what i am doing wrong.
Binding in a header will never work. Binding is for ItemTemplate only; you can programmably set the value in the header, but realize the repeater is for binding multiple rows of data; there are multiple items, but only one header. Which BuySellTypeId should be used? The first one? The last one? There is no way for the repeater to tell, so you would have to programmably set the value.

Categories