How do I access a control from a class not other than WebForm1?

  • Thread starter Thread starter Richard Dixson
  • Start date Start date
R

Richard Dixson

I created a web form and dropped a table in there called resultsTable. This
also resulted in the following being added to the Webform1 class:
public System.Web.UI.WebControls.Table resultsTable;

I can easily add rows and columns via C# programming from the WebForm1
class. However I cannot figure out how I can access this control outside of
this class.

For example, I have a different class called TestResult. As I create
TestResult classes from the Page_Load event, I want the constructor of
TestResult to add rows and columns to the resultsTable. However I can't
figure out how I can access the resultsTable from my TestResult constructor.

For example:

private void Page_Load(object sender, System.EventArgs e)
{
// Generate rows and cells.
TestResult[] TestResults = new TestResult[3];
TestResults[0] = new TestResult("Mike Brown", "Pass");
TestResults[1] = new TestResult("Jill Smith", "Did not attend");
...

So then in my TestResult constructor, as I receive the name and status (i.e.
Mike Brown, Pass) I want to add a row to the table for each person and set
the cells to these values, among other things that the constructor is doing.
I imagine I can pass a reference to the testResult table but that seems a
bit messy. Is there a better way? How can I talk to the testResults table
to add these rows/columns from a class other than the class testResults
table is defined in?

Thank you.
 
The easiest, IMO, would be to pass in the table object, by reference, to the
class you are using to add rows.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
I was thinking along those lines - just seems kind of messy. For example I
have:

TestResults[0] = new TestResult("Bill Brady", "Pass", resultsTable);
TestResults[1] = new TestResult("Jill Smith", "Did not attend",
resultsTable);

Just doesn't seem very elegant/programatically correct.

Isn't there some way that I could have the TestResult class call a method or
property in Webform to get a reference to the resultsTable. The more I
think about it that should be doable. For example if I add a getter to the
Webform class that returns a reference to the resultsTable, then I can use
that getter from my TestResult class to obtain a copy, right?

Cowboy said:
The easiest, IMO, would be to pass in the table object, by reference, to the
class you are using to add rows.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Richard Dixson said:
I created a web form and dropped a table in there called resultsTable. This
also resulted in the following being added to the Webform1 class:
public System.Web.UI.WebControls.Table resultsTable;

I can easily add rows and columns via C# programming from the WebForm1
class. However I cannot figure out how I can access this control
outside
of
this class.

For example, I have a different class called TestResult. As I create
TestResult classes from the Page_Load event, I want the constructor of
TestResult to add rows and columns to the resultsTable. However I can't
figure out how I can access the resultsTable from my TestResult constructor.

For example:

private void Page_Load(object sender, System.EventArgs e)
{
// Generate rows and cells.
TestResult[] TestResults = new TestResult[3];
TestResults[0] = new TestResult("Mike Brown", "Pass");
TestResults[1] = new TestResult("Jill Smith", "Did not attend");
...

So then in my TestResult constructor, as I receive the name and status (i.e.
Mike Brown, Pass) I want to add a row to the table for each person and set
the cells to these values, among other things that the constructor is doing.
I imagine I can pass a reference to the testResult table but that seems a
bit messy. Is there a better way? How can I talk to the testResults table
to add these rows/columns from a class other than the class testResults
table is defined in?

Thank you.
 
Back
Top