I have 10 drop down lists on my page 5 are for employee names and they are labeled ddlemp1, ddlemp2, ddlemp3, ddlemp4, ddlemp and I have 5 drop down lists for level and they are labeled ddllvl1, ddllvl2, ddllvl3, ddllvl4, ddllvl5
Is it possible to bind the same items to all my drop down lists that are like ddlemp and bind a second set of choices to all drop down lists that are named like ddllvl?
And I am populating the drop down lists like so:
this.ddlemp1.Items.Insert(1, new ListItem("Fran", "14"));
this.ddlemp1.Items.Insert(2, new ListItem("George", "3")):
EDIT
Would a repeater work in my instance as I have one drop down list of each on a row in my table, and from my understanding of a repeater Each element I need to be "repeated" should fall under the same repetaer. So unless I could nest a ddllvl repeater inside my ddlemp repeater somehow?
<asp:Repeater ID="RepeateDropDownListSelections" runat="server">
<tr id="row6" runat="server">
<td><asp:DropDownList ID="ddlemp6" runat="server" Height="16px" Width="278px"></asp:DropDownList></td>
<td class="style2"><asp:DropDownList ID="ddllvl6" runat="server" Height="16px" Width="45px"></asp:DropDownList></td>
</tr>
</asp:Repeater>
Instead of adding the list items to your drop downs, instead add them to a separate list and bind that to each control. For example:
var employees = new List<ListItem>
{
new ListItem("Fran", "14"),
new ListItem("George", "3")
};
ddlemp1.DataSource = employees;
ddlemp1.DataBind();
ddlemp2.DataSource = employees;
ddlemp2.DataBind();
...
ddlemp5.DataSource = employees;
ddlemp5.DataBind();
Repeat the same for your other list.
Related
I have a repeater inside a repeater, and I trying to bind the child repeater with a list of Aka's from session.
My codebehind looks like:
var results = HttpContext.Current.Session["completeRecord"];
var rep = (Repeater)e.Item.FindControl("rptAkas");
var akaList = new List<string>();
foreach (Entity list in (IEnumerable<Entity>) results)
{
akaList.AddRange(list.Aka);
}
rep.DataSource = akaList;
rep.DataBind();
In my child repeater I have the following:
<asp:Repeater runat="server" ID="rptAkas" OnItemDataBound="repeater_OnItemDataBound">
<ItemTemplate>
<p><strong>Aka</strong><asp:Literal runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "akaList") %>'></asp:Literal></p>
</ItemTemplate>
</asp:Repeater>
this throws the following message:
Additional information: DataBinding: 'System.String' does not contain a property with the name 'akaList'.
How can I bind this list to the repeater?
EDIT
Container.DataItem took care of it.
Container.DataItem is the current item your Repeater is iterating over. It's not the list of strings, it's each string individually.
Think of what you're doing in a more pseudocode form:
foreach ( var a_single_string in Repeater.DataSource )
{
var some_var = a_single_string.akaList;
}
When you reference Container.DataItem, it's the same as addressing a_single_string in the example above.
Not practically applicable code by a long shot, but I hope it explains why you are getting the error. You are actually asking for the property "akaList" of a string, hence the error.
I must bind a gridview to a method which returns list<>, there are few multi select lookups in the list, for this I have a method which returns dictionary<int, string>.
In the list I have the multi select item values as {1, xyz}, {2, abc}
So I must display in the grid as xyz, abc.
For this I have written a method FormatString which is called in gridview binding.. i.e aspx page
<%# FormatString(?????????) %>
I must pass list<> in the ???? to retreive the data..
Please suggest me some solutions..
'<%# FormatString(Container.DataItem as YourListType) %>'
public String FormatString(YourListType listObj )
{
//do what you want.
}
You can try this way
Get the values to a different list from your dictionary items
var items = yourDictionary.Values.SelectMany(x => x).ToList();
Then you can simply bind the list to gridview
So I have tried and searched far and wide but cannot get my nested repeater to work properly.
<asp:Repeater ID="rptrParent" runat="server">
<ItemTemplate>
<dl class="StudentTasksList">
<dt><%# Eval("ClassCode") %></dt>
<asp:Repeater ID="rptrChild" runat="server">
<ItemTemplate>
<dd><%# Eval("Title") %></dd>
</ItemTemplate>
</asp:Repeater>
</dl>
</ItemTemplate>
</asp:Repeater>
C# Code
private void ListTasks()
{
using (StudentContext syndb = new StudentContext())
{
using (TaskContext dbdb = new TaskContext())
{
// Iterate through each active Class for the current Student finding any tasks not archived for this year
var ClassTasks = (from c in syndb.GetClasses()
where c.Students.Select(y => y.StudentID).Contains(LoggedInUserID)
join t in dbdb.Tasks on c.ClassCode equals t.ClassCode
where !t.Archive.HasValue || t.Archive.HasValue && t.Archive.Value && t.DueDate.Value.Year == DateTime.Now.Year
group c by c.ClassCode);
rptrParent.DataSource = ClassTasks;
rptrParent.DataBind();
}
}
}
So ClassCode comes from the GetClasses() method for the selected student, and Title is the name of any Titles found in the Tasks join for the current Class. The Result I need is a list of the classes for a student with any related task titles under each of the ClassCode. I have tried this several different ways so this may not be the best sample of what I am trying to do. I would get great if someone could show me a LINQ C# example of a joined linq query that populates a nested repeater as I cannot find anything decent enough to work this one out.
I would also like to know how to populate an N/A in place if the class doesn't have any tasks, but I wont push my luck.
To populate the inner repeater, you must handle the ItemDataBound event of the parent repeater like this:
<asp:Repeater ID="rptrParent" runat="server" OnItemDataBound="rptrParent_ItemDataBound">
And in the code behind
protected void rptrParent_ItemDataBound(object sender, RepeaterItemEventArgs e) {
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem)) {
Repeater rptrChild = (Repeater)item.FindControl("rptrChild");
rptrChild.DataSource = DataBinder.Eval(item.DataItem, "Group");
rptrChild.DataBind();
}
}
I guess that the property of the data item you need to use as data source of the inner repeater is Group as you are grouping in your LINQ statement, but maybe you need to change that...
As far as I know LINQ dose not play any role in data binding. The LINQ query you have mentioned looks find and may populate data as you needed. But You have nasted Repeater so "rptrParent.DataBind();" will bind the outer most repeater only. I think you have to write ItemDataBound event for "rptrParent" and find child repeater control assign it with group you got from LINQ query.
Hope this will help you.
I'm not sure how to correct the following problem. I have dropdown list that has a object data source. Then in the code there is a method like this
void InitPageData()
{
MembershipUser user = Membership.GetUser();
DataSetTableAdapters.MemberInfoTableAdapter da = new DataSetTableAdapters.MemberInfoTableAdapter();
DataSet.MemberInfoDataTable dt = da.GetMember((Guid)user.ProviderUserKey);
if (dt.Rows.Count == 1)
{
DataSet.MemberInfoRow mr = dt[0];
//rank.Text = mr.rank;
//position.Text = mr.position;
UserName.Text = user.UserName;
...
}
This method populates form fields on the page. What I'm trying to do is to have the rank dropdown list populated from the ods but use this method above to populate the selected item of the rank dropwon list with the line rank.Text = mr.rank. In this example the the line of code that throws the error is commented out otherwise it throws this: "'rank' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value".
I've chaned the code to rank.DataTextFiled = mr.rank and rank.DataValueField = mr.rankid.ToString() but this threw another error: "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Star'." "Star" is the value of the mr.rank.
Here is what the dropdown list and the ods look like:
<asp:DropDownList runat="server" ID="rank" CssClass="txtfield" DataSourceID="ODCRanks"
DataTextField="Rank" DataValueField="ID" AppendDataBoundItems="True">
<asp:ListItem Text="--- Select a Rank ---" Value="-1" />
<asp:ObjectDataSource ID="ODCRanks" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetRanks"
TypeName="RanksTableAdapters.RankTableAdapter"></asp:ObjectDataSource>
You should try adding data columns to your data tables (with ID and Rank being the column name) so that the data can be binded to control.
The Text property sets it by value.
See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.text.aspx
You seem to have what will be the text associated with the value and you want to set that as the selected item. I guess this mostly because your value collection is bound to something called ID and I figure a rank of Star isn't an ID.
If Star is what will show up as something in DataTextField, use the items collection FindByText method to select it.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection.findbytext.aspx
Example
ListItem li = DropDownList1.Items.FindByText("one");
if (li != null) li.Selected = true;
If Star is indeed an ID, then check that the collection is fully loaded before trying to select anything in it.
try something like
rank.SelectedIndex = rank.Items.IndexOf(rank.Items.FindByText(mr.rank));
I have a listbox with 20 colors. It looks something like this:
1:Red
2:Green
3:Blue
4:Orange
5:Red
6:Yellow
7:Orange
8:Red
9:Green
....
It gets the data from an ObjectDataSource which in turn gets it's data from a method which returns a datatable. I want a dropdown which basically has 2 items, Order By # and Order By Color. If the user chooses Order By #, it will Order the ListBox in ascending or descending order. If the user chooses Order By Color, it will Order By Color. How do I go about doing this?
Can I sort this using a DataView?
Thanks,
XaiSoft
You can add the sort expression to your ObjectDataSource as a Select parameter, you can define it like so:
<asp:ObjectDataSource
ID="ObjectDataSource1"
runat="server"
SelectMethod="SelectMethod"
TypeName="MyDataObject">
<asp:Parameter Direction="input" Type="string" Name="sortExpression">
</asp:Parameter>
</asp:ObjectDataSource>
Then in the "SelectMethod" method where the data is retrieved add a parameter of that name and return a DataView:
public DataView SelectMethod(string sortExpression)
{
DataTable table = GetData();
DataView dv = new DataView(table);
dv.Sort = sortExpression;
return dv;
}
Then in the wizard for the ObjectDataSource you can bind that Parameter to the dropdown SelectedValue. Make the value of each of the DropDown items the same as your column names.
(I'm assuming you've already figured out how to bind the ListBox in the first place.)
Set the property AutoPostback="true" on your DropdownList. This will cause the SelectedIndexChanged event to fire when the user picks a different value.
In there you can rebind your listbox.
Edit: deleted my misunderstanding around the ObjectDataSource - joshperry's answer covers that much better!
Just wonder... You already have the data in the ListBox, why not sorting it using javascript? To avoid go back to the server and ask for the same thing.
just get the correct list box id and you're done!
<script language="JavaScript" type="text/javascript">
function sortlist() {
var lb = document.getElementById('mylist'); // <-- Use $get(<%# myList.ClientID %>); if you want
arrTexts = new Array();
for(i=0; i<lb.length; i++) {
arrTexts[i] = lb.options[i].text;
}
arrTexts.sort();
// arrTexts.reverse() // <-- uncomment if you want descending
for(i=0; i<lb.length; i++) {
lb.options[i].text = arrTexts[i];
lb.options[i].value = arrTexts[i];
}
}
</script>
<select name="mylist" id="mylist" size="5">
<option value="Anton">Anton</option>
<option value="Mike">Mike</option>
<option value="Peter">Peter</option>
<option value="Bill">Bill</option>
<option value="Carl">Carl</option>
</select>
<br />
sort