博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebGrid Helper with Check All Checkboxes
阅读量:4589 次
发布时间:2019-06-09

本文共 6594 字,大约阅读时间需要 21 分钟。

 

Tuesday, September 13, 2011

Introduction:

          WebGrid helper is one of the helper of ASP.NET Web Pages(WebMatrix) technology included in ASP.NET MVC 3. This helper is very easy to use and makes it very simple to display tabular data in your web page. In addition to displaying tabular data, it also supports formatting, paging and sorting features. But WebGrid helper does not allow you to put raw html(like checkbox) in the header. In this article, I will show you how you can put html element(s) inside the WebGrid helper's header using a simple trick. I will also show you how you can add the select or unselect all checkboxes feature in your web page using jQuery and WebGrid helper.  

Description:

          To make it easy to add this feature in any of your web page, I will create an extension method for the WebGrid class. Here is the extension method, 

public static IHtmlString GetHtmlWithSelectAllCheckBox(this WebGrid webGrid, string tableStyle = null,string headerStyle = null, string footerStyle = null, string rowStyle = null,	string alternatingRowStyle = null, string selectedRowStyle = null,	string caption = null, bool displayHeader = true, bool fillEmptyRows = false,	string emptyRowCellValue = null, IEnumerable
columns = null, IEnumerable
exclusions = null, WebGridPagerModes mode = WebGridPagerModes.All, string firstText = null, string previousText = null, string nextText = null, string lastText = null, int numericLinksCount = 5, object htmlAttributes = null, string checkBoxValue = "ID") { var newColumn = webGrid.Column(header: "{}", format: item => new HelperResult(writer => { writer.Write("
" ); })); var newColumns = columns.ToList(); newColumns.Insert(0, newColumn); var script = @"
"; var html = webGrid.GetHtml(tableStyle, headerStyle, footerStyle, rowStyle, alternatingRowStyle, selectedRowStyle, caption, displayHeader, fillEmptyRows, emptyRowCellValue, newColumns, exclusions, mode, firstText, previousText, nextText, lastText, numericLinksCount, htmlAttributes ); return MvcHtmlString.Create(html.ToString().Replace("{}", "
") + script); }

          This extension method accepts the same arguments as the WebGrid.GetHtml method except that it takes an additionalcheckBoxValue parameter. This additional parameter is used to set the values of checkboxes. First of all, this method simply insert an additional column(at position 0) into the existing WebGrid. The header of this column is set to {}, because WebGrid helper always encode the header text. At the end of this method, this text is replaced with a checkbox element.  

          In addition to emitting tabular data, this extension method also emit some javascript in order to make the select or unselect all checkboxes feature work. This method will add a css class selected-row for rows which are selected and not-selected-row css class for rows which are not selected. You can use these CSS classes to style the selected and unselected rows.

          You can use this extension method in ASP.NET MVC, ASP.NET Web Form and ASP.NET Web Pages(Web Matrix), but here I will only show you how you can leverage this in an ASP.NET MVC 3 application. Here is what you might need to set up a simple web page,

Person.cs

public class Person        {            public int ID { get; set; }            public string Name { get; set; }            public string Email { get; set; }            public string Adress { get; set; }        }

IPersonService.cs

public interface IPersonService        {            IList
GetPersons(); }

PersonServiceImp.cs

public class PersonServiceImp : IPersonService        {            public IList
GetPersons() { return _persons; } static IList
_persons = new List
(); static PersonServiceImp() { for (int i = 5000; i < 5020; i++) _persons.Add(new Person { ID = i, Name = "Person" + i, Adress = "Street, " + i, Email = "a" + i + "@a.com" }); } }

HomeController.cs

public class HomeController : Controller        {            private IPersonService _service;            public HomeController()                : this(new PersonServiceImp())            {            }            public HomeController(IPersonService service)            {                _service = service;            }            public ActionResult Index()            {                return View(_service.GetPersons());            }            [HttpPost]            public ActionResult Index(int[] selectedRows)            {                return View(_service.GetPersons());            }        }

Index.cshtml

@model IEnumerable
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; var grid = new WebGrid(source: Model); }

Index

@using (Html.BeginForm()) {
Person @grid.GetHtmlWithSelectAllCheckBox( tableStyle: "grid", checkBoxValue: "ID", columns: grid.Columns( grid.Column(columnName: "Name"), grid.Column(columnName: "Email"), grid.Column(columnName: "Adress") ))

}

          Now just run this application. You will find the following screen,  

WebGridCheWebGridCheckAllCheckBox.png

          Select all or some rows of your table and submit them. You can get all the selected rows of your table as , 

WebGridCheWebGridCheckAllSelectedRows.png

Summary:

          In ASP.NET MVC 3, you can utilize some helpers of ASP.NET Web Pages(WebMatrix) technology, which can be used for common functionalities. WebGrid helper is one of them. In this article, I showed you how you can add the select or unselect all checkboxes feature in ASP.NET MVC 3 application using WebGrid helper and jQuery. I also showed you how you can add raw html in WebGrid helper's header. Hopefully you will enjoy this article too. A sample application is attached.  

转载于:https://www.cnblogs.com/dufu/p/3971837.html

你可能感兴趣的文章
Linux内核移植的若干问题
查看>>
linux程序对比
查看>>
linux数码管驱动程序和应用程序
查看>>
linux在菜单中添加SEG选项
查看>>
linux物理地址和虚拟地址
查看>>
linux驱动程序与菜单关联
查看>>
linux在配置菜单中添加选项
查看>>
linux修改文件系统注册设备
查看>>
linux添加地址映射
查看>>
c#程序集
查看>>
Microsoft 中间语言
查看>>
.NET Framework 简介
查看>>
c#通用语言运行时CLR
查看>>
编译和执行 C# 应用程序
查看>>
c#命名空间
查看>>
c#字面量
查看>>
C# 应用程序文件夹结构
查看>>
c# Format() 方法
查看>>
c#实例
查看>>
c# Format() 方法
查看>>