Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在不同的类之间传递值_C#_Class_Static Members - Fatal编程技术网

C# 在不同的类之间传递值

C# 在不同的类之间传递值,c#,class,static-members,C#,Class,Static Members,我有两个独立的类:一个在tbIndexUI.aspx.cs页面中,另一个在常规的.cs类文件中。 我想将两个数据成员从常规的.cs类文件传递到.aspx页面,但是每次“page_Load”方法触发时,它都会重置以前传递的所有值。我尝试在“Page_Load”中注释所有内容,我甚至一起删除了该方法,但参数值仍在重置 是否有方法将这些值传递给并维护它们?我迷路了,任何例子都会非常有用。我看了这个,但没有成功 我的aspx.cs页面的代码 public partial class tbIndexUI

我有两个独立的类:一个在tbIndexUI.aspx.cs页面中,另一个在常规的.cs类文件中。 我想将两个数据成员从常规的.cs类文件传递到.aspx页面,但是每次“page_Load”方法触发时,它都会重置以前传递的所有值。我尝试在“Page_Load”中注释所有内容,我甚至一起删除了该方法,但参数值仍在重置

是否有方法将这些值传递给并维护它们?我迷路了,任何例子都会非常有用。我看了这个,但没有成功

我的aspx.cs页面的代码

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;
    }
 }
我的常规类process.cs的代码

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

不要让你的类库类有网页类的实例。相反,您希望您的.aspx页面/控件在您的“常规”.cs文件中有类的实例,因为这使它们可以跨多个页面重用

按照您发布代码的编写方式,
sendInformation
方法不能用于任何其他网页,因为它是硬编码的,以使用
tbIndexUI
控件

相反,您希望有一个包含
sendInformation
方法的类名称的实例(在发布的代码中没有指明)。这样做允许类保存
numocols
itemsPerCol
值,并通过属性将它们公开给网页/控件

你可以这样写:

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

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

现在,在您的aspx代码中,您有一个包含SMOfCols和ItemsPercol的类的实例,
并且无论何时您将该实例存储在
会话
缓存或
视图状态
中,它都可以在页面回发中保持。

不要让您的类库类有网页类的实例。相反,您希望您的.aspx页面/控件在您的“常规”.cs文件中有类的实例,因为这使它们可以跨多个页面重用

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"] ;
 }
按照您发布代码的编写方式,
sendInformation
方法不能用于任何其他网页,因为它是硬编码的,以使用
tbIndexUI
控件

相反,您希望有一个包含
sendInformation
方法的类名称的实例(在发布的代码中没有指明)。这样做允许类保存
numocols
itemsPerCol
值,并通过属性将它们公开给网页/控件

你可以这样写:

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

    // Method(s) here that set the values above
}
现在,在您的aspx代码中,您有一个包含SMOfCols和ItemsPercol的类的实例

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"] ;
 }
我建议您阅读以下指南,了解在页面和页面加载之间保存数据的不同方法

我建议您阅读以下指南,了解在页面和页面加载之间保存数据的不同方法


Mauricio,我尝试使用您在示例中显示的ViewState,但这些值不适用。当我单步执行代码时,值被正确声明,但当它进入aspx.cs页面时,Viewstates被重置为null。知道发生了什么吗?Mauricio,我尝试使用您在示例中显示的ViewState,但这些值不适用。当我单步执行代码时,值被正确声明,但当它进入aspx.cs页面时,Viewstates被重置为null。知道发生了什么吗?