Re-use code
-
Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code:
public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } }
If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-) -
Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code:
public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } }
If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-) -
Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code:
public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } }
If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-)You need to add a
using
statement at the top of your new program, that refers to the namespace whichViewState
belongs to.Kristian Sixhoej
"Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
-
Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code:
public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } }
If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-)- ViewState is serialized and can contain serializable types. Do not convert to string. Use: ViewState["w/e"] = someObject directly. 2) Create a base page that inherits from page then you can reuse the code.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
You need to add a
using
statement at the top of your new program, that refers to the namespace whichViewState
belongs to.Kristian Sixhoej
"Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
Hi Kristian, Microsoft sais that the namespace are UI, and I have that namespace in the using statements. So I have no clue, what to do?
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; /// /// Summary description for ERSGridView /// public class ERSGridView {
-
- ViewState is serialized and can contain serializable types. Do not convert to string. Use: ViewState["w/e"] = someObject directly. 2) Create a base page that inherits from page then you can reuse the code.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
Hi Ennis, How do I create a base page that inherits from page. I now tried to search at google, and I cann't figure out to replace the expamples with my code?
in App_Code:
public class MyPageBase : System.Web.UI.Page {
public string ViewStateItem{ get{ return (string)ViewState\["someViewStateItem"\]; } set{ ViewState\["someViewStateItem"\] = value; } }
}
In your Web project
public class SomeWebPage : MyPageBase { ... }
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
in App_Code:
public class MyPageBase : System.Web.UI.Page {
public string ViewStateItem{ get{ return (string)ViewState\["someViewStateItem"\]; } set{ ViewState\["someViewStateItem"\] = value; } }
}
In your Web project
public class SomeWebPage : MyPageBase { ... }
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest HemingwayI copied the same code into a new file I called ERSBasePage and I gets the same error here to..... my ERSBasePage.cs file:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Summary description for ERSPageBase /// public class ERSPageBase { public ERSPageBase() { // // TODO: Add constructor logic here // } public string ViewStateItem { get { return (string)ViewState["someViewStateItem"]; } set { ViewState["someViewStateItem"] = value; } } }
And the page where I want to inherith:public partial class ERS_Adm_AdmFrontPage : ERSPageBase { protected void Page_Load(object sender, EventArgs e) {
The error sais: Error 1 The name 'ViewState' does not exist in the current context C:\Inetpub\wwwroot\ANSI\App_Code\ERSPageBase.cs 24 30 C:\...\ANSI\ As you can see, I am using the page directive UI as I read I shall use regarding ViewState -
I copied the same code into a new file I called ERSBasePage and I gets the same error here to..... my ERSBasePage.cs file:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Summary description for ERSPageBase /// public class ERSPageBase { public ERSPageBase() { // // TODO: Add constructor logic here // } public string ViewStateItem { get { return (string)ViewState["someViewStateItem"]; } set { ViewState["someViewStateItem"] = value; } } }
And the page where I want to inherith:public partial class ERS_Adm_AdmFrontPage : ERSPageBase { protected void Page_Load(object sender, EventArgs e) {
The error sais: Error 1 The name 'ViewState' does not exist in the current context C:\Inetpub\wwwroot\ANSI\App_Code\ERSPageBase.cs 24 30 C:\...\ANSI\ As you can see, I am using the page directive UI as I read I shall use regarding ViewStateLook really close at your definition of ERSPageBase. There is something missing that is not missing from my sample.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway