对GridView的行加颜色并弹出Kindeditor的实现思路

前台代码:


代码如下:

<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
function tureDelete() {
if (confirm('真的要删除吗?') == false)
{ return false;}
}
function showdiv()
{
document.getElementByIdx_x("show1").style.display = "block";
document.getElementByIdx_x("showDiv").style.display = "block";
}
</script>
<link rel="stylesheet" href="Kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="Kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="Kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="Kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="Kindeditor/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function (K) {
var editor1 = K.create('#content1', {
cssPath: 'Kindeditor/plugins/code/prettify.css',
uploadJson: 'Kindeditor/asp.net/upload_json.ashx',
fileManagerJson: 'Kindeditor/asp.net/file_manager_json.ashx',
allowFileManager: true,
afterCreate: function () {
var self = this;
K.ctrl(document, 13, function () {
self.sync();
K('form[name=example]')[0].submit();
});
K.ctrl(self.edit.doc, 13, function () {
self.sync();
K('form[name=example]')[0].submit();
});
}
});
prettyPrint();
});
</script>
<style type="text/css">
#showDiv {
position:absolute;
top:50%;
left:50%;
margin-left:-350px;
margin-top:-250px;
}
</style>
</head>
<body>
<asp:Label ID="Label1" runat="server" Visible="false" Text=""></asp:Label>
<form id="form1" runat="server">
<div id="show1" style="display:none;background-color: Black;position:absolute; width:100%;height:100%;top:0px;left:0px; opacity:0.3;"></div>
<div id="showDiv" style="display:none;background-color:bisque;">
<table>
<tr>
<td>
标题
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
内容
</td>
<td>
<textarea id="content1" cols="100" rows="8" style="width:700px;height:500px;visibility:hidden;" runat="server"></textarea>
</td>
</tr>
<tr>
<td>
类别
</td>
<td>
<asp:DropDownList ID="ddlClassName" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
用户
</td>
<td>
<asp:DropDownList ID="ddlUser" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnUpdate" runat="server" Text="保存" />
</td>
</tr>
</table>
</div>
<div >
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="ck1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="标题">
<ItemTemplate>
<a href='WebForm1.aspx?id=<%#eval_r("Id") %>'><%#eval_r("NewsTitle") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NewsContent" HeaderText="内容" />
<asp:BoundField DataField="RealName" HeaderText="创建者" SortExpression="RealName" />
<asp:BoundField DataField="CreateTime" DataFormatString="{0:yyyy-mm-dd hh:mm:ss}" HeaderText="创建时间" SortExpression="CreateTime" />
<asp:BoundField DataField="ClassName" HeaderText="类型" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="linkbtnEdit" CommandArgument='<%# eval_r("Id") %>' runat="server">编辑</asp:LinkButton>
<asp:LinkButton ID="linkDelete" CommandArgument='<%# eval_r("Id") %>' runat="server">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="lbtnProc" runat="server" OnClick="lbtnProc_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">最后一页</asp:LinkButton>
    
<asp:Button ID="Button1" runat="server" Text="删除" OnClick="Button1_Click" OnClientClick="return tureDelete()" />
<asp:Button ID="Button2" runat="server" Text="弹出层" OnClick="Button2_Click1" />
</td>
</tr>
</table>
</div>
</form>
</body>

后台代码:


代码如下:

