2

I have a simple asp.net user control:

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="QueryDefinitionItem.ascx.cs"
    Inherits="xxx.UserControls.QueryDefinitionItem" %>
    <link href="../Styles/Site.css" rel="stylesheet" type="text/css" />
    <div class="title">
      <h1 id="TextField" runat="server"></h1>
    </div>

Code behind:

 public partial class QueryDefinitionItem : System.Web.UI.UserControl
{
    private string m_text;
    protected void Page_Load(object sender, EventArgs e)
    {
        TextField.InnerText = m_text;
    }

    public void SetText(string text)
    {
        m_text = text;
    }
}

I use LoadControl to load one instance programatically:

protected void Page_Load(object sender, EventArgs e)
    {
        var control = (QueryDefinitionItem)LoadControl(typeof (QueryDefinitionItem),null);
        control.SetText("test2");
        itemsPanel.Controls.Add(control);}

I then use the SetText function to set the text and once the Page Load is fired I get a NullReferenceException that says TextField is null...

I cant figure out why.

2
  • At what point are you adding the control dynamically ? Commented Jan 3, 2012 at 9:34
  • @RichardFriend Its the page load event of the page Commented Jan 3, 2012 at 9:41

3 Answers 3

3

You should have to use LoadControl(string) method instead of LoadControl(Type,Param) and store value directly to the HtmlControl. (There is bug take a look at connect).

protected void Page_Load(object sender, EventArgs e)
{
}
public void SetText(string text)
{
  TextField.InnerText = text;
}

Or create a property,

   public string Text
    {
      get { return TextField.InnerText; }
      set { TextField.InnerText=value;  }
    }

Use Page_Init or Page_Load event to create controls dynamically.

protected void Page_Load(object sender, EventArgs e)
  {
  var control = (QueryDefinitionItem)LoadControl("~/QueryDefinitionItem.ascx");
  control.SetText("test2");
  itemsPanel.Controls.Add(control);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

@Amit Where are you trying to access that TextField. I mean in which event it is returning you null. Is it Page_Load of control or the page where you are creating it?
@AVD I am creating the user control at the page load event of the page and I have tried to access it on the page load of the control. I have also tried accesing it (user the method you offered) directly from the page load of the page.
0

Try to use the same code but in page pre render should work.

Comments

0

Swap these around

control.SetText("test2");   
itemsPanel.Controls.Add(control);

Becomes

itemsPanel.Controls.Add(control);
control.SetText("test2");   

Your user control will not have created its control tree until it is added to the control tree itself.

You can also use EnsureChildControls to make sure it has created its controls.

public void SetText(string text)  { 
    EnsureChildControls();
    TextField.InnerText = text;  
}  

I would also recommend creating your control before page_load, this is a good read

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.