how to use eval function to get data from string array - c#

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.

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.

Display content in Repeater

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.

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.

Can you call PadLeft inside a databound server control?

Say I have a label inside a gridview template column and I assign the text to the databound item, "OrderID". Is there any way I can call .ToString().PadLeft(7, '0') ? Here's the label as it stands:
<asp:Label ID="lblOrderID" runat="server" Text='<%# "WW" + DataBinder.Eval(Container.DataItem, "OrderID") %>' />
Because PadLeft requires a char, I can't use my single quotes because I've already surrounded the value in single quotes and can't do double quotes because of the DataBinder.Eval expression. Is there another way I can get this to work on one line?
You can do it inline (I'll leave it to someone else to give you the answer as I don't have VS open to test it).
But I would do it by adding a method in the codebehind to return a formatted order id. Or even better, put the format method in a static helper class so it's available to any page that needs a formatted order id.
E.g. if you're binding to a collection of Orders, something like:
public static string GetFormattedOrderId(object dataItem)
{
Order order = (Order) dataItem;
return String.Format("WW{0:N7}", order.OrderId); \
// or return order.OrderId.ToString().PadLeft... if you prefer
}
or if you're binding to a DataTable, something like:
public static string GetFormattedOrderId(object dataItem)
{
DataRow row = (DataRow) dataItem;
return String.Format("WW{0:N7}", row["OrderId"]);
}
then you can have a consistenly formatted order id anywhere in your markup:
'<%# MyFormattingHelper.GetFormattedOrderId(Container.DataItem) %>'

C# Repeater.DataSource from 2 StringCollections

I have 2 StringCollections:
StringCollection ParameterIDs
StringCollection ParameterValues
Is it able to map these both StringCollections as DataSource, something like:
repeater.DataSource = ParameterIDs (as row1) + ParameterValues (as row2);
repeater.DataBind();
and use them in repeater like:
<asp:Repeater ID="repeatParameters" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="formLabel">
<asp:Label ID="lblParameterID" Text="<% #DataBinder.Eval(Container.DataItem,"ParameterIDs") %>" runat="server" MaxLength="50"></asp:Label><br />
</td>
<td class="formInputText">
<asp:Label ID="lblParameterValue" Text="<%#DataBinder.Eval(Container.DataItem,"ParameterValues") %>" runat="server" MaxLength="50"></asp:Label><br />
</td>
</tr>
<tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
First thought:
Mash them up in a Dictionary and use that as a datasource.
Second thought:
Create a DataSet with the values you need.
Third thought:
Use KeyValuePair.
As you see there are a lot of different ways of doing this but all of them share a common element:
Create a single object which stores and maps the corresponding values.
No, because the DataSource requires an object with the ICollection interface.
Like the others said, you could create a dictionary or something like:
List<KeyValuePair<string,string>>
Where the parameter id is the key, and the parameter value is the value in the KeyValuePair.
This will do exactly what you want, providing you are using .NET 3.5:
StringCollection parameterIds = new StringCollection();
StringCollection parameterValues = new StringCollection();
parameterIds.Add("someID");
parameterValues.Add("someValue");
var dataSource = parameterIds.Cast<string>()
.SelectMany(itemOne => parameterValues
.Cast<string>(), (itemOne, item2) => new { Row1 = itemOne, Row2 = item2 });
repeater.DataSource = dataSource;
repeater.DataBind();
Like Bogdan_Ch has said, I recommend you move away from StringCollection to List<string> if you are using .NET 2.0 and above. Doing that has the added advantage that you don't need to use the Cast extension method.
I would populate a Dictionary with your string collections and then bind to the Dictionary.
I would suggest 2 options
1. In .NET 1.1 you don't have generic lists, so you may join collections manually in your code, and then use joined collectionas a datasource
StringCollection joined = new StringCollection();
foreach(string s in stringCollection1)
joined.Add(s);
foreach (string s in stringCollection2)
{
if (!joined.Contains(s))
joined.Add(s);
}
In .NET 2.0 and more I would suggest you to use List instead of StringCollection
With AddRange() you can add another list to an existing list.
In .NET 3.5 you can use Linq to intersect 2 lists
public static IEnumerable<TSource> Intersect<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second
)

Categories