Combo box with multiple columns and related values - asp.net

Is it possible to show multiple columns and headers inside of a combo box/dropdown list in asp.net and show related columns values, for an example, if I click on a country name then it should show me all the cities for that country and clicking on city name should show me all the related places.
Is there any third part control available? I have checked telerik and they have combo box with multiple columns but not with related values.

I hope this will help you to get started.
<telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Population")%>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadComboBox>
<telerik:RadComboBox ID="RadComboBox2" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="RadComboBox2_SelectedIndexChanged">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Population")%>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadComboBox>
<telerik:RadComboBox ID="RadComboBox3" runat="server">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Population")%>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadComboBox>
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
public int Population { get; set; }
}
public class State
{
public int Id { get; set; }
public int CountryId { get; set; }
public string Name { get; set; }
public int Population { get; set; }
}
public class City
{
public int Id { get; set; }
public int StateId { get; set; }
public string Name { get; set; }
public int Population { get; set; }
}
public List<Country> Countries
{
get
{
return new List<Country>
{
new Country {Id = 1, Name = "United States", Population = 1000},
new Country {Id = 2, Name = "Canada", Population = 2000},
new Country {Id = 3, Name = "Mexico", Population = 3000}
};
}
}
public List<State> States
{
get
{
return new List<State>
{
new State {Id = 1, CountryId = 1, Name = "California", Population = 100},
new State {Id = 2, CountryId = 1, Name = "New York", Population = 200},
new State {Id = 3, CountryId = 2, Name = "Quebec", Population = 300},
new State {Id = 4, CountryId = 2, Name = "Ontario", Population = 400}
};
}
}
public List<City> Cities
{
get
{
return new List<City>
{
new City {Id = 1, StateId = 1, Name = "Los Angeles", Population = 10},
new City {Id = 2, StateId = 1, Name = "San Diego", Population = 20},
new City {Id = 3, StateId = 1, Name = "San Francisco", Population = 30},
new City {Id = 4, StateId = 1, Name = "San Joe", Population = 40},
new City {Id = 5, StateId = 2, Name = "New York", Population = 50},
new City {Id = 6, StateId = 2, Name = "Paterson", Population = 60},
new City {Id = 7, StateId = 2, Name = "Newark", Population = 70},
new City {Id = 8, StateId = 2, Name = "Smithtown", Population = 80},
};
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
RadComboBox1.DataSource = Countries;
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataBind();
RadComboBox1.Items.Insert(0, new RadComboBoxItem("Select Country", ""));
}
}
protected void RadComboBox1_SelectedIndexChanged(
object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox2.Items.Clear();
RadComboBox3.Items.Clear();
int countryId;
if (Int32.TryParse(e.Value, out countryId))
{
RadComboBox2.DataSource = States.Where(s => s.CountryId == countryId);
RadComboBox2.DataValueField = "Id";
RadComboBox2.DataTextField = "Name";
RadComboBox2.DataBind();
RadComboBox2.Items.Insert(0, new RadComboBoxItem("Select State", ""));
}
}
protected void RadComboBox2_SelectedIndexChanged(
object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox3.Items.Clear();
int stateId;
if (Int32.TryParse(e.Value, out stateId))
{
RadComboBox3.DataSource = Cities.Where(c => c.StateId == stateId);
RadComboBox3.DataValueField = "Id";
RadComboBox3.DataTextField = "Name";
RadComboBox3.DataBind();
RadComboBox3.Items.Insert(0, new RadComboBoxItem("Select City", ""));
}
}

I don't know about 'related' values, per sé; however you may need to develop that on your own.
Besides Telerik, if you don't mind using jQuery, here are a couple of plugins you can use for free to achieve the multi-column/header aspect:
mcDropdown jQuery Plug-in v1.3.1
Jquery Multi Column Selectbox
There are a few others you can take a peak at using this google search.
Helpful reference: jQuery

Related

How to bind database using LIST?

