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.
Related
I'm writing a form which uses drop down menus for value selection. I'd like to know if it is possible to post Value and Name of the SelectList back to the Controller or add values for every list item? I need to set LateralityId = Value and LateralityName = Text.
I'll just post some snippets of the code to get the idea across. The model I'm using is
public class Condition
{
public string LateralityName { get; set; }
public string LateralityId { get; set; }
}
and the values are posted with
#Html.DropDownListFor( m => m.Condition.LateralityId, GetLateralities() )
where
public static List<SelectListItem> GetLateralities()
{
return new List<SelectListItem>
{
new SelectListItem { Text = "", Value = "900" },
new SelectListItem { Text = "Left", Value = "1001" },
new SelectListItem { Text = "Right", Value = "1002" },
new SelectListItem { Text = "Midline", Value = "1003" },
new SelectListItem { Text = "Left Midline", Value = "1004" },
new SelectListItem { Text = "Right Midline",Value = "1005" },
new SelectListItem { Text = "Both Sides", Value = "1006" },
new SelectListItem { Text = "Unknown", Value = "990" },
new SelectListItem { Text = "Unspecified", Value = "997" },
new SelectListItem { Text = "N/A", Value = "999" },
};
}
Any help is much appreciated. Thanks!
Solution thanks to Stephen Muecke
In the controller I simply use
LateralityName = GetLateralities().FirstOrDefault( x => x.Value == m.Condition.LateralityId )?.Text;
I would like to have grouped items within an asp.net dropdownlist and I thought of using the SelectList functionality from MVC to my webforms project. Here's what I have tried and the result. Can you help please with the binding?
<asp:DropDownList runat="server" id="ddlGroupBySelGeo" AutoPostBack="True"
DataTextField="Text" DataValueField="Value"
SelectMethod="GetMapItems" OnSelectedIndexChanged="ddlGroupBySelGeo_OnSelectedIndexChanged">
</asp:DropDownList>
public SelectList GetMapItems(object sender, EventArgs e)
{
var items = new List<SelectListItem>();
var group1 = new SelectListGroup() { Name = "Select" };
var group2 = new SelectListGroup() { Name = "Group" };
items.Add(new SelectListItem() { Text = "display selected", Value = "select", Group = group1 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Region").ToString(), Value = "Regional", Group = group2 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Municipality").ToString(), Value = "Municipio", Group = group2 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Locality").ToString(), Value = "Localidade", Group = group2 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Neighborhood").ToString(), Value = "Bairro", Group = group2 });
return new SelectList(items);
}
Have you tried setting the Value and Text parameters in the SelectList before you return it?
return new SelectList(items, "Value", "Text");
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
I have a route
routes.MapRoute(
"AlphaPagedContacts", // Route name
"Contact/Alpha{alpha}", // URL with parameters
new { controller = "Contact", action = "AlphaList", alpha = UrlParameter.Optional },
new { alpha = #"\A-Z" } // Parameter defaults
);
I'm trying to make the URL's display like
/Contact/AlphaA
/Contact/AlphaB
for contacts based on username. However, the URL's are showing up as: http://localhost:54568/Contact/AlphaList?alpha=H
My HTMLHelper is
#Html.AlphaLinks(new PagingModel { MaxPages = Model.MaxPages, CurrentLetter = Model.CurrentLetter, UrlGeneratorFunctionAlpha = i => Url.Action("AlphaList", new { alpha = i }) })
and my implementation is
public static MvcHtmlString AlphaLinks(this HtmlHelper helper, PagingModel model)
{
string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
var stringBuilder = new StringBuilder("<ul class='pager'>");
foreach (string letter in letters)
{
stringBuilder.Append(String.Format("<li {2}><a href='{1}'>{0}<a></li>", letter, model.UrlGeneratorFunctionAlpha(letter), letter == model.CurrentLetter ? "class=Selected" : String.Empty));
}
stringBuilder.Append("</ul>");
return MvcHtmlString.Create(stringBuilder.ToString());
}
My controller code for the actionresult is:
public ActionResult AlphaList(string alpha = "A")
{
var logic = new ContactBUS();
var pageSize = 10;
var usernames = from c in XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Contacts.xml")).Elements("Contact")
select new
{
Username = (string)c.Element("Username"),
Lastname = (string)c.Element("LastName"),
Firstname = (string)c.Element("FirstName"),
Email = (string)c.Element("Email"),
};
var model = new AlphaListContactViewModel
{
Contacts = logic.GetContacts().Skip(0).Take(20).ToList(),
CurrentLetter = alpha,
MaxPages = (int)Math.Ceiling(logic.GetContactsCount() / (double)pageSize)
};
return View(model);
}
Any idea why this is happening?
Also, I need to make it so that when the user clicks on a letter, it shows the contacts' usernames for that letter.
Any help would be greatly appreciated.
Do you have any other routes that come before the one we see here? The first route that matches wins.
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.