datalist结合PagedDataSource 类进行分页
2007-11-24 11:54
281 查看
public void sel_callname()
{
PagedDataSource ps = new PagedDataSource();//用来给数据控件显示分页的类不能继承
int curpage = Convert.ToInt32(this.lblCurrentIndex.Text.Trim());
//获取数据源
string sqlstr = "select articleid,title,classname,username,dateandtime From db_article";
com = new SqlCommand(sqlstr, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter();
da.SelectCommand = com;
ds = new DataSet();
da.Fill(ds);
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;//是否可以分业
ps.PageSize = 3;//每页显示数据的数量
ps.CurrentPageIndex = curpage - 1;//取得当前的页码
this.LinkButton2.Enabled = true;
this.Linkbutton3.Enabled = true;
this.Linkbutton4.Enabled = true;
this.lblCurrentIndex.Enabled = true;
//如果是当前第一页
if (curpage == 1)
{
this.btnFirst.Enabled = false;//不显示第一页按钮
this.LinkButton2.Enabled = false;//不显示上一页按钮
}
//如果是最后一页
if (curpage == ps.PageCount)
{
this.Linkbutton3.Enabled = false;//不显示下一页按钮
this.Linkbutton4.Enabled = false;//不显示最后一页按钮
}
this.lblPageCount.Text = Convert.ToString(ps.PageCount);//得到最后一页
for (int i = 1; i <= ps.PageCount; i++)//填充下拉列表值
{
DropDownList2.Items.Add("" + i);
}
try
{
GridView1.DataSource = ps;
GridView1.DataBind();
}
catch (SqlException ex)
{
myLabel.Text = "数据库错误!" + ex.Message;
}
finally
{
ds.Clone();
com.Clone();
con.Close();
}
}
//怎样获得给DataList中添加一个Bootn按钮进行删除的功能
//首先绑定datalist,str是传进来的任何有效的sql查询语句
protected void Data_x(string str)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
da = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "table");
DataList1.DataSource = ds;
DataList1.DataKeyField = "xid";//用来绑定主键这点很重要
DataList1.DataBind();
}
catch (SqlException ex)
{
throw (ex);
}
finally
{
ds.Clone();
con.Close();
}
}
//下面是通过DataList1的ItemCommand事件来获得添加到DataList中的按钮事件
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["dsn"]);
//DataList1.DataKeys[e.Item.ItemIndex].ToString();用来获得DataList1.DataKeyField = "xid";//用来绑定主键
string del_str = "delete from bbs_xinxi where xid=" + DataList1.DataKeys[e.Item.ItemIndex].ToString();
com = new SqlCommand(del_str, con);
try
{
con.Open();
if (com.ExecuteNonQuery() > 0)
{//下面是删除成功后从新绑定DataList
string _str = "select xid,xuname,xdate,xtext from bbs_xinxi where xjname='" + u_name +
"'and xyidu=1 order by xdate desc";
Data_x(_str);
}
else
{
Page.RegisterStartupScript("key","<script>此信息不能被删除!</script>");
}
}
catch (SqlException ex)
{
throw (ex);
}
finally
{
con.Close();
}
}
{
PagedDataSource ps = new PagedDataSource();//用来给数据控件显示分页的类不能继承
int curpage = Convert.ToInt32(this.lblCurrentIndex.Text.Trim());
//获取数据源
string sqlstr = "select articleid,title,classname,username,dateandtime From db_article";
com = new SqlCommand(sqlstr, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter();
da.SelectCommand = com;
ds = new DataSet();
da.Fill(ds);
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;//是否可以分业
ps.PageSize = 3;//每页显示数据的数量
ps.CurrentPageIndex = curpage - 1;//取得当前的页码
this.LinkButton2.Enabled = true;
this.Linkbutton3.Enabled = true;
this.Linkbutton4.Enabled = true;
this.lblCurrentIndex.Enabled = true;
//如果是当前第一页
if (curpage == 1)
{
this.btnFirst.Enabled = false;//不显示第一页按钮
this.LinkButton2.Enabled = false;//不显示上一页按钮
}
//如果是最后一页
if (curpage == ps.PageCount)
{
this.Linkbutton3.Enabled = false;//不显示下一页按钮
this.Linkbutton4.Enabled = false;//不显示最后一页按钮
}
this.lblPageCount.Text = Convert.ToString(ps.PageCount);//得到最后一页
for (int i = 1; i <= ps.PageCount; i++)//填充下拉列表值
{
DropDownList2.Items.Add("" + i);
}
try
{
GridView1.DataSource = ps;
GridView1.DataBind();
}
catch (SqlException ex)
{
myLabel.Text = "数据库错误!" + ex.Message;
}
finally
{
ds.Clone();
com.Clone();
con.Close();
}
}
//怎样获得给DataList中添加一个Bootn按钮进行删除的功能
//首先绑定datalist,str是传进来的任何有效的sql查询语句
protected void Data_x(string str)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
da = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "table");
DataList1.DataSource = ds;
DataList1.DataKeyField = "xid";//用来绑定主键这点很重要
DataList1.DataBind();
}
catch (SqlException ex)
{
throw (ex);
}
finally
{
ds.Clone();
con.Close();
}
}
//下面是通过DataList1的ItemCommand事件来获得添加到DataList中的按钮事件
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["dsn"]);
//DataList1.DataKeys[e.Item.ItemIndex].ToString();用来获得DataList1.DataKeyField = "xid";//用来绑定主键
string del_str = "delete from bbs_xinxi where xid=" + DataList1.DataKeys[e.Item.ItemIndex].ToString();
com = new SqlCommand(del_str, con);
try
{
con.Open();
if (com.ExecuteNonQuery() > 0)
{//下面是删除成功后从新绑定DataList
string _str = "select xid,xuname,xdate,xtext from bbs_xinxi where xjname='" + u_name +
"'and xyidu=1 order by xdate desc";
Data_x(_str);
}
else
{
Page.RegisterStartupScript("key","<script>此信息不能被删除!</script>");
}
}
catch (SqlException ex)
{
throw (ex);
}
finally
{
con.Close();
}
}
相关文章推荐
- DataList利用PagedDataSource来进行分页!
- DataList利用PagedDataSource来进行分页!_萧四郎
- Repeater 结合PagedDataSource 实现分页
- 利用PagedDataSource 对象进行分页
- 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页
- (转贴)使用PagedDataSource给Repeater、DataList增加分页
- 用PagedDataSource实现DataList和Repeater控件的分页
- 自己写的用DataList进行分页的sqlhelper文件
- ajax结合豆瓣搜索结果进行分页完整代码
- asp.net用datalist进行分页
- GridView与Datalist的结合使用and分页技术
- C#中DataList分页--使用PagedDataSource实现
- 结合smarty,获取$_GET传递过来的参数并进行重新索取和排列,以便传递给分页函数
- asp.net 结合mysql存储过程进行分页代码
- asp.net 结合mysql存储过程进行分页代码
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- asp 用 PagedDataSource 为 datalist 添加分页需注意
- Repeater利用PagedDataSource进行分页