Hi iam try to Get Database data and showed in RadTreeview,
here i got one sample,that sample like default sample,every Root and Child has to be enter manually,
but i want bind from database,values are filled automatically(root/child)
i will show that example code,
List<SiteDataItem> siteData = new List<SiteDataItem>();
siteData.Add(new SiteDataItem(1, 0, "Products"));
siteData.Add(new SiteDataItem(2, 1, "RadControls for ASP.NET Ajax"));
siteData.Add(new SiteDataItem(3, 1, "RadControls for Silverlight"));
siteData.Add(new SiteDataItem(4, 2, "RadGrid"));
siteData.Add(new SiteDataItem(5, 2, "RadScheduler"));
siteData.Add(new SiteDataItem(6, 2, "RadEditor"));
siteData.Add(new SiteDataItem(7, 3, "RadGrid"));
siteData.Add(new SiteDataItem(8, 3, "RadMenu"));
siteData.Add(new SiteDataItem(9, 3, "RadEditor"));
RadTreeView1 .DataTextField = "Text";
RadTreeView1.DataFieldID = "ID";
RadTreeView1.DataFieldParentID = "ParentID";
RadTreeView1.DataSource = siteData;
RadTreeView1.DataBind();
Please help me to do as i want
Please try with the below code snippet.
protected void Page_Load(object sender, System.EventArgs e)
{
List<TestModels> models = new List<TestModels>();
models.Add(new TestModels() { ID = 1, ParentID = null, Text = "a1" });
models.Add(new TestModels() { ID = 2, ParentID = 1, Text = "a2" });
models.Add(new TestModels() { ID = 3, ParentID = 2, Text = "a3" });
models.Add(new TestModels() { ID = 4, ParentID = null, Text = "a4" });
models.Add(new TestModels() { ID = 5, ParentID = 4, Text = "a5" });
RadTreeView1.DataTextField = "Text";
RadTreeView1.DataFieldID = "ID";
RadTreeView1.DataFieldParentID = "ParentID";
RadTreeView1.DataSource = models;
RadTreeView1.DataBind();
}
Class
public class TestModels
{
public int ID { get; set; }
public int? ParentID { get; set; }
public string Text { get; set; }
}
Please Set NULL in ParentID in place-of 0.
Because It consider 0 as value. RadTreeView is not able to found any ID field which have 0 value.
You can try something like this.
DataTable data= GetDataFromDatabase()
RadTreeView1.DataTextField = "TextFieldwhichyouwantshow";
RadTreeView1.DataFieldID = "IDFromdatabase";
RadTreeView1.DataFieldParentID = "ParentIDFromDatabase";
RadTreeView1.DataSource = data;
RadTreeView1.DataBind();

How to get the dropdownlistselectedid in asp.net

var countries = context.MasterCountries.ToList();
ddlcountry.DataSource = countries;
ddlcountry.DataTextField = "Description";
ddlcountry.DataValueField = "Id";
ddlcountry.DataBind();
<asp:DropDownList runat="server" ID="ddlcountry" CssClass="textbox textbox-reg" />
int countryid = ddlcountry.SelectedIndex;
I am trying to get the id of the company but it's not working.
Try that:
int countryid = int.Parse(ddlcountry.SelectedValue);

stuck on changing each row color during run-time in listview in asp.net based on database entries

