I am new to c#.
Is it possible to use a local variable (declared in Page_Load method inside System.Web.UI.Page class) in the .aspx page. or do i have to declare an accessor variable inside "UI.Page" class and use it as reference?
public partial class consoleTours : System.Web.UI.Page
{
public string AStr{ get; set; }// i could use this
}
protected void Page_Load(object sender, EventArgs e)
{
string LStr=""; <i>// i couldn't use this
}
Thank you for edit. as to c# i am also new to stackoverflow as you could see.
Point of my question is.I couldn't use public property (AStr) for tryParse.i first use local variable for parsing then assign LStr to AStr and use it in the page. so it makes me use 2 variables instead one. I thought there should be another way.
You have 2 valid options 1:
Use a public property on the page:
This is what you have done already:
public class MyPage : System.Web.UI.Page
{
public string MyPageTitle { get; set; }
}
Now, the property MyPageTitle can be accessed anywhere in your cs file, and can be used in your ASPX file aswell.
If you want to have a property which is accessible on multiple pages, you must play with inheritance:
Use inheritance to create a new Page object:
First, you create class that acts as a Page:
public class ParentPage : System.Web.UI.Page
{
public string MyPageTitle { get; set; }
}
Now, when you create a new page, your code will look by default like this:
public class MyPage : System.Web.UI.Page
{
}
Change the System.Web.UI.Page to your created ParentPage, so it will looks like the following:
public class MyPage : ParentPage
{
}
Now, in the 'MyPage' class, you will have access to the MyPageTitle property as well as on the aspx file.
Thus, your are exposing a variable to another control by using inheritance.
Declare the variable just inside the class and outside of a method
public string LStr="";
protected void Page_Load(object sender, EventArgs e)
{
LStr= "this new value";
}
Related
I have a code behind that has two classes within the same namespace. I'd like to reference the first classes public variables from the second class in the namespace. I know I could obviously pass the variables, but I'd like to know if there is a way to reference them.
namespace CADE.results
{
public partial class benchmark : BasePage
{
public string numAnswers = ""; <-- like to reference this from GetScore()
protected void Page_Load(object sender, EventArgs e)
{
BenchmarkAdd bma = new BenchmarkAdd();
bma.GetScore();
}
}
public class BenchmarkAdd
{
public BenchmarkAdd()
{
}
public void GetScore()
{
benchmark.numAnswers++; <-- would like to reference public var here
}
}
}
I thought just benchmark.numAnswers would work but it doesn't. Is it possible to reference numAnswers from the BenchmarkAdd class?
I'd like to reference the first classes public variables from the second class in the namespace.
Well numAnswers is an instance variable, so GetScore() isn't going to know which instance to update.
The only way this can be done (without passing your Page instance, or passing numAnswers using ref) is to make numAnswers static:
public static string numAnswers = "";
Which you could then update in GetScore():
public void GetScore()
{
CADE.results.benchmark.numAnswers++;
}
However, the effect of making this static is that each benchmark instance no longer has it's own numAnswers field; there would be only one copy of the field which would instead belong to the type itself.
I have a problem validating the user of the page in ASP.Net my plan is using Attribute per page but it seams the constructor of the class is called first before the attribute. Is there a way to do this using Attribute?
i tried something like this
public class BaseAuthenticate : Attribute
{
public BaseAuthenticate(string pageID)
{
// condition if current user is allowed in pageID,
// throws exception if not allowed
}
}
[BaseAuthenticate("03902020-BC73-4DC0-A000-D4E20409FA2C")]
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// will not reach here if not validated in BaseAuthenticate
}
}
any help would be appreciated.
TIA
Change your constructor such that it calls the superconstructor -
public BaseAuthenticate(string pageID) : base() {
//blah blah blah
// Egyptian braces >>>
}
That should execute the base constructor. Obviously, whatever the superclass needs as an argument will have to be put in base().
I have a default page and an analisis page, and at Default I have some radio buttons to change a table, but I'm getting a "an object reference is required for the non-static field" exception on compilation, everything is non-static, and I haven't figured out why I'm getting this error.
Here's the code for _Default:
public partial class _Default : System.Web.UI.Page {
public bool Carro {
get { return radCarroSi.Checked ;}
}
}
And here is the code for Analisis:
public partial class Analisis : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
_Default prevPage = PreviousPage as _Default;
if (prevPage != null) {
if (_Default.Carro == true) {
row8.Visible = false;
}
}
}
}
}
Can you help me? I believe its pretty easy but as I'm new at asp I haven't seen the problem.
Thanks in advance.
You don't have an instance of _Default:
You're just referring to the class name, not the instance of the class.
To elaborate a little further.
public class YourClass
{
public bool Carro { get; set; }
}
YourClass instance = new YourClass(); // this would create a new instance of `YourClass`.
You can refer to it by using instance.Carro like you have done with _Default.Carro
However I believe you are trying to determine if the value of Carro has been checked in the page, ASP.NET doesn't work quite like this, you will need to understand how to manage state between the client and server. This can be achieved via ViewState, Session, Cookies and Query Strings
You can't check if a radio button is checked in a different page so this line will not work in the Analisis page (besides, you don't even have an instance of _Default):
if (_Default.Carro == true) {
row8.Visible = false;
}
You need to pass the radion button value between pages using Session, Querystring or some other method
I have been battling this for some time and I need some guidance.
I'm coding in ASP.NET 4.0 WEBFORMS.
Question is: How to expose a textbox, Label or any other control to another class.
I have a webform (see below).
public partial class WebForm1 : System.Web.UI.Page
{
}
This is then referenced and sent to another class.
public class SearchInitializer
{
private WebForm1 _webform1;
public SearchInitializer(WebForm1 Webform1)
{
_webform1 = Webform1;
}
public void ChewSettings()
{
_webform1 //can't find any control in here?!
}
}
First I thought of creating a public property which I thought I could access from the reference I sent to the new class.. But nooo!
public partial class WebForm1 : System.Web.UI.Page
{
public string KeywordBox1
{
get {return txt_keyword.Text;}
set {txt_keyword.Text = value;}
}
}
The I tried to inherit the Webform into the other class. Making the the property available but no luck there.
public class SearchInitializer : Webform1
{
private WebForm1 _webform1;
public SearchInitializer(WebForm1 Webform1)
{
_webform1 = Webform1;
}
public void ChewSettings()
{
_webform1 //can't find any control in here?!
}
}
Okay an abstract class migth be of use here, inheriting everything. But I think I got that wrong to. I have events and static classes, so they can talk with the page. But I really would like not to use a static class as a container to save all the info in my controls.
So these are the examples I have tried and they all failed. So this is me basicly trying to expand what I know ;) Thanks for reading!!
Why have they failed and how should I do it?
EDIT AS REQUESTED!
public partial class WebForm1 : System.Web.UI.Page
{
protected void btn_click(object sender, EventArgs e)
{
SearchInitializer searchIni = new SearchInitializer(this);
}
}
To expose the controls there are two methods I can think of that you can employ.
You can remove the following statement from the myPage.designer.cs file and place it in your code behind as a public declaration:
protected global::System.Web.UI.WebControls.TextBox myTextBox;
becomes
public System.Web.UI.WebControls.TextBox myTextBox;
This should make it immediately accessible. My preferred method is to add a property for each specific control that you want to provide access to.
public System.Web.UI.WebControls.TextBox MyTextBoxElement
{
get
{
return myTextBox;
}
}
This allows to provide supplementary access controls if you need them or other conditionals. In any case, to access either the field or the property, the consuming object must reference this by your specific page type.
Not sure what you are trying to do, but to access a base class within an inherited calss you need to use the base keyword, not declare an instance there of.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string KeywordBox1
{
get { return txt_keyword.Text; }
set { txt_keyword.Text = value; }
}
}
public class SearchInitializer : WebForm1
{
public SearchInitializer()
{
}
public void ChewSettings()
{
// Works
base.KeywordBox1 = "Red";
}
}
If intellisense is not showing the property, try rebuilding the solution. It will then refresh the list of available properties and it should show.
Your original approach must work. I suggest you create a small test project with a form with text box and SearchInitializer class and see that it works, after that figure out what is different in your current project.
i have two separate questions; however, since they are very similar i will ask them in one posting.
what is the reason i cannot reference the textboxes from here? i created another file in my project and put
namespace EnterData.DataEntry
{
public partial class WebForm1 : System.Web.UI.Page
{
to make it go into the same namespace and partial class as the webform. but i cannot access the textbox!
public partial class WebForm1 : System.Web.UI.Page
{
public class LOMDLL.Main_Lom_Form PopulateMainForm()
{
//populate class
LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
try
{
TheForm.lom_number = lom_numberTextBox.Text.ToInt();
TheForm.identified_by = identified_byTextBox.Text;
TheForm.occurrence_date = occurrence_dateTextBox.Text.ToDateTime();
//TheForm.pre_contact = pre_contactTextBox.Text; //need to create this texdtbox
//TheForm.pre_practice_code = pre_practice_codeTextBox.Text; //create this
TheForm.report_by = report_byTextBox.Text;
TheForm.report_date = report_dateTextBox.Text.ToDateTime();
TheForm.section_c_comments = section_c_commentsTextBox.Text;
TheForm.section_c_issue_error_identified_by = section_c_issue_error_identified_byTextBox.Text;
TheForm.section_d_investigation = section_d_investigationTextBox.Text;
TheForm.section_e_corrective_action = section_e_corrective_actionTextBox.Text;
TheForm.section_f_comments = section_f_commentsTextBox.Text;
}
catch (Exception e)
{
}
i get this error:
Error 20 Cannot access a non-static member of outer type 'EnterData.DataEntry.WebForm1' via nested type 'EnterData.DataEntry.WebForm1.LOMDLL' C:\Documents and Settings\agordon\My Documents\Visual Studio 2008\Projects\lomdb\EnterData\DataEntry\DAL.cs 68 38 EnterData
on all textboxes
what is the reason that i cannot access the textboxes from here?
Did you mean to nest classes here? If you intended to declare a method that returns a Main_Lom_Form(), try this:
public LOMDLL.Main_Lom_Form PopulateMainForm()
If you intended to call that method against a member of WebForm1 called TheForm, then instantiate that outside of the call to PopulateMainForm:
LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
public void PopulateMainForm()
{
// snip
}
here there is an error:
public class LOMDLL.Main_Lom_Form PopulateMainForm()
are you declaring this class inside the webform class? anyway the class declaration is wrong.
for the last point of your question, when you have this:
public static class Main_Lom_Form
imagining you moved out from the other webform class, what is the namespace you have around it (before)? Just play around moving the class inside same namespace than class WebForm1 and you will not need to put the namespace before the class name, you will be able to create it like this:
var obj = new Main_Lom_Form();
in case it would make sense, but I doubt ;-)
I think you have a few issues with your code. The first one that pops out is this:
public class LOMDLL.Main_Lom_Form PopulateMainForm()
That is not a valid line of C# code. I'm assuming you actually meant to write:
public LOMDLL.Main_Lom_Form PopulateMainForm()
Secondly, if you have defined Main_Lom_Form as static, you can't instantiate it, it's a static class. You can address this:
public class Main_Lom_Form
I think its a combination of the above two issues which is causing the compiler to have a stroke.