Reputation: 163
I have a large number of .aspx
pages (asp.net 4, c#) that all inherit from a custom base page class (that inherits from System.Web.UI.Page class).
By convention all these pages have the same set of controls on them (for example a couple of textboxes with the same ID).
I'd like to put some generic code in the custom base page class that retrieves the .Text
values from these controls on the page.
Note this is not a MasterPage set up. I have a custom base page class, then a series of pages that inherit from that base page class.
How do I reference the textboxes on the page from the base class?
Upvotes: 4
Views: 3164
Reputation: 1786
One thing you could do is put an abstract method or property on the base class. This would force the inheritors to implement this method and the base could reliably call it.
protected abstract TextBox MyTextBox { get; }
then all your inherited pages would have to implement this method and ideally would return their MyTextBox.
You could create an interface as defined above or just make another PageBase that inherits from your other Base that represents pages with that set of controls.
EDIT:
As an example of implementation. Assuming the base class is called MyPageBase and HomePage.aspx has a Textbox on it with ID="TextBox1"
On the base define the abstract property
public abstract class MyPageBase : Page
{
protected abstract TextBox MyTextBox { get; }
}
On the page:
public partial class HomePage : MyPageBase
{
protected override TextBox MyTextBox
{
get
{
return this.TextBox1;
}
}
}
Within the base you can access the property since its abstract it works like an interface and MUST be implemented. Then the base can access this property assuming the inheritor honors the contract.
this.MyTextBox.Text = "Change the text";
If all you want to do is modify the text or another specific property it would be more ideal to encapsulate it better and provide a getter/setter to only the text property on the specific textbox. The sample doesn't let you change the actual instance of the textbox but it lets you access and modify any of its properties.
Upvotes: 3
Reputation: 46067
You should be able to access the controls using Page.FindControl("ControlID")
.
From your base class:
var txt = Page.FindControl("TextBox1") as TextBox;
if (txt != null)
{
//found the textbox
//...
}
Depending where on the form the controls are located, specifically if they're located in a container that implements the INamingContainer
interface, you may need to make a recursive FindControl()
method that can traverse the control hierarchy.
public Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
foreach (Control control in root.Controls)
{
Control foundControl = FindControlRecursive(control, id);
if (foundControl != null)
return foundControl;
}
return null;
}
Upvotes: 4
Reputation: 9180
If they all have the same set of controls on them by convention, why not move the controls to the base class?
Alternatively, you could create an interface that includes the common set of controls, and then implement that interface in all your aspx.cs codebehinds. This would allow you to have some aspx pages that defy the convention. You can cast "this" as the interface, in the base class and if its not null, modify the controls. For example:
IControlSet controlSet = this as IControlSet;
if(controlSet != null)
{
controlSet.Name.Text = "someName";
}
Upvotes: 2