lonelyandrew 2013-07-07 13:47 采纳率: 0%
浏览 1046

为什么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);
        }
    }
}

}

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-05 08:48
    关注

    在 Page_Load 事件函数中,如果不是第一次加载页面(即,不是 PostBack),则 SqlCommand 变量被重新赋值,所以在翻页的操作中 SqlCommand 的值已经为 null,因此 Bind() 函数中的 SqlCommand 参数就为 null。


    为了解决这个问题,你可以将 SqlCommand 定义为全局变量,或者将 SqlCommand 存储到 Session 中,在翻页操作中再从 Session 中取出 SqlCommand。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型