Html辅助方法(分页、下拉框)
引用命名空间:
using System.Text;
using System.Web.Mvc;
Html分页方法
#region 分页Html辅助方法
/// <summary>
/// 分页Html辅助方法
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="currentPage"></param>
/// <param name="pageSize"></param>
/// <param name="totalCount"></param>
/// <param name="parameterString"></param>
/// <returns></returns>
public static HtmlString PageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount, string parameterString)
{
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
pageSize = pageSize == ? : pageSize;
var totalPages = Math.Max((totalCount + pageSize - ) / pageSize, );//总页数
var output = new StringBuilder();
output.Append("<nav>");
output.Append("<ul class='pagination'>");
string pageSizrWithParameter = string.Empty;
if (!string.IsNullOrEmpty(parameterString))
pageSizrWithParameter = pageSize + "&" + parameterString;
if (totalPages>)
{
output.AppendFormat("<li><a href='{0}?pageIndex=1&pageSize={1}' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>",redirectTo,pageSizrWithParameter);
if (currentPage > )//处理上一页连接
output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>上一页</a></li>",redirectTo,currentPage-,pageSizrWithParameter); output.Append("");
int currint = ;
for (int i = ; i < ; i++)
{//一共最多显示10个页码,前面五个后面五个
if ((currentPage+i-currint)>= && (currentPage+-currint)<=totalPages)
{
if (currint == i)//当前页处理
output.AppendFormat("<li class='active'><a href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></li>", redirectTo, currentPage, pageSizrWithParameter, currentPage);
else//一般页处理
output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></li>",redirectTo,currentPage+i-currint,pageSizrWithParameter,currentPage+i-currint);
}
output.Append("");
}
if (currentPage < totalPages)//处理下一页连接
output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>下一页</a></li>", redirectTo, currentPage + , pageSizrWithParameter);
output.Append(""); if (currentPage != totalPages)
output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'><span aria-hidden='true'>»</span></a></li>", redirectTo, totalPages, pageSizrWithParameter);
output.Append("");
}
output.Append("</ul>");
output.Append("</nav>"); return new HtmlString(output.ToString());
}
#endregion
控制器方法(搜索的关键字在Js中拼接出来,然后用window.location="路径?参数="+。。+"&参数="+。。。+"。。。。")
[HttpGet]
public ActionResult Moments(int pageIndex=,int pageSize=)
{
int totalRecord=;
List<实体类> list=得到集合方法(pageIndex,pageSize,out totalRecord);
ViewData["totalRecord"]=totalRecord;
ViewData["pageIndex"]=pageIndex;
ViewData["pageSize"]=pageSize; #region 生成搜索状态保存数据
StringBuilder sb=new StringBuilder();
foreach(string item in Request.QueryString.AllKeys)
{
if(!item.Equals("pageIndex") && !item.Equals("pageSize"))
sb.Append(item+"="+Request.QueryString[item]+"&")
}
ViewData["parameter"]=sb.ToString().Trim('&');
#endregion
return View(lam);
}
引用分页
<!--在控制器里面存储的ViewData,totalRecord表示根据添加查询到的数据并返回的条数,parameter表示搜索条件(关键字搜索等等)-->
@Html.PageNavigate(int.Parse(ViewData["pageIndex"].ToString()),int.Parse(ViewData["pageSize"].ToString()),int.Parse(ViewData["totalRecord"].ToString()),ViewData["parameter"].ToString())
DropDownList:(还没有经过测试,只是展示一下思路,我也不清楚理解是否是正确的,求大神们指教一下)
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> list)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name!=null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list!=null)
{
foreach (var item in list)
{
option.MergeAttribute("value", item.Value);
option.InnerHtml = item.Text;
select.InnerHtml += option;
}
}
return new HtmlString(select.ToString());
//return ExtDropDownList(htmlHelper, select.ToString(), null);
}
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> list, IDictionary<string,object> htmlAttribute)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name != null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list != null)
{
foreach (var item in list)
{
option.MergeAttribute("value", item.Value);
option.InnerHtml = item.Text;
select.InnerHtml += option;
}
}
select.MergeAttributes(htmlAttribute);
return new HtmlString(select.ToString());
}
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, object attribute)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name != null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list != null)
{
foreach (var item in list)
{
option.MergeAttribute("value", item.Value);
option.InnerHtml = item.Text;
select.InnerHtml += option;
}
}
return ExtDropDownList(htmlHelper, select.ToString(), null, HtmlHelper.AnonymousObjectToHtmlAttributes(attribute));
}
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name != null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list != null)
{
foreach (var item in list)
{
if (!String.IsNullOrEmpty(value))
{
if (item.Value == value)
option.MergeAttribute("value", value); }
else
option.MergeAttribute("value", item.Value); option.InnerHtml = item.Text; select.InnerHtml += option;
}
}
return new HtmlString(select.ToString());
}
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, IDictionary<string, object> htmlAttribute)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name != null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list != null)
{
foreach (var item in list)
{
if (!String.IsNullOrEmpty(value))
{
if (item.Value == value)
option.MergeAttribute("value", value); }
else
option.MergeAttribute("value", item.Value); option.InnerHtml = item.Text; select.InnerHtml += option;
}
}
select.MergeAttributes(htmlAttribute);
return new HtmlString(select.ToString());
}
public static HtmlHelper ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, object attribute, IDictionary<string, object> htmlAttribute)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name != null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list != null)
{
foreach (var item in list)
{
if (!String.IsNullOrEmpty(value))
{
if (item.Value == value)
option.MergeAttribute("value", value); }
else
option.MergeAttribute("value", item.Value); option.InnerHtml = item.Text; select.InnerHtml += option;
}
}
select.MergeAttributes(htmlAttribute);
return ExtDropDownList(htmlHelper, select.ToString(), null, null, HtmlHelper.AnonymousObjectToHtmlAttributes(attribute), null);
}
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, object attribute1, object attribute2)
{
TagBuilder select = new TagBuilder("select");
TagBuilder option = new TagBuilder("option");
if (!select.Attributes.ContainsKey("id") && name != null)
select.GenerateId(name);//添加Id属性 if (!String.IsNullOrEmpty(name))
select.MergeAttribute("name", name);//添加那么属性 //添加节点
option.MergeAttribute("value", "");
option.InnerHtml = "--请选择--";
select.InnerHtml += option;
if (list != null)
{
foreach (var item in list)
{
if (!String.IsNullOrEmpty(value))
{
if (item.Value == value)
option.MergeAttribute("value", value); }
else
option.MergeAttribute("value", item.Value); option.InnerHtml = item.Text; select.InnerHtml += option;
}
}
return ExtDropDownList(htmlHelper, select.ToString(), null, null, HtmlHelper.AnonymousObjectToHtmlAttributes(attribute1), HtmlHelper.AnonymousObjectToHtmlAttributes(attribute2));
}
Html辅助方法(分页、下拉框)的更多相关文章
- jquery 分页 下拉框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- select标签设置只读的方法(下拉框不可选但可传值)
1. <select id="s1" name="s1" onfocus="this.defaultIndex=this.selectedInd ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- jquery选中将select下拉框中一项后赋值给text文本框
jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...
- JavaScript向select下拉框中加入和删除元素
JavaScript向select下拉框中加入和删除元素 1.说明 a 利用append()方法向下拉框中加入元素 b 利用remove()方法移除下拉框中最后一个元素 2.设计源代码 < ...
- JavaScript向select下拉框中添加和删除元素
JavaScript向select下拉框中添加和删除元素 1.说明 a 利用append()方法向下拉框中添加元素 b 利用remove()方法移除下拉框中最后一个元素 2.设计源码 < ...
- android中自定义下拉框(转)
android自带的下拉框好用不?我觉得有时候好用,有时候难有,项目规定这样的效果,自带的控件实现不了,那么只有我们自己来老老实实滴写一个新的了,其实最基本的下拉框就像一些资料填写时,点击的时候出现在 ...
- 对于隐藏性质的非标准的动态 id 的下拉框,如何定位和选中
今天,在页面上碰到一个非 select 标签的下拉框,打算进行定位和模拟选中. <input aria-invalid="false" autocomplete=" ...
- 【selenium】基于python语言,如何用select选择下拉框
在项目测试中遇到了下拉框选择的控件,来总结下如何使用select选择下拉框: 下图是Select类的初始化描述,意思是,给定元素是得是select类型,不是就抛异常.接下来给了例子:要操作这个sele ...
- 简述Object(ActiveX)控件遮挡Dialog、select下拉框的解决办法
1.背景 最近在做项目的过程中,我们使用了Object控件,但是同时在上面写了一个select下拉框,因此每次点击下拉框的时候我们会发现,下拉框的部分内容被Object控件给遮挡了,调查研究后发现,我 ...
随机推荐
- Zabbix监控mysql配置及故障告警配置
本文主要介绍zabbix监控mysql的配置,包含使用zabbix自带模板监控mysql相关信息及自定义key监控mysql同步情况.同时介绍了触发器的创建及zabbix通过邮件方式告警配置. 一.配 ...
- C++学习29 重载[](下标运算符)
前面已经提到,下标操作符[]必须以类的成员函数的形式进行重载.在类中的声明格式如下: 返回值类型 & operator[] (参数) 或 const 返回值类型 & operator[ ...
- Ext.Form 自动填写表单内容
前台: 表单必须含有name属性 if (action == 'edit' || action == 'show') { MyForm1.getForm().load({ url: '/data/cu ...
- convert NameValueCollection/Dictionary<string, object> to JSON string
public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...
- python 如何找到某一目录下的文件类型(三种方法)
#!/usr/bin/env python import glob import os os.chdir(“./”) for file in glob.glob(“*.py”): print file ...
- opencv基于HSV的肤色分割
//函数功能:在HSV颜色空间对图像进行肤色模型分割 //输入:src-待处理的图像,imgout-输出图像 //返回值:返回一个iplimgae指针,指向处理后的结果 IplImage* SkinS ...
- HP Mobile Center 1.01 Related System Requirements
最近要开始使用HP Mobile Center,以下是我在官网上搜集的配置信息,包含软硬件. Reference: http://mobilecenterhelp.saas.hp.com/en/la ...
- Custom Date tag
Custom Date tag: custom date based on pattern format. Default date is current day. <CUSTOMDATE[+, ...
- Java基础——数据类型之间的转换
Java数据类型分为三大类,即布尔型.字符型和数值型.其中数值型又分为整型和浮点型.Java的基本数据类型(8种)为布尔型boolean(1字节):字符型char(2字节):整型byte(1字节).s ...
- dell 网络产品线
https://en.wikipedia.org/wiki/Dell_Networking_Operating_System http://topics-cdn.dell.com/pdf/ DNOS ...