时间:2016-02-16 00:52 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【对GridView的行加颜色并弹出Kindeditor的实现思路】,下面是详细的讲解!
对GridView的行加颜色并弹出Kindeditor的实现思路
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);
}
}
关于对GridView的行加颜色并弹出Kindeditor的实现思路的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
