views:

99

answers:

3

Hello,

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.

+1  A: 

Hey,

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.

Brian
Thanks for the quick answer.I did try that but i couldn't get it to work.How would a write in my edit.aspx page to get the usercontrol and then use its properties (create a variable) ? The i did it was to create a usercontrol object but then then information is gone because i instantiate it as a new object...
If you use LoadControl and add this to the control tree, you can do one of two things: store it in a variable and always refer to this, or add it to the control tree, then use FindControl to find a reference to it later, and cast it to the user control type. ViewState reloads all control properties at Load event or later.
Brian
I do use LoadContol add the control. The FindControl takes an ID as a parameter. What is the ID of the usercontrol? Since loadcontrol takes a path to the file. I did also try to do this the first time but got a null pointer exception. Thanks again for the quick answer.This is the code snippet i can think of: var edit = (EditTemplate) FindControl("EditTemplate");
Still can't get i to work. I allways get null pointer exception when trying to access my usercontrol and its properties from the edit.aspx site.
Could you post some code related to the problem then? That would help.
Brian
A: 

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

Recursive-FindControl.

Radex
I tried to use this. This way i get a hold of the control. But when i set the IsValid property in my user control and later get the property in my page, the property is reset to the default value, which is true.It does not seem to matter when a set it to false. I am guessing that i am loosing the state of the control
Could you post a code where you are trying retrieve a value from IsValid property (whole method, if possible)? I am interested in what step of page's life cycle you want to access it. But if you are really losing state of the control, you should consider to use ViewState or Control State mechanics.
Radex
A: 

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
} 
Nathan Donze