最近我遇到了一个问题。但问题不会影响我的流程,但会使GUI看起来很好,如果解决了。
问题是......我有一个搜索屏幕,根据搜索条件筛选一些记录,这些记录显示在定义了一些ItemTemplate的网格视图中。我的问题是网格的长度是根据网格中的记录数进行调整的。我需要有一个恒定的网格高度,以便我的页面长度对所有搜索保持不变。只有当用户希望每页显示10条以上的记录时,才应增加此高度。
请帮我解决这个问题。
答案 0 :(得分:1)
因为你的要求是:你有一个按钮说“显示更多”,点击时会显示下一行10行。
一种方法是使用List
objects
,然后调用List<T>.GetRange()
方法。您也可以使用Take(n)
中的LINQ
扩展名仅返回所需数量的记录,以防您已使用此版本。我有CountToDisplay
作为变量来保存要显示的当前记录数,最初设置为0(零)
GetEmployees
方法
protected List<Employee> GetEmployees(int CountToDisplay)
{
List<Employee> employees= new List<employee>();
// Sample code to fill the List. Use the `Take` Extension
dbDataContext db = new dbDataContext();
var e= ( from c in db.Employees
select c ).Take(CountToDisplay);
//iterate through results and add to List<Employee>
foreach(var c in e)
{
employee emp = new employee { name = c.name, address = c.address };
employees.Add(emp);
}
return employees;
}
此处Employee
是一个类:
public class Employee
{
public string name;
public string address;
}
现在是有趣的部分。假设您有一个“显示更多”按钮,单击时会显示下一行10行。这种情况一直持续到你结束。所以在我的情况下,我使用了一个链接按钮,并在单击时使用服务器方法来加载和刷新网格。
<asp:LinkButton ID="btnShowMore" class="ShowMoreLink" runat="server"
OnClick="ShowMoreResults"></asp:LinkButton>
这是ShowMoreResults
函数:
private void ShowMoreResults()
{
// Keep incrementing with the Minimum rows to be displayed, 5, 10 ...
CountToDisplay = CountToDisplay +
Convert.ToInt16(WebConfigurationManager.AppSettings["EmpGridViewMinCount"]);
// finally called the Grid refresh method
RefreshGrid();
}
网格刷新方法:
private void RefreshGrid()
{
List<Employee> employees = GetEmployees(CountToDisplay)
if (employees != null)
{
empGrid.DataSource = employees;
}
// **Hide the ShowMore link in case Count to display exceeds the total record.**
btnShowMore.Visible = employees.Count > CountToDisplay;
// Finally bind the GridView
empGrid.DataBind();
}