winform tablelayoutpanel_idea checkout

winform tablelayoutpanel_idea checkoutWebGridHelperwithCheckAllCheckboxesTuesday,September13,2011ASP.NETASP.NETMVCHtmlHelperjQueryWebMatrixIntroduction:WebGridhelperisoneofthehelperofASP.NETWebPages(We…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

WebGrid Helper with Check All Checkboxes

Tuesday, September 13, 2011ASP.NET ASP.NET MVC Html Helper jQuery WebMatrix

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<WebGridColumn> columns = null,
	IEnumerable<string> 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("<input class=\"singleCheckBox\" name=\"selectedRows\" value=\""
                    + item.Value.GetType().GetProperty(checkBoxValue).GetValue(item.Value, null).ToString()
                    + "\" type=\"checkbox\" />"
                    );
                }));

                var newColumns = columns.ToList();
                newColumns.Insert(0, newColumn);

                var script = @"<script>
                
                if (typeof jQuery == 'undefined')
                {
                    document.write(
                        unescape(
                        ""%3Cscript src='http://ajax.proxy.ustclug.org/ajax/libs/jquery/1.6.2/jquery.min.js'%3E%3C/script%3E""
                        )
                     );
                }

                (function(){

                    window.setTimeout(function() { initializeCheckBoxes();  }, 1000);
                    function initializeCheckBoxes(){    

                        $(function () {

                            $('#allCheckBox').live('click',function () {

                                var isChecked = $(this).attr('checked');                        
                                $('.singleCheckBox').attr('checked', isChecked  ? true: false);
                                $('.singleCheckBox').closest('tr').addClass(isChecked  ? 'selected-row': 'not-selected-row');
                                $('.singleCheckBox').closest('tr').removeClass(isChecked  ? 'not-selected-row': 'selected-row');

                            });

                            $('.singleCheckBox').live('click',function () {

                                var isChecked = $(this).attr('checked');
                                $(this).closest('tr').addClass(isChecked  ? 'selected-row': 'not-selected-row');
                                $(this).closest('tr').removeClass(isChecked  ? 'not-selected-row': 'selected-row');
                                if(isChecked && $('.singleCheckBox').length == $('.selected-row').length)
                                     $('#allCheckBox').attr('checked',true);
                                else
                                    $('#allCheckBox').attr('checked',false);

                            });

                        });
                    }

                })();
            </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("{}",
                                            "<input type='checkbox' id='allCheckBox'/>") + 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<Person> GetPersons();
        }

PersonServiceImp.cs

        public class PersonServiceImp : IPersonService
        {
            public IList<Person> GetPersons()
            {
                return _persons;
            }

            static IList<Person> _persons = new List<Person>();

            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<WebGridHelperCheckAllCheckboxes.Models.Person>
        @{
            ViewBag.Title = "Index";
            Layout = "~/Views/Shared/_Layout.cshtml";
            var grid = new WebGrid(source: Model);
        }
        <h2>Index</h2>

        <style>
        .selected-row{
            background: none repeat scroll 0 0 #CACAFF;
            color: #222222;
        }
        .not-selected-row{
            background: none repeat scroll 0 0 #FFFFFF;
            color: #000000;
        }
        .grid
        {
            border-collapse: collapse;
        }
        .grid th,td
        {
            padding : 10px;
            border: 1px solid #000;
        }
        </style>

        @using (Html.BeginForm())
        {
            <fieldset>
                <legend>Person</legend>
                @grid.GetHtmlWithSelectAllCheckBox(
                    tableStyle: "grid", checkBoxValue: "ID",
                    columns: grid.Columns(
                        grid.Column(columnName: "Name"),
                        grid.Column(columnName: "Email"),
                        grid.Column(columnName: "Adress")
                ))
                <p>
                    <input type="submit" value="Save" />
                </p>
            </fieldset>
        }

          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/3960942.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185585.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月5日 下午6:46
下一篇 2022年10月5日 下午6:46


相关推荐

  • 使用LabVIEW编写简单串口采集程序

    使用LabVIEW编写简单串口采集程序使用 LabVIEW 编写简单串口采集程序 1LabVIEW 版本使用版本为 2018 版 使用硬件接口需安装 VISA 驱动 2 需要实现的功能设计程序 将串口接收的指定帧头帧尾的数据接收 并提取有效数据位 将数据转为电压值实时显示 并可保存为文本文件 数据帧格式 3 界面设计左边是一个接受开关 以及串口的一些设置 左下方是保存路径选择及保存按钮 中间上方实时显示单次接收的数据 为一维数组 下方是累计接收的数据 为二维数组 界面右侧实时显示数据计算出的电压值 4 程序设计 4 1

    2026年3月20日
    1
  • mysql decimal 空,MySQL DECIMAL数据类型

    mysql decimal 空,MySQL DECIMAL数据类型同事问MySQL数据类型DECIMAL(N,M)中N和M分别表示什么含义,M不用说,显然是小数点后的小数位数,但这个N究竟是小数点之前的最大位数,还是加上小数部分后的最大位数?这个还真记不清了。于是乎,创建测试表验证了一番,结果如下:测试表,seller_cost字段定义为decimal(14,2)CREATETABLE`test_decimal`(`id`int(11)NOTNULL,`sell…

    2022年7月17日
    22
  • Openclaw-原版部署(Mac)

    Openclaw-原版部署(Mac)

    2026年3月12日
    2
  • TensorFlow实现遗传算法_unity opencv

    TensorFlow实现遗传算法_unity opencv一、VGGNet简介VGGNet是牛津大学计算机视觉组和GoogleDeepMind公司的研究员一起研发的深度卷积神经网络。VGGNet探索了卷积神经网络的深度与其性能之间的关系,通过反复堆叠的小型卷积核和的最大池化层,VGGNet成功地构造了16~19层深的卷积神经网络。VGGNet的错误率大幅下降,取得了ILSVRC2014比赛分类项目的第2名和定位项目的第1名。同时,VGGNet的拓…

    2022年8月30日
    5
  • 计算机组成原理核心知识点总结&面试笔试要点[通俗易懂]

    作为一名计算机专业的学生,计算机组成原理、计算机网络、操作系统这三门课程可以说是专业核心基础课,是至关重要的,其内容是一名合格的coder所必备的知识集;非科班出身的程序员要是想要有所提升,也需要认真学习这三门课程,可以快速形成计算机知识的结构体系,理解计算机底层原理,在工作实践中可以借鉴优秀的设计;而且很多互联网公司在笔试和面试中都会涉及到这三门课程的知识点,因此我通过视频学习对这三门课程就行…

    2022年4月12日
    69
  • 我的程序里

    我的程序里我的程序里没有一点点防备也没有一丝顾虑突然错误出现在我的日志里带给我惊喜身不自已可是你偏又这样在我不知不觉中悄悄的消失从我的堆栈里没有音讯剩下了报警短信你存在我某一个模块里我的梦里,我的心里,我的

    2022年7月3日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号