I want to change color of each row in a listview based on a data which is stored in database.
I've written following code but I don't know by the help of which object I can access to markup property of listview :
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if(e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
int policyid = (int)DataBinder.Eval(dataitem.DataItem, "policyID");
if(policyid == 3)
//what should I write here to change the corresponding row's color?
}
}
regards.
Hey Austin Powers here is the code
ASPX CODE :
<asp:ListView ID="ListView1" runat="server"
onitemdatabound="ListView1_ItemDataBound">
<LayoutTemplate>
<table id="itemPlaceholderContainer" border="0" cellpadding="0" cellspacing="0">
<tr>
<th>
EmpID
</th>
<th>
EmpName
</th>
<th>
Department
</th>
<th>
Age
</th>
<th>
Address
</th>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="MainTableRow" runat="server">
<td id="EmpID" runat="server">
<asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' />
</td>
<td id="EmpNameTD" runat="server">
<asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
</td>
<td id="DepartmentTD" runat="server">
<asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' />
</td>
<td id="AgeTD" runat="server">
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td id="AddressTD" runat="server">
<asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
AND HERE IS THE CODEBEHIND CODE
protected void Page_Load(object sender, EventArgs e)
{
var Employee = new { EmpID = 1, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" };
var customerList = (new[] { Employee }).ToList();
customerList.Add(new { EmpID = 2, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 3, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 4, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 5, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 6, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 7, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 8, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 9, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 10, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
ListView1.DataSource = customerList;
ListView1.DataBind();
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
int policyid = (int)DataBinder.Eval(dataitem.DataItem, "EmpID");
if (policyid == 3)
{
HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("MainTableRow");
cell.BgColor = "Red";
}
}
}
tHANKS

How To Freeze Columns in GridView

I'm trying to freeze columns in gridview where grid has static height and all rows are rendered(no paging and scroll var is visible).
I only managed to add scroll through content by overflow property,but this time all columns are scrolling as well.My task is to freeze columns while maintaining column width.
Let this be my grid
<div style="height:200px;overflow:auto;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
UseAccessibleHeader="true or false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" />
</Columns>
</asp:GridView>
</div>
and this is code view
public class Person
{
public String Name { get; set; }
public String Surname { get; set; }
}
->On Page Load
List<Person> lst = new List<Person>();
lst.Add(new Person() { Name = "A", Surname = "A1" });
lst.Add(new Person() { Name = "B", Surname = "B1" });
lst.Add(new Person() { Name = "C", Surname = "C1" });
lst.Add(new Person() { Name = "D", Surname = "D1" });
lst.Add(new Person() { Name = "E", Surname = "E1" });
lst.Add(new Person() { Name = "F", Surname = "F1" });
lst.Add(new Person() { Name = "G", Surname = "G1" });
lst.Add(new Person() { Name = "H", Surname = "H1" });
lst.Add(new Person() { Name = "I", Surname = "I1" });
lst.Add(new Person() { Name = "J", Surname = "J1" });
lst.Add(new Person() { Name = "K", Surname = "K1" });
GridView1.DataSource = lst;
GridView1.DataBind();
How can i achieve this with minimum effort of coding or styling?
Note:Rendered browser is IE only.
You could use the Ideasparks CoolGridView instead. It works fine for me and is free.
Download from Codeplex.com.

What's wrong with this linqTOsql self referencing object mapping?

I'm trying to create a self referencing object using linqTOsql mapping. So far, I am definitely in over my head. Here's the code I have:
[Table]
public class Category
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 catID { get; set; }
public Int64 parentCatID { get; set; }
public string catName { get; set; }
public string catDescription { get; set; }
internal EntityRef<IEnumerable<Category>> _category;
[Association(ThisKey = "parentCatID", Storage = "_category")]
public IEnumerable<Category> category {
get { return _category.Entity; }
set { _category.Entity = value; }
}
}
My fakeRepository is defined like this:
// Fake hardcoded list of categories
private static IQueryable<Category> fakeCategories = new List<Category> {
new Category { catID = 1, parentCatID = 0, catName = "Root", catDescription = "" },
new Category { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
new Category { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
new Category { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
new Category { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
new Category { catID = 6, parentCatID = 0, catName = "Another Root", catDescription = "" },
new Category { catID = 7, parentCatID = 0, catName = "Ze German Root", catDescription = "" },
new Category { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
new Category { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();
I pass Category to the view like this:
public ActionResult CategoryTree()
{
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID == 0)
.OrderBy(x => x.catName);
return View(cats);
}
The problem that I'm running into is that all of this compiles, but I don't get anything beyond the root categories. Model[0].category is returning null.
What is wrong with my self-referencing object?
Edit
I wonder if it's not working because I don't have a real linq-to-sql data context in my fakeRepository. If that's the case, is there a way around that? Can I can get this to work without a connection to a database?
Yeah, you hit the nail on the head. It's not working because you're using a fake repository.
Linq-to-Sql does all the wiring up for you and sets the related collections based on the properties (& their attributes) that you setup in your model.
I don't know how to accomplish this without a connection to the database because internal EntityRef<IEnumerable<Category>> _category; is completely foreign to me - I'm more of a POCO model type of guy.
After a quick google, I found this - How to: Map Database Relationships (LINQ to SQL)
Could you change your model to read:
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 CatId { get; set; }
[Column]
public Int64 ParentCatId { get; set; }
[Column]
public string CatName { get; set; }
[Column]
public string CatDescription { get; set; }
private EntitySet<Category> _ChildCategories;
[Association(Storage = "_ChildCategories", OtherKey = "ParentCatId")]
public EntitySet<Category> ChildCategories
{
get { return this._ChildCategories; }
set { this._ChildCategories.Assign(value); }
}
private EntityRef<Category> _ParentCategory;
[Association(Storage = "_ParentCategory", ThisKey = "ParentCatId")]
public Category ParentCategory
{
get { return this._ParentCategory.Entity; }
set { this._ParentCategory.Entity = value; }
}
Now because your ChildCategories is of type EntitySet<Category> (which inherits from IList<T>) you should be able to wire fake relationships up yourself.
So you could do something like this:
private static IQueryable<Category> GetFakeCategories()
{
var categories = new List<Category> {
new Category { CatId = 1, ParentCatId = 0, CatName = "Root", CatDescription = "" },
new Category { CatId = 2, ParentCatId = 1, CatName = "Category w/subs", CatDescription = "" },
//Blah
new Category { CatId = 8, ParentCatId = 3, CatName = "Brand new cats", CatDescription = "" },
new Category { CatId = 9, ParentCatId = 8, CatName = "Brand new cats sub", CatDescription = "" }
};
//Loop over the categories to fake the relationships
foreach (var category in categories)
{
category.ChildCategories = new EntitySet<Category>(); //new up the collection
foreach (var subLoopCategory in categories)
{
if (category.ParentCatId == subLoopCategory.CatId)
category.ParentCategory = subLoopCategory;
if (category.Id == subLoopCategory.ParentCatId)
category.ChildCategories.Add(subLoopCategory);
}
}
return categoies.AsQueryable();
}
It works in my head at least... :-)
HTHs,
Charles
EDIT: Re: Comment below about a null reference on _childCategories.
You could change the model to look like:
private EntitySet<Category> _ChildCategories = new EntitySet<Category>();
It is supposed to be null. You are getting all categories where the ParentId = 0 ... and you don't have a child with an Id of 0. So that seems right to me.
It is not showing any subcategories because it has no subcategories to show. Try this:
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID != 0)
.OrderBy(x => x.catName);
The parentCatId needs to point to a valid CatId for it to be a subcategory. This query should get you all the categories that are subcategories.

Resources