Creating a custom user control programatically

Discussion in 'ASP .Net Building Controls' started by Big Daddy, Nov 1, 2006.

  1. Big Daddy

    Big Daddy Guest

    I created a composite control derived from UserControl. It has two
    textboxes. The ascx file looks like this:

    <%@ Control Language="C#" AutoEventWireup="true"
    CodeFile="RangeCriteriaControl.ascx.cs" Inherits="RangeCriteriaControl"
    %>
    <asp:TextBox ID="txtLow" runat="server" Columns="5"></asp:TextBox>
    &nbsp;to&nbsp;
    <asp:TextBox ID="txtHigh" runat="server" Columns="5"></asp:TextBox>

    If I include this control in the aspx file of a page, it works great.
    But I would like to be able to instantiate it programmatically. When I
    try this from within the code-behind of my page:

    _criteriaControl = new RangeCriteriaControl();
    Controls.Add(_criteriaControl);
    _criteriaControl.BindData();

    Within the BindData() method, I try to fill in the two textboxes with
    data, but the references to the two textbox controls are null. For
    example, if this is the method:

    public void BindData()
    {
    txtLow.Text = "2";
    txtHigh.Text = "3";
    }

    txtLow and txtHigh are null. Why is this? Is there any way to create
    my control programmatically and have the controls within it be
    instantiated right away too?

    Thanks in advance,
    John
     
    Big Daddy, Nov 1, 2006
    #1
    1. Advertising

  2. You want to call the LoadControl method. It is explained here.

    http://msdn2.microsoft.com/en-us/library/c0az2h86.aspx

    Brennan Stehling
    http://brennan.offwhite.net/blog/

    Big Daddy wrote:
    > I created a composite control derived from UserControl. It has two
    > textboxes. The ascx file looks like this:
    >
    > <%@ Control Language="C#" AutoEventWireup="true"
    > CodeFile="RangeCriteriaControl.ascx.cs" Inherits="RangeCriteriaControl"
    > %>
    > <asp:TextBox ID="txtLow" runat="server" Columns="5"></asp:TextBox>
    > &nbsp;to&nbsp;
    > <asp:TextBox ID="txtHigh" runat="server" Columns="5"></asp:TextBox>
    >
    > If I include this control in the aspx file of a page, it works great.
    > But I would like to be able to instantiate it programmatically. When I
    > try this from within the code-behind of my page:
    >
    > _criteriaControl = new RangeCriteriaControl();
    > Controls.Add(_criteriaControl);
    > _criteriaControl.BindData();
    >
    > Within the BindData() method, I try to fill in the two textboxes with
    > data, but the references to the two textbox controls are null. For
    > example, if this is the method:
    >
    > public void BindData()
    > {
    > txtLow.Text = "2";
    > txtHigh.Text = "3";
    > }
    >
    > txtLow and txtHigh are null. Why is this? Is there any way to create
    > my control programmatically and have the controls within it be
    > instantiated right away too?
    >
    > Thanks in advance,
    > John
     
    Brennan Stehling, Nov 2, 2006
    #2
    1. Advertising

  3. Big Daddy

    Big Daddy Guest

    Thanks, that did the trick.
     
    Big Daddy, Nov 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SamIAm
    Replies:
    1
    Views:
    583
    Jerry III
    Oct 17, 2003
  2. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,104
    Ravi Ambros Wallau
    Jun 1, 2005
  3. Leeor Chernov
    Replies:
    2
    Views:
    798
    Leeor Chernov
    Oct 16, 2005
  4. Matt Sokol
    Replies:
    2
    Views:
    409
    Theo Tillotson
    Aug 7, 2003
  5. Ravi Ambros Wallau
    Replies:
    0
    Views:
    320
    Ravi Ambros Wallau
    Jun 1, 2005
Loading...

Share This Page