Server Control Property Problem

Discussion in 'ASP .Net Building Controls' started by Joe, Nov 5, 2003.

  1. Joe

    Joe Guest

    I wrote a simple server control that inherits from the DropDownList
    control. I will be using the control in an aspx page and want to
    access it in a repeater. I have created a public property called
    DefaultValue. For some reason, the property is never set from within
    my aspx page. It just gets ignored. Does anyone know why?

    Here is how it looks in my aspx:
    --------------------------------------------------------

    <frmcontrol:controltypedropdown DefaultValue="5"
    id="Controltypedropdown1" runat="server"/>

    ----------------------------------------------------------
    Below is my control code.
    -----------------------------------------------------------

    namespace blah.Controls
    {

    /// <summary>
    /// A server control to display the dropdown of Control Types. Will
    be
    /// displayed programatically in a repeater.
    /// </summary>
    public class ControlTypeDropDown :
    System.Web.UI.WebControls.DropDownList
    {
    private DataTable dt;
    private ListItem[] coll;

    private string defaultValue="0";
    public string DefaultValue
    {
    get{return defaultValue;}
    set{defaultValue=value;}
    }

    public ControlTypeDropDown() : base()
    {
    lock(this)
    {
    if(HttpContext.Current.Application["ControlTypeDropDown"]==null)
    {
    Survey s = new Survey();
    dt = s.GetFormControls();


    coll = new ListItem[dt.Rows.Count+1];
    coll[0] = new ListItem("", "0");
    for(int i=0;i<dt.Rows.Count;i++)
    {
    coll[i+1] = new ListItem(Convert.ToString(dt.Rows["type"]),
    Convert.ToString(dt.Rows["controlTypeId"]));
    }

    HttpContext.Current.Application.Add("ControlTypeDropDown", coll);
    }
    else
    {
    coll = (ListItem[])HttpContext.Current.Application["ControlTypeDropDown"];
    }

    this.Items.AddRange(coll);
    this.Items.FindByValue(DefaultValue).Selected=true;
    }
    }




    }
    }
    Joe, Nov 5, 2003
    #1
    1. Advertising

  2. Joe

    Teemu Keiski Guest

    Hi,

    If I got your code correct, you are using the value in the constructor of
    your control? That is probably too early as properties are set somewhere
    middle of instantiation and initialization of the control. So as an answer,
    put this code into overridden OnInit method in your control (that's where
    initialization should happen).

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "Joe" <> wrote in message
    news:...
    > I wrote a simple server control that inherits from the DropDownList
    > control. I will be using the control in an aspx page and want to
    > access it in a repeater. I have created a public property called
    > DefaultValue. For some reason, the property is never set from within
    > my aspx page. It just gets ignored. Does anyone know why?
    >
    > Here is how it looks in my aspx:
    > --------------------------------------------------------
    >
    > <frmcontrol:controltypedropdown DefaultValue="5"
    > id="Controltypedropdown1" runat="server"/>
    >
    > ----------------------------------------------------------
    > Below is my control code.
    > -----------------------------------------------------------
    >
    > namespace blah.Controls
    > {
    >
    > /// <summary>
    > /// A server control to display the dropdown of Control Types. Will
    > be
    > /// displayed programatically in a repeater.
    > /// </summary>
    > public class ControlTypeDropDown :
    > System.Web.UI.WebControls.DropDownList
    > {
    > private DataTable dt;
    > private ListItem[] coll;
    >
    > private string defaultValue="0";
    > public string DefaultValue
    > {
    > get{return defaultValue;}
    > set{defaultValue=value;}
    > }
    >
    > public ControlTypeDropDown() : base()
    > {
    > lock(this)
    > {
    > if(HttpContext.Current.Application["ControlTypeDropDown"]==null)
    > {
    > Survey s = new Survey();
    > dt = s.GetFormControls();
    >
    >
    > coll = new ListItem[dt.Rows.Count+1];
    > coll[0] = new ListItem("", "0");
    > for(int i=0;i<dt.Rows.Count;i++)
    > {
    > coll[i+1] = new ListItem(Convert.ToString(dt.Rows["type"]),
    > Convert.ToString(dt.Rows["controlTypeId"]));
    > }
    >
    > HttpContext.Current.Application.Add("ControlTypeDropDown", coll);
    > }
    > else
    > {
    > coll = (ListItem[])HttpContext.Current.Application["ControlTypeDropDown"];
    > }
    >
    > this.Items.AddRange(coll);
    > this.Items.FindByValue(DefaultValue).Selected=true;
    > }
    > }
    >
    >
    >
    >
    > }
    > }
    Teemu Keiski, Nov 6, 2003
    #2
    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. Matthew Louden
    Replies:
    1
    Views:
    6,872
    Scott M.
    Oct 11, 2003
  2. Rene Aichinger
    Replies:
    0
    Views:
    532
    Rene Aichinger
    Dec 7, 2005
  3. Ryan Liu
    Replies:
    0
    Views:
    500
    Ryan Liu
    Feb 21, 2009
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,397
    Nathan Sokalski
    Mar 4, 2010
  5. Samuel

    Update Property that mapped to a child control's property ?

    Samuel, Jan 23, 2006, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    175
    Steven Cheng[MSFT]
    Jan 24, 2006
Loading...

Share This Page