public partial class GridView : System.Web.UI.Page
{
string constr = "data source=.;initial catalog=News;user id=sa;password=111111;";
string sql = "select T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20) as NewsContent,T2.RealName,T1.CreateTime,T3.ClassName from (select ROW_NUMBER() over (order by Id) as rownumber,* from T_News) T1 left join T_User T2 on T1.NewsCreator=T2.UserId left join T_NewsClass T3 on T1.ClassId=T3.ClassId where rownumber>(@pageIndex-1)*@pageSize and rownumber<=@pageIndex*@pageSize";
int count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageIndex"] = 1;
DataPage(sql);
}
}
private void DataPage(string sql)
{
SqlConnection conn = new SqlConnection(constr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
// string sql = "select T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20) as NewsContent,T2.RealName ,T1.CreateTime,T3.ClassName from T_News1 T1 join T_User T2 on T1.NewsCreator=T2.UserId join T_NewsClass T3 on T1.ClassId=T3.ClassId";
cmd.Parameters.AddWithValue("@pageSize", 10);
cmd.Parameters.AddWithValue("@pageIndex", Convert.ToInt32(ViewState["pageIndex"]));
cmd.CommandText = sql;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
string sql1 = "select count(*) from T_News";
cmd.CommandText = sql1;
int i =Convert.ToInt32(cmd.ExecuteScalar());
if (i % 10 == 0)
{
ViewState["pageCount"] = i / 10;
}
else
{ ViewState["pageCount"] = i / 10+1; }
conn.Close();
conn.Dispose();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
ViewState["pageIndex"] = 1;
DataPage(sql);
}
protected void lbtnProc_Click(object sender, EventArgs e)
{
int i=Convert.ToInt32(ViewState["pageIndex"]) ;
if (i>1)
{
i--;
ViewState["pageIndex"] = i;
DataPage(sql);
}
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(ViewState["pageIndex"]);
if (i <Convert.ToInt32(ViewState["pageCount"]))
{
i++;
ViewState["pageIndex"] = i;
DataPage(sql);
}
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
ViewState["pageIndex"] = ViewState["pageCount"];
DataPage(sql);
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["sortExp"] == null)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add(e.SortExpression, "ASC");
ViewState["sortExp"] = dic;
sql += " Order by " + e.SortExpression + " " + dic[e.SortExpression];
DataPage(sql);
}
else
{
//判断用户本次点击的排序字段是否和上次点击的排序字段一致,如果一致的话,那么就更改此字段的排序规则,如果不是就清除上次的排序字段,添加新的排序字段和规则(这是根据一个字段排序的情况)
Dictionary<string, string> dic = ViewState["sortExp"] as Dictionary<string, string>;
if (dic.ContainsKey(e.SortExpression))
{
if (dic[e.SortExpression] == "ASC")
{
dic[e.SortExpression] = "DESC";
}
else
{
dic[e.SortExpression] = "ASC";
}
}
else//如果不包含的话就生新创建一个
{
//dic.Clear();
dic.Add(e.SortExpression, "ASC");
}
sql +=" Order by "+ e.SortExpression + " " + dic[e.SortExpression];
DataPage(sql);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Controls.Count > 0)
{
LinkButton link = e.Row.Cells[i].Controls[0] as LinkButton;
string sortexp = link.CommandArgument;
if (ViewState["sortExp"] != null)
{
Dictionary<string, string> dic = ViewState["sortExp"] as Dictionary<string, string>;
if (dic.ContainsKey(sortexp))
{
Literal li = new Literal();
if (dic[sortexp] == "ASC")
{
li.Text = "↑";
}
else
{
li.Text = "↓";
}
e.Row.Cells[i].Controls.Add(li);
}
}
}
}
}
//按条件给gridview的行加背景颜色
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text == "肖唯哲")
{
e.Row.BackColor = Color.Red;
//根据条件统计当前页的记录数
count++;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells.RemoveAt(6);
e.Row.Cells.RemoveAt(5);
e.Row.Cells.RemoveAt(4);
e.Row.Cells.RemoveAt(3);
e.Row.Cells.RemoveAt(2);
e.Row.Cells.RemoveAt(1);
e.Row.Cells[0].ColumnSpan = 8;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[0].Text = string.Format("肖唯哲:{0}", count);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlid = string.Empty;
foreach (GridViewRow row in this.GridView1.Rows)
{
CheckBox ck1=row.Cells[0].FindControl("ck1") as CheckBox;
if (ck1.Checked == true)
{
LinkButton link=row.Cells[6].FindControl("linkbtnEdit") as LinkButton;
sqlid +=" "+link.CommandArgument + " ,";
}
}
string sql1 = "delete from T_News where Id in (" + sqlid.TrimEnd(',')+")";
int i= DeleteDatas(sql1);
if (i > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('删除成功!')", true);
DataPage(sql);
}
}
private int DeleteDatas(string sql1)
{
SqlConnection conn = new SqlConnection(constr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql1;
int num = Convert.ToInt32(cmd.ExecuteNonQuery());
return num;
conn.Dispose();
}
protected void Button2_Click1(object sender, EventArgs e)
{
//Response.Write("<script type='text/javascript'>showdiv()</script>");
ClientScript.RegisterStartupScript(this.GetType(), "key", "showdiv()", true);
}
}

