即使在asp.net中页面加载后如何保留全局字符串变量的值

发布于 2024-10-29 17:16:50 字数 1901 浏览 5 评论 0原文

我在保留我在独家新闻顶部定义的字符串变量时遇到问题,每次页面加载时字符串值都会变为空。下面是代码片段:

public partial class Caravan_For_Sale : System.Web.UI.Page
{
string check;
PagedDataSource pds = new PagedDataSource(); //paging

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        BindGrid();
    } 

}

private void BindGrid()
{
    DataTable dt = null;
    switch (check)
    {
        case "0-1500":
            break;
        case "1500-2000":
            dt = caravans.GetFilterbyPrice1();
            break;

        case "2000+":

            break;

        default:
             dt = caravans.GetAllCaravans();
            break;
    }
   // DataTable dt = caravans.GetAllCaravans();

    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 3;//add the page index when item exceeds 12 //Convert.ToInt16(ddlPageSize.SelectedValue);
    pds.CurrentPageIndex = CurrentPage;
    DataList1.RepeatColumns = 3; // 4 items per line
    DataList1.RepeatDirection = RepeatDirection.Horizontal;

    DataList1.DataSource = pds;
    DataList1.DataBind();

    lnkbtnNext.Enabled = !pds.IsLastPage;
    lnkbtnPrevious.Enabled = !pds.IsFirstPage;

    doPaging();

}
  protected void lnkPrice2_Click(object sender, EventArgs e)
{
    LinkButton _sender = (LinkButton)sender;
    check = _sender.CommandArgument;
 //  items["test"] = test;
    DataTable dt = caravans.GetFilterbyPrice2();
    if (dt.Rows.Count < 3)
    {
        lnkbtnNext.Enabled = false;
        lnkbtnPrevious.Enabled = false;

    }

    CurrentPage = 0;
    BindGrid();

}

   protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName.Equals("lnkbtnPaging"))
    {

        CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
        BindGrid();
    }
}

每次当 dlPaging_ItemCommand 变为活动状态(页面加载)时,字符串检查都会变为空。任何帮助或建议将不胜感激

I am having problems in retaining the string variable which I defined on the top of my scoop, everytime when page loads the string value becomes null. below is the snippet of the code:

public partial class Caravan_For_Sale : System.Web.UI.Page
{
string check;
PagedDataSource pds = new PagedDataSource(); //paging

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        BindGrid();
    } 

}

private void BindGrid()
{
    DataTable dt = null;
    switch (check)
    {
        case "0-1500":
            break;
        case "1500-2000":
            dt = caravans.GetFilterbyPrice1();
            break;

        case "2000+":

            break;

        default:
             dt = caravans.GetAllCaravans();
            break;
    }
   // DataTable dt = caravans.GetAllCaravans();

    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 3;//add the page index when item exceeds 12 //Convert.ToInt16(ddlPageSize.SelectedValue);
    pds.CurrentPageIndex = CurrentPage;
    DataList1.RepeatColumns = 3; // 4 items per line
    DataList1.RepeatDirection = RepeatDirection.Horizontal;

    DataList1.DataSource = pds;
    DataList1.DataBind();

    lnkbtnNext.Enabled = !pds.IsLastPage;
    lnkbtnPrevious.Enabled = !pds.IsFirstPage;

    doPaging();

}
  protected void lnkPrice2_Click(object sender, EventArgs e)
{
    LinkButton _sender = (LinkButton)sender;
    check = _sender.CommandArgument;
 //  items["test"] = test;
    DataTable dt = caravans.GetFilterbyPrice2();
    if (dt.Rows.Count < 3)
    {
        lnkbtnNext.Enabled = false;
        lnkbtnPrevious.Enabled = false;

    }

    CurrentPage = 0;
    BindGrid();

}

   protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName.Equals("lnkbtnPaging"))
    {

        CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
        BindGrid();
    }
}

The string check becomes null everytime when the dlPaging_ItemCommand becomes active(page loads). Any help or suggestions will be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风蛊 2024-11-05 17:16:50

据我所知,您有两个选择:

1)再次加载。
不确定你的情况是否可行。这通常在处理数据库查询时完成。

2)将其放入 ViewState 中,如下所示:

ViewState["check"] = check;

然后加载它:

string check = Convert.ToString(ViewState["check"]);

As far as I know, you have two options:

1) Load it again.
Not sure if it's possible in your case. This is usually done when dealing with database queries.

2) Put it in the ViewState just like this:

ViewState["check"] = check;

And load it after with this:

string check = Convert.ToString(ViewState["check"]);
泅人 2024-11-05 17:16:50

您的类会在每次加载时实例化,因此它不会在页面视图之间具有全局变量。您需要以某种方式存储它。就像在查询字符串或会话中一样。您还可以使用视图状态。

例如

ViewState("Variable") = "你的字符串"

Your class is instantiated on every load so it will not have a global variable from page view to page view. You will need to store it somehow. Like in the querystring or a session. You can also use the viewstate.

For example

ViewState("Variable") = "Your string"

烏雲後面有陽光 2024-11-05 17:16:50

正如其他人已经回答的那样,Viewstate 是要走的路。无论你做什么,请不要把它塞进会话中。

Viewstate is the way to go, as the other people have answered. Whatever you do, please don't stuff it in the session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文