Repeater and public array as NOT datasource. - asp.net

I want to using two dataSources with repeater. One is binded to dataSource property so my question is is that possible to f.ex. to also repeat some array of string as public property?

It is possible to bind to a property of your datasource that is also a collection. For example:
class Person
{
List<Phone> Phones { get; set; }
string Name { get; set; }
}
class Phone
{
string Number { get; set; }
}
void Page_Load(...)
{
List<Person> people = GetPeople();
peopleRepeater.DataSource = people;
peaopleRepeater.DataBind();
}
aspx page
<asp:Repeater ID="peopleRepeater" runat="server">
<ItemTemplate>
Name : <%# Eval("Name") %>
Phones: <br/>
<asp:Repeater ID="phonesRepeater" runat="server" DataSource='<%# (Container.DataItem as Person).Phones %>'>
<ItemTemplate>
<%# Eval("Number") %> <br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

Most ASP.NET Data controls only will bind to a single data source at a time. It is possible to have nested controls that can be bound to multiple data sources. Some 3rd party controls such as RadGridView are designed to handle multiple datasource binding (e.g. hierachical).

Related

User Control inside Repeater - Values lost on PostBack - RadRating

I have a UserControl Inside a repeater, coded in the following way:
ParentPage.aspx:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<uc1:MovieDetailPanel runat="server" myMovieID='<%# Eval("movieID") %>' myMovieName='<%# Eval("movieName") %>'
myMovieDescription='<%#Eval("movieDescription") %>' myMovieImagePath='<%# Eval("posterImage") %>'
myMovieYear ='<%# Eval("movieYear") %>' myMovieGenre ='<%# Eval("movieGenre") %>' myMovieAverageRating ='<%# Eval("movieRating") %>'
myMovieCountry ='<%# Eval("movieCountry") %>' myMovieLanguage ='<%# Eval("movieLanguage") %>'
id="MovieDetailPanel1" />
</ContentTemplate>
</ItemTemplate>
</asp:Repeater>
ParentPage.cs:
private void LoadMyControls()
{
Repeater1.DataSource = new Common().getMoviesList(selectedGenre, selectedYear, selectedPopularity, selectedLanguage, selectedCountry);
Repeater1.DataBind();
}
UserControl.cs:
public int myMovieID { get; set; }
public string myMovieName { get; set; }
public double myMovieAverageRating { get; set; }
//some more properties
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MovieRatingControl.Value = myMovieAverageRating ;
}
}
I have a Telerik RadRating (ID# MovieRatingControl) in my UserControl. On page load of the UserControl I am setting the value of the rating control, as passed by repeater from Database (wrapped it inside if(!Page.IsPostBack) as shown above. So each movie gets its own rating as present in the database.
However, on postback (when I click the rate button), the RadRating Control loses its value and becomes 0. This happens on all the UserControls inside the repeater and not just the UserControl in which I clicked the Rate button. Can someone please let me know how I am supposed to retain this rating value ? I need to get that and update my DB Tables.
P.S: If im not using if (!Page.IsPostBack), I retain values from DB but not the rating set by the user. The issue is, the rating set by user is not maintained.

How to call a code-behind method from aspx page?

I've an object that contains a field called DevList which is defined like this
public List<string> DevList { get; set; }
I also defined a method called DisplayListOfDevelopers that is supposed to concatenate the list of developers and display it as a one string.
This is how I'm calling the method from aspx.
<asp:TemplateField HeaderText = "Developer(s)">
<ItemTemplate>
<asp:Label
ID="_lblDevList"
runat="server"
Text= '<%# DisplayListOfDevelopers(DevList) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
But, I'm getting this error: The name 'DevList' does not exist in the current context
Am I missing something?
EDIT
_gvStatus = ds;
_gvStatus.DataBind();
Where ds is just a list of objects that contains the DevList for now.
Thanks for helping
Assuming this is how your class looks:
public class MyItem
{
public List<string> DevList { get; set; }
}
And that
ds = List<MyItem>();
Do this:
In your code-behind:
protected string DisplayListOfDevelopers(object _devList)
{
//Cast your dev list into the correct object
}
In your markup:
<asp:TemplateField HeaderText="Developer(s)">
<ItemTemplate>
<asp:Label
ID="_lblDevList"
runat="server"
Text='<%# DisplayListOfDevelopers(Eval("DevList")) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Just be sure to make the function in your code-behind is protected or public.

Set SelectedValue of DropDownList inside DataGrid

I got a datagrid where a datasource #1 is bound to, e.g.
public class Class
{
public string Val { get; set; }
public string Val2 { get; set; }
}
List<Class> classes = new List<Class>();
dgr.DataSource = classes;
Inside this datagrid i got a listbox for each row with a datasource #2 bound to:
<Columns>
<asp:TemplateColumn HeaderText="Spaltenname">
<ItemTemplate>
<asp:ListBox runat="server" DataTextField="Text" DataValueField="Value" DataSource="<%#oParentTablesHandler.DataTableXYZ%>" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
This works, but I have problems setting the SelectedValue.
SelectedValue="<%# "" %>
will work but I need a selection depending on Val from datasource #1. How can I do that? I need to use Eval i guess, but
SelectedValue="<%# Eval("Val") %> did not work...
edit: I found out that I want to select an item by text and not by value, argh. Is there a way to do that?
Try like this..
<%# ((Class)Container.DataItem).Val %>
UPDATE:
well.there may be some value in 'Val' property that may not exist in your Datasource#2's corresponding column...
So for test purpose try following..
<asp:ListBox .. AppendDataBoundItems="true">
<Items>
<asp:ListItem Text="NA" Value="" />
</Items>
</asp:ListBox >

Public Properties in Code Behind of a User Control

In a regular .aspx page, you can access public properties from the codebehind. Is there any way to do a similar thing in a user control. For example, in the following code 'List' is public property of the codebehind of the user control and yet it is not accessible.
<% foreach (TripTeam team in List) { %>
<div>
<label><%= team.Name %></label>
</div>
<%} %>
You can access the public properties of a UserControl from your .aspx page. Here's an example
<script runat="server">
public string Caption { get { return _caption.Text; } set { _caption.Text = value; } }
public string Text{ get { return _tb1.Text; } set { _tb1.Text = value; } }
<div>
<asp:Label ID="_caption" runat= "server" class="caption" /><br />
<asp:TextBox ID="_tb1" runat="server" CssClass="textBox" Width="25px" />
Then on your aspx page, you can set the Text and Caption properties within your user control:
<uc1:CaptionText ID="ct1" runat="server" Caption="User name" />
You need to say 'userControlId.List' to access the property when accessing a property in the control from a page which uses the control.

Binding Gridview to IList<BusinessObject> that contains an IList<BusinessObject>

I'm having trouble figuring out how to bind a custom IList to a gridview. The IList contains another custom IList. I need to bind a property from this IList to the gridview.
public class Seminar : BusinessObject
{
private string _description = String.Empty;
private List<User> _attendees;
public string Description {get {return _description;} set {_description = value;}}
public List<User> Attendees {get {return _attendees;} set {_attendees = value;}}
}
public class User : BusinessObject
{
private string _name = String.Empty;
public string Name { get { return _name; } set { _name = value; } }
}
Backend page.aspx.cs:
List<Seminar> seminarList = SeminarManager.Get(id);
gridSeminars.DataSource = seminarList;
gridSeminars.DataBind();
Frontend page.aspx:
<asp:GridView ID="gridSeminars" runat="server">
<Columns>
<asp:BoundField DataField="Id" />
<asp:BoundField DataField="Description" />
<asp:BoundField DataField="Name" />
</Columns>
</asp:GridView>
The problem is with populating the "Name" field in the gridview. All suggestions are welcome.
Have you tried,
Attendees.Name
Edit:
Attendees is an IEnumerable itself. The above suggestion only works with Attendee.Name
If you wan´t to display all the attendees you need to make a templatefield and maybe use a repeater or something...something like:
<asp:TemplateField>
<ItemTemplate>
<asp:Repeater runat="server" DataSource='<%# Eval("Attendees") %>'>
<ItemTemplate>
<tr>
<td>
Name
</td>
<td>
<%# Eval("Name")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
Johan's answer is a good one, specifically the part about how you are trying to bind a collection to a column that is expecting something that can be converted to a string.
Another option would be to put a TemplateField that contains the bindable control (Repeater, another GridView, etc.) you choose to use and binding the DataSource property to the Attendees property of your business object.
Also, I don't if you're married to the idea of using a GridView, but in cases like these where you need more control of the layout, I would suggest the new ListView (.NET 3.5) control, or lately I've just been using nested repeaters so I can have more refined control of the layout I am trying to generate.
Hope this helps.
I'm assuming you want to flatten the hierarchy and display one row for every user. If you have access to Linq, you can use this:
List<Seminar> seminarList = SeminarManager.Get(id);
gridSeminars.DataSource = from seminar in seminarList // loop through all of the seminars in the list
from user in seminar.Attendees // loop through all of the users in the current seminar
select new // create a new, flattened object to bind to.
{
seminar.Id,
seminar.Description,
user.Name
};
gridSeminars.DataBind();
http://weblogs.asp.net/zeeshanhirani/archive/2008/03/26/select-many-operator-part-1.aspx

Resources