对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);
}
}

时间: 2024-11-08 18:52:55

对GridView的行加颜色并弹出Kindeditor的实现思路_实用技巧的相关文章

asp.net 弹出警告窗口实现代码_实用技巧

复制代码 代码如下: /// <summary> /// 提供一个可选"是""否"的弹出窗口,若选是,则跳到url1,选否则跳到url2 /// </summary> /// <param name="strMsg"></param> /// <param name="url1">选"是"</param> /// <param n

aspxgridview CustomButtonCallback 不支持弹出消息提示解决方法_实用技巧

aspxgridveiw是devexpress的一个grid控件,使用起来还不错.但是今天遇到一个问题,就是不能再 CustomButtonCallback 事件中使用response.write,因为CustomButtonCallback 事件是无刷新的,所以不支持,但是即使使用ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "MyScript", myScript, true)也无济于事,在网上查了很久,

在ASP.NET中实现弹出日历的具体方法_实用技巧

ctlCalendar.ascx的源代码: 复制代码 代码如下: <%@ Control Language="c#" AutoEventWireup="false" Codebehind="ctlCalendar.ascx.cs" Inherits="calendar.ctlCalendar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5

gridview行索引获取方法及实现代码(非js版)_实用技巧

前一版本http://www.jb51.net/article/33251.htm是用Javascript获取GridView的行索引. 此篇Insus.NET使用非Javascript获取GridView的行索引. 数据还是使用前一篇的数据来更改. 首先为GridView的控件写OnRowCreated ="GridViewCosmetic_RowCreated" 复制代码 代码如下: Protected Sub GridViewCosmetic_RowCreated(sender

asp.net静态方法弹出对话框实现思路_实用技巧

还是不说废话,直接上代码.为菜鸟所准备--其实就是弹出JavaScript小窗口,总得来说就是定义的一个DIV 复制代码 代码如下: /// <summary> /// 弹出JavaScript小窗口,其实就是定义的一个DIV /// </summary> /// <param name="js">窗口信息</param> public static void Alert(string message) { message = "

gridview实现服务器端和客户端全选的两种方法分享_实用技巧

复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="

asp.net无法加载oci.dll等错误的解决方法_实用技巧

解决方法一 修复方法: 1:找到oracle客户端的安装目录,例如:E:/oracle/ora92,选中目录,在"属性--安全"里面删掉"ASP .NET.Users用户"然后再添加进去,并赋予"完全控制或者修改权限":将everyone赋予"完全控制或者修改权限",注意:网站的目录权限也要设置为ervryone完全访问,不然oracle好了,网站同样访问不了.重新启动.2:最简单直接的方法是启用ASP .Net用户模拟,在w

asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例_实用技巧

本文实例讲述了asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码.分享给大家供大家参考,具体如下: <%@ WebHandler Language="C#" Class="GetQRCode" %> using System; using System.Web; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtW

ASP.NET生成树形显示的GridView实现思路_实用技巧

目的:生成树形结构的表格数据(EasyUI也有TreeGrid,此处只是提供一个思路),可以扩展单击展开/收缩节点 图例: 类代码: 复制代码 代码如下: using System; using System.Data; /// <summary> ///GridViewHelper 的摘要说明 /// </summary> public class GridViewHelper { private string gridline; //连接线 private DataTable