An instance of a form in another

  • Thread starter Thread starter Dan Aldean
  • Start date Start date
D

Dan Aldean

Hi,

2 web pages, One of them has properties that I need to read in a second
form:

First Form

public class FirstForm : System.Web.UI.Page
{
private void submitButton_Click(......)
{
Server.Transfer("SecondForm.aspx")
}

public string UserName //property
{
get
{
return userName.Text; //textbox
}
}
}


-------------------------------
Second form

public class SecondForm
{
public FirstForm firstForm;

private void Page_Load(...)
{
firstForm = (FirstForm) Context.Handler;
userGreeting.Text = firstForm.UserName;
}
}


The problem is that on the real project it does not work.
public FirstForm firstForm;
issues an error, it doesn't know what FirstForm is.
What do I miss?

Thanks
 
Take a look at the PreviousPage property in your second webform. Once again,
this is an ASP.NET newsgroup issue.
Peter
 
Hi

I have a suggestion..
Another option would be moving your logic to another class which is not
a form, I mean moving data to a high level layer. (may be business
object)

I think making objects of one form in another form for accessing data
is not preferable...

Thanks
-Cnu.
 
Thanks,
Let me refraze:
2 WINDOWS pages, one of them has properties that I need to read in a second
form:

First Form

public class FirstForm : System.Web.UI.Page
{
public string UserName //property
{
get
{
return userName.Text; //textbox
}
}
}


-------------------------------
Second form

public class SecondForm
{
public FirstForm firstForm;

}

Now it's not ASP anymore, the question still remains.
 
thanks to all for the answers. but still why the forms are not visible to
one another?
i have a sample that works. unfortunately the project that i took over
doesn't.
it might be some settings that prevents that.
 
OK, now your code sample is dealing once again with WebForms (ASP.NET web
pages). We have two different animals here - Windows Forms are for regular
windows executable programs that run on your desktop. WebForms run from an
asp.NET WEB SITE that you bring up in your browser.

I suggest to focus on one modality at a time to avoid further confusion!
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
Hi

"Windows forms", I think you are trying to access data of a control on
the windows form.
If you place a control on a windows form, the control will become an
private member of the form. so you can not access the property out side
the form. eg: Say If you have a textbox on form2, textbox would be
private to the form2. you can create an instance of form2 in form1 but
can not access the text box. However public data / properties of form2
are accessible to the form1. Have look at the following code snippet...


namespace SAMPLE
{
public partial class Form1 : System.Windows.Form
{
private Form2 frm2 = new Form2(); //create an instance of
Form2

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(frm2.data); //data is accecible

// frm2.textbox1. OOOOPS not available
}
}
}

namespace SAMPLE //textbox would be private member that can be found
in Form2.Designer.cs
{
public partial class Form2 : System.Windows.Form
{
public string data; //accesible out side
public Form2()
{
data = "Some data here";
InitializeComponent();
}
}
}

I do not have much experience of the WebForms and its internals :-(

Thanks
-Cnu.
 
This is the part that is not working. For Form1 Form2 does not exist,
therefore I cannot read th eproperty created by me in the Form1 with GET
private Form2 frm2 = new Form2(); //create an instance of

It must be some settings that prevent the instance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top