Referencing a page control for another class

  • Thread starter Thread starter Greg Smith
  • Start date Start date
G

Greg Smith

I have a page that has a code behind file that is looking very messy. I
would like to put some of the code in a class library to clean up the page
but I can't get the syntax correct for referring to controls like buttons on
the from functions in the class library.

What would be the correct syntax to do this?


Any help is greatly appreciated.
 
Hi Greg,

Your idea sounds like a good one, only problem is we need to see some
code to see where it's going wrong. For a guess, I'd say that for
starters, your class lib project needs to reference System.Web, and
also import the namespaces such as System.Web.UI.WebControls.
 
Thank you for your response.
Your idea sounds like a good one, only problem is we need to see some
code to see where it's going wrong.

Here is my code.

The calling page:
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;

public partial class _Default : System.Web.UI.Page
{
Class1 cl = new Class1();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnCaller_Click(object sender, EventArgs e)
{
cl.ReLabel_button(this);
}
}

The class 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;

public class Class1
{
public Class1()
{

}
public void ReLabel_button(Page pg)
{
pg.btnCaller.Text = "Hello World!";
}
}
 
Thanks for posting the code. The issue is that the input parameter to
ReLabel_button is of type Page. This means that your passed-in Page
object is subclassed to type Page from type _Default. btnCaller,
however, is only available on type _Default, not type Page. Therefore,
it is not visible within the method.

An easier way to do this is to have the method take a parameter of type
Button, and just pass in btnCaller.
 
KJ said:
Thanks for posting the code. The issue is that the input parameter to
ReLabel_button is of type Page. This means that your passed-in Page
object is subclassed to type Page from type _Default. btnCaller,
however, is only available on type _Default, not type Page. Therefore,
it is not visible within the method.

An easier way to do this is to have the method take a parameter of type
Button, and just pass in btnCaller.

Rat farts!

Actually ther are about 30 controls. I was hoping to pass just the page.
 
You can pass the page. Just change the method signature to
ReLabel_button(_Default pg)

I'm not sure I follow.

So the code should look like ....

public partial class _Default : System.Web.UI.Page
{
Class1 cl = new Class1();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnCaller_Click(object sender, EventArgs e)
{
cl.ReLabel_button(_Default pg);
}
}

public class Class1
{
public Class1()
{
}
public void ReLabel_button(Page pg)
{
<Something something>.Text = "Hello World!";
}
}
 
Not quite. Code the method like this:

public void ReLabel_button(_Default pg)
{
///your code here
}

Then, call it like this:

cl.ReLabel_button(pg); //here, keep in mind that pg is of type _Default
 
I still have something wrong. Possibly to do with your "//here, keep in
mind that pg is of type _Default " statement.

My code now looks like this.

public partial class _Default : System.Web.UI.Page
{
Class1 cl = new Class1();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnCaller_Click(object sender, EventArgs e)
{
cl.ReLabel_button(pg); //here, keep in mind that pg is of type
_Default
}
}

public class Class1
{
public Class1()
{
}
public void ReLabel_button(_Default pg)
{
///your code here
}
}
 
Back
Top