snapplex
snapplex

Reputation: 851

passing values between separate classes

I have two separate classes: 1 In a tbIndexUI.aspx.cs page and the other in a regular.cs class file. I would like to pass two data members from the regular .cs class file to the .aspx page, however every time the "Page_Load" method fires it resets all the values that were previously passed. I tried commenting out everything in "Page_Load" and I event went as far as removing the method all together, but the parameter values are still being reset.

Is there a way to pass these values to and maintain them? Any examples would be extremely helpful as I am lost. I looked at this [example] but was unsuccessful.

Code for my aspx.cs page

public partial class tbIndexUI : System.Web.UI.UserControl
{
    private int _numOfCols = 0;
    private int itemsPerCol = 0;

    public int numColumns
    {
        set
        {
            _numOfCols = value;
        }
    }

    public int itemsPerColumn
    {
        set
        {
            _itemsPerCol = value;
        }
    }
    public static void passData(int numOfCol, int itemsPerCol)
    {
        numColumns = numOfCol;
        itemsPerColumn = itemsPerCol;
    }
 }

Code for my regular class process.cs

void sendInformation()
{
    tbIndexUI.passData(numOfCols, itemsPerCol);
}

Upvotes: 1

Views: 3524

Answers (2)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

public partial class tbIndexUI : System.Web.UI.UserControl
{
    public int numColumns
    {
        set
        {
            ViewState["numOfCols"] = value;
        }
    }

    public int itemsPerColumn
    {
        set
        {
            ViewState["itemsPerCol"] = value;
        }
    }
    public static void passData(int numOfCol, int itemsPerCol)
    {
        numColumns = numOfCol;
        itemsPerColumn = itemsPerCol;
    }

    //when you need to use the stored values
    int _numOfCols = ViewState["numOfCols"] ;
    int itemsPerCol = ViewState["itemsPerCol"] ;
 }

I recommend that you read the following guide about the different ways that you can persist data between pages and page loads

http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-State

Upvotes: 1

Karl Anderson
Karl Anderson

Reputation: 34846

Do not have your class library classes have instances of web page classes. You want the opposite, you want your .aspx page/controls to have instances of classes in your "regular" .cs files, because that makes them reusable across multiple pages.

The way your posted code is written, the sendInformation method cannot be used with any other web page, because it is hard-coded to use the tbIndexUI control.

Instead, you want to have an instance of whatever the class name (you do not indicate that in your posted code) is that has the sendInformation method in it. Doing it this way allows for the class to hold the numOfCols and itemsPerCol values and expose them to the web page/control via properties.

Instead you could write this:

public class TheClassThatHoldsNumOfColsAndItemsPerCol
{
    public int NumOfCols { get; set; }
    public int ItemsPerCol { get; set; }

    // Method(s) here that set the values above
}

Now in your aspx code, you have an instance of TheClassThatHoldsNumOfColsAndItemsPerCol and anytime you store that instance in Session cache or ViewState so it can persist across page postbacks.

Upvotes: 0

Related Questions