简体   繁体   中英

Asp.net passing values to the User control then display them

I'm trying to make a generic Search UserControl that can be given some values, based on those values the search results will display. However I'm currently trying to display the results of my values, and they always show up as my default values.

my UserControl code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ProductSearch.ascx.cs" Inherits="..." %>
<asp:Label ID="lblSearchWord" runat="server" />
<asp:Label ID="lblSearch" runat="server" />

Code Behind:

private string _searchWord = string.Empty;
private int _search = -1;
public string SearchWord
      {
         get { return _searchWord; }
         set { _searchWord = value; }
      }

      public int Search
      {
         get { return _search; }
         set { _search = value; }
      }
protected void Page_Load(object sender, EventArgs e)
      {
         lblGroupId.Text = LevelId.ToString();
         lblSearchWord.Text = SearchWord;
}

When I press the search button on the main aspx.cs page I do the following:

 protected void btnSearch_Click(object sender, EventArgs e)
      {
          ucPS.SearchWord = txtProductSearch.Text;
          ucPS.Search = 1
}

My aspx page contains the following

<%@ Register src="UserControls/ProductSearch.ascx" tagname="ProductSearch" tagprefix="ps" %>
<ps:ProductSearch id="ucPS" runat="server" />

My problem is that I can't use Query strings as the user might have selected some other things on this page that I need to keep the state of, however I did test that one and foudn it working.

Where am I going wrong? or is there an better alternative (except for query strings).

All variables in a page are disposed at the end of the page-lifecycle . Hence SearchWord will always be initialized with the default value on every postback.

You need to persist it somewehere else, for example in a ViewState variable.

public string SearchWord
{
    get
    {
        if (ViewState["SearchWord"] == null)
            return "";
        else
            return (String)ViewState["SearchWord"];
    }
    set { ViewState["SearchWord"] = value; }
}

Nine Options for Managing Persistent User State in Your ASP.NET Application

public string SearchWord
{
    get
    {
        if (ViewState["SearchWord"] == null)
           ViewState["SearchWord"] = string.Empty;

        return ViewState["SearchWord"];
    }
    set
    {
        ViewState["SearchWord"] = value;
    }
}

and I use databind not pageload, this way your usercontrol doesn't load unless you call it.

protected override DataBind()
{
     //you can add a condition here if you like
     if(SearchWord != string.Empty)
        lblSearchWord.Text = SearchWord;
}

to call this from aspx:

usercontrol.SearchWord = "my word";
usercontrol.DataBind();

and thats it..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM