Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Controls within UserControl null?

I've built a small User Control which is essentially a DropDownList with some preset Values based on what the Target-Property is set on.

Here's the Code:

public partial class Selector : System.Web.UI.UserControl
{
    public string SelectedValue { get {return this.ddl.SelectedValue; } }
    public int SelectedIndex { get { return this.ddl.SelectedIndex; } }
    public ListItem SelectedItem { get { return this.ddl.SelectedItem; } }
    private string target;
    public string Target { get { return this.target; } set { this.target = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        ddl.DataSource = target=="Group"?Util.GetAllGroups(Session["sessionId"].ToString()):Util.GetAllUsers(Session["sessionId"].ToString());
        ddl.DataBind();
    }
}

ASP-Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Selector.ascx.cs" Inherits="InspireClient.CustomControls.Selector" %>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>

If I insert my Selector into an aspx-Page it works just fine. Example:

<SCL:Selector Target="Group" runat="server" />

However, If I programmatically add it like this

ctrl = new Selector();
ctrl.Target = "User";

the DropDownList "ddl" is null and the application (logically) throws an error. Is Page_Load the wrong Method to do such a thing? What am I doing wrong?

I should add, "ctrl" is of type dynamic, not sure if this has anything to do with it.

Thanks in advance!

Dennis

like image 664
Dennis Röttger Avatar asked May 02 '11 09:05

Dennis Röttger


People also ask

What is Web user control?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.

What is UserControl in Winforms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.

What is user control and custom control in ASP.NET with example?

User controls are custom, reusable controls, and they use the same techniques that are employed by HTML and Web server controls. They offer an easy way to partition and reuse common user interfaces across ASP.NET Web applications. They use the same Web Forms programming model on which a Web Forms page works.


1 Answers

Since you're dynamically adding a user control and not a "simple" web control, you should use the LoadControl() method to instantiate it:

protected void Page_Load(object sender, EventArgs e)
{
    Selector yourControl = (Selector) LoadControl("Selector.ascx");
    yourControl.Target = "User";
    Controls.Add(yourControl);
}
like image 169
Frédéric Hamidi Avatar answered Nov 06 '22 09:11

Frédéric Hamidi