noshitsherlock
noshitsherlock

Reputation: 1153

Find control in usercontrol from a Page ASP.NET

I am loading a control to a page dynamically with LoadControl("src to file").

In the usercontrol i have a validator and some other controls that i would like to access from my page. I canät get it to work, null pointer exception.

Scenario is like this. I have a Edit.aspx page which loads the EditTemplate.ascx usercontroll. I would like to get information or find the controls in the EditTemplate from the Edit.aspx site.

I have tried exposing the controls and validators as properties but how do i access them from my Edit.aspx?

Example code:

Edit.aspx, the control is later added into a

Control control = LoadControl("src to ascx");
TemplatePlaceHolder.Controls.Add(control);

EditTemplate.ascx

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyImageFile" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

CodeBehind

public partial class EditTemplate : System.Web.UI.UserControl, IEditTemplate {
    public RequiredFieldValidator Validator {
        get { return this.RequiredFieldValidator1; }
        set { this.RequiredFieldValidator1 = value; }
    }

From the Edit.aspx site i would like to check the validators isValid property. Isvalid is set in a Save method.

The save button that saves the template is located in edit.aspx, so the post in done from that page.

So the question is how to get a hold of the property from the usercontrol in the edit.aspx page, where and how should this be done?

Thanks again.

Upvotes: 1

Views: 6037

Answers (3)

Nathan Donze
Nathan Donze

Reputation: 1384

As mentioned in previous answers, I would expose any validators you must access from the parent ASPX page as properties in the user control.

public RequiredFieldValidator ValidatorToCheck
{
    get { return this.rfvMyField; }
}

Then, you can dynamically add your user control to some placeholder (being sure to assign an ID to the user control).

// In my example, this is occurring in the Page_Load event
Control control = LoadControl("~/Controls/EditTemplate.ascx");
control.ID = "ucEditTemplate";
pnlControlHolder.Controls.Add(control); // the placeholder in my example is a panel

When you want to access the IsValid property on the given validator (presumably in your save action) you can do so as follows (being sure to cast the control to the appropriate type and using the ID you originally assigned to the user control):

EditTemplate control = (EditTemplate)pnlControlHolder.FindControl("ucEditTemplate");
if (control.ValidatorToCheck.IsValid)
{
    // Some action
} 

Upvotes: 0

Radek Stromsk&#253;
Radek Stromsk&#253;

Reputation: 858

You can always use recursive approach. Check the solution on Steve Smith's blog:

Recursive-FindControl.

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

Easiest way is to have the user control define properties like:

public IValidator SomeValidator {
  get { return this.cuvValidator; }
  set { this.cuvValidator = value; }
}

public string Text {
  get { return this.txtText.Text; }
  set { this.txtText.Text = value; }
}

Which your edit page can use.

HTH.

Upvotes: 1

Related Questions