对GridView的行加颜色并弹出Kindeditor的实现思路
程序员文章站
2024-03-03 23:37:04
前台代码: 复制代码 代码如下:
前台代码:
<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);
}
}
复制代码 代码如下:
<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);
}
}