(0)

相关推荐

  • kindeditor编辑器点中图片滚动条往上顶的bug

    比如现在我插入两张图片, 无论我点击哪张图片,里边的滚动条都会往上顶. 本来以为往上会有解决方法,一查结果没有:然后想着去官网查查,然而什么都没有,想到官网提交这个bug,结果没地方提交. 怎么解决,如果单是解决这个bug,去研究源码,我觉的是吃力不讨好的. 然后我就直接从 click.mousedown 这两个事件找起,找它们获得高度的地方,然后 感觉是的话就console.log下,看是不是.然后就找到了. 在这个函数里 pos : function() { var self = this,

  • dotnet封装的kindeditor编辑器控件

    KindEditor很不错,刚接触不久,非常喜欢.KindEditor网站有ForPHP等扩展的,没有ForNet的.我是搞.net开发的,就用它简单封装了一个控件,拖过来即可使用,使用更加简单.源码提供给大家,有兴趣的朋友可以进一步完善. 1.第一次使用,需要配置一下web.config. 复制代码 代码如下: <configSections><section name="KindEditor" type="KindEditorForDotNet.Conf

  • 使用JavaScript为Kindeditor自定义按钮增加Audio标签

    流程比较简单,主要有以下步骤: 注册插件(按钮.Lang.htmlTags.插件脚本) 基于media插件代码修改 注册插件 首先,全局配置kindeditor参数: KindEditor.lang({ audio : 'MP3' }); KindEditor.options.htmlTags['audio'] = ['src','controls','autoplay','type']; KindEditor.options.htmlTags['source'] = ['src','contr

  • jQuery读取和设定KindEditor值的方法

    在使用Kindeditor的时候,想要利用Ajax传值,但是通过editor封装的方法是行不通的,原因在于编辑器我们是放在另一个jsp页面,通过iframe来加载的,同时这个iframe的display="none"的,要通过一个事件来触发. 复制代码 代码如下: <iframe src="../common/editor.jsp" frameborder="0" scrolling="no" style="m

  • ASP.NET网站使用Kindeditor富文本编辑器配置步骤

    1. 下载编辑器 下载 KindEditor 最新版本,下载页面: http://www.kindsoft.net/down.php 2. 部署编辑器 解压 kindeditor-x.x.x.zip 文件,将editor文件夹复制到web目录下  3.在网页中加入(ValidateRequest="false") 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" Validat

  • jQuery编辑器KindEditor4.1.4代码高亮显示设置教程

    编辑器KindEditor官网: http://www.kindsoft.net/ 1.需要加载的JS和CSS文件为: 复制代码 代码如下: <script src="kindeditor-4.1.4/kindeditor.js" type="text/javascript" charset="utf-8"></script> <script src="kindeditor-4.1.4/plugins/co

  • kindeditor修复会替换script内容的问题

    kindeditor一些个人修改 1.替换script里面的内容的问题 2.颜色选择器扩展,复制的fck编辑器选颜色 3.swfupload.swf上传前图片预览功能 kindeditor.js function _formatHtml(html, htmlTags, urlType, wellFormatted, indentChar) { if (html == null) { html = ''; } //2015-03-25 html = html.replace(/textarea__

  • Angularjs编写KindEditor,UEidtor,jQuery指令

    目前angularJS非常火热,本人也在项目中逐渐使用该技术,在angularJS中,指令可以说是当中非常重要的一部分,这里分享一些自己编写的指令: 注:本人项目中用了oclazyload进行部分JS文件加载 1.KindEditor 复制代码 代码如下: angular.module('AdminApp').directive('uiKindeditor', ['uiLoad', function (uiLoad) {     return {         restrict: 'EA',

  • KindEditor图片上传的Asp.net代码实例

    复制代码 代码如下: using System;using System.Globalization;using System.Collections;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;u

  • nodejs 整合kindEditor实现图片上传

    kindEditor官网上中提供了ASP,ASP.NET,JSP相关的整合应用,http://kindeditor.net/docs/upload.html可以参照实现nodejs的整合,发现实用nodejs更简单 环境: unbuntu 14.10 nodejs 0.10.35 express 4.11.2 formidable 1.0.16 kindEditor 4.1.10 webStorm 8 1.通过IDE或终端创建一个名称为test的工程 2.编辑package.json添加form

随机推荐