问题描述
- 为什么Bind()执行完一回后,SqlCommand会变null?
-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication2
{
public partial class MailList : System.Web.UI.Page
{
string UserRole;
string SqlCommand;
string SearchContent;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected void Page_Load(object sender, EventArgs e)
{
LabelUserLimits.Text = "当前用户权限:" + UserRole;
if(!IsPostBack)
{
RadioButtonTitleSearch.Checked = true;
if (Session["UserRole"] == null)
{
UserRole = "游客";
SqlCommand = "select *from MailManagement where ReplyOrNot='是' or MailAuthor='管理员' order by MailId desc";
Bind(SqlCommand);
}
else if (Session["UserRole"].ToString() == "管理员" || Session["UserRole"].ToString() == "普通用户")
{
UserRole = "管理员";
SqlCommand = "select *from MailManagement order by MailId Desc";
Bind(SqlCommand);
}
}
}
int PageCount;//总页数
int CurrentPage = 1;//定义当前页
protected void Bind(string SqlCommand)
{
string SqlConnection = ConfigurationManager.AppSettings["conn"].ToString();
SqlDataAdapter DataAdapter = new SqlDataAdapter(SqlCommand, SqlConnection);
// DataSet Dataset = new DataSet();
DataTable Datatable = new DataTable();
DataAdapter.Fill(Datatable);
//DataList1.DataSource =Datatable;
// DataList1.DataBind();
//创建数据源
PagedDataSource Pagedatasourse = new PagedDataSource();
Pagedatasourse.DataSource = Datatable.DefaultView;
//允许分页
Pagedatasourse.AllowPaging = true;
//设置每页显示记录数
Pagedatasourse.PageSize = int.Parse("5");
//获取总页数
PageCount = Pagedatasourse.PageCount;
this.LabelAll.Text = PageCount.ToString();
Pagedatasourse.CurrentPageIndex = CurrentPage - 1;
//当前页
this.LabelCurrent.Text = Convert.ToString(CurrentPage);
//数据绑定
this.DataList1.DataSource = Pagedatasourse;
this.DataList1.DataBind();
if (CurrentPage == 1)
{
LinkButtonFirst.Enabled = false;
LinkButtonForm.Enabled = false;
}
if (CurrentPage == PageCount)
{
LinkButtonLast.Enabled = false;
LinkButtonNext.Enabled = false;
}
if (CurrentPage != 1)
{
LinkButtonFirst.Enabled = true;
LinkButtonForm.Enabled = true;
}
if (CurrentPage != PageCount)
{
LinkButtonLast.Enabled = true;
LinkButtonNext.Enabled = true;
}
}
//首页
protected void LinkButtonFirst_Click(object sender, EventArgs e)
{
if (this.LabelCurrent.Text == "1")
{ }
else
{
CurrentPage = 1;
Bind(SqlCommand);
}
}
//上一页
protected void LinkButtonForm_Click(object sender, EventArgs e)
{
if (this.LabelCurrent.Text != "1")
{
CurrentPage = int.Parse(this.LabelCurrent.Text) - 1;
this.LabelCurrent.Text = CurrentPage.ToString();
Bind(SqlCommand);
}
}
//下一页
protected void LinkButtonNext_Click(object sender, EventArgs e)
{
if (this.LabelAll.Text == this.LabelCurrent.Text)
{} else { CurrentPage = int.Parse(this.LabelCurrent.Text) + 1; this.LabelCurrent.Text = CurrentPage.ToString(); Bind(SqlCommand); } } //尾页 protected void LinkButtonLast_Click(object sender, EventArgs e) { if (this.LabelAll.Text != this.LabelCurrent.Text) { this.LabelCurrent.Text = this.LabelAll.Text; CurrentPage = int.Parse(this.LabelAll.Text); Bind(SqlCommand); } } //页面跳转 protected void ButtonGo_Click(object sender, EventArgs e) { if (TextBoxPageNumber.Text == CurrentPage.ToString()) { return; } if (!Class1.IsNumberic(TextBoxPageNumber.Text)) { Class1.alert("请输入数字!"); return; } if (int.Parse(TextBoxPageNumber.Text) > int.Parse(LabelAll.Text)) { Class1.alert("超出页码范围!"); return; } if (int.Parse(TextBoxPageNumber.Text) < 0) { Class1.alert("页码不可为负"); return; } else { CurrentPage = Convert.ToInt32(this.TextBoxPageNumber.Text); Bind(SqlCommand); } } protected void ButtonMail_Click(object sender, EventArgs e) { //Response.Redirect("UserMail.aspx"); } protected void ButtonSearch_Click(object sender, EventArgs e) { if (Session["UserRole"] == null) { UserRole = "游客"; } else if (Session["UserRole"].ToString() == "管理员") { UserRole = "管理员"; } else if (Session["UserRole"].ToString() == "普通用户") { UserRole = "普通用户"; } if (RadioButtonTitleSearch.Checked) { SearchContent = "%" + TextBoxSearch.Text + "%"; SqlCommand = "select *from MailManagement where MailTitle like '"+SearchContent+"'"; Bind(SqlCommand); } else if (RadioButtonAllSearch.Checked) { SearchContent = "%" + TextBoxSearch.Text + "%"; SqlCommand = "select *from MailManagement where MailTitle like '" + SearchContent + "'or MailContent like '" + SearchContent + "' or ReplyContent like '" + SearchContent + "'"; Bind(SqlCommand); } } }
}
时间: 2024-11-03 18:04:45