Control Guru Needed

Discussion in 'ASP .Net Building Controls' started by Joe, Jan 20, 2004.

  1. Joe

    Joe Guest

    I am having a hard time with setting a property on a custom server
    control that i am placing in a repeater. The control inherits from
    DropDownList. The property is being set in the repeater using the
    DataBinder.Eval method. I can write that value out to the screen,
    however, it is not available to the control. I think it is a problem
    with the databinding of the repeater making data available to the
    custom control too late.

    Below is a snippet of how the control is being setup in my aspx page.
    After that is the code for my control. I've tried to set that
    DefaultProperty in 2 different places but can't think of anything
    else.

    ----------------------------------
    ASPX
    ----------------------------------

    <asp:repeater id=rpt DataSource="<%#dt%>" Runat="server">

    <ItemTemplate>
    <frmControl:ControlTypeDropDown id="ddControl" runat="server"
    DefaultValue='<%#DataBinder.Eval(Container.DataItem,
    "controlTypeId")%>' /><Br>
    </ItemTemplate>
    </asp:Repeater>

    ----------------------------------
    Server Control
    ----------------------------------

    using System;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Caching;
    using System.Web.UI.WebControls;
    using System.Collections;

    namespace app.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 string defaultValue;
    public string DefaultValue
    {
    get{return defaultValue;}
    set{defaultValue=value;}
    }

    public ControlTypeDropDown() : base(){}


    protected override void OnInit(System.EventArgs e)
    {

    if(base.EnableViewState && !Page.IsPostBack)
    {
    //If using ViewState, only DataBind the FIRST time the page is
    loaded.
    this.DataBind();

    }
    else if(!base.EnableViewState && !Page.IsPostBack)
    {
    //If not using ViewState, DataBind EVERY time the page is loaded.
    //Setup();
    this.DataBind();
    }



    }



    protected override void RenderContents(System.Web.UI.HtmlTextWriter
    writer)
    {
    if(DefaultValue != null && DefaultValue != "")
    {
    base.Items.FindByValue(DefaultValue).Selected=true;
    }

    base.RenderContents(writer);



    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
    if(DefaultValue != null && DefaultValue != "")
    {
    base.Items.FindByValue(DefaultValue).Selected=true;
    }

    base.Render(writer);
    }

    protected override void OnLoad(System.EventArgs e)
    {
    if(base.EnableViewState)
    base.DataBind();

    base.Attributes.Add("onChange", "openWin(this)");
    }




    public override void DataBind()
    {
    SortedList ht;
    if(HttpContext.Current.Application["ControlTypeDropDown"]==null)
    {
    TS s = new TS();
    ht = s.GetFormControls();

    HttpContext.Current.Application.Add("ControlTypeDropDown", ht);
    }
    else
    {

    ht = (SortedList)HttpContext.Current.Application["ControlTypeDropDown"];
    }

    base.DataSource = ht;
    base.DataTextField = "value";
    base.DataValueField = "key";
    base.DataBind();

    }
    }
    }
    Joe, Jan 20, 2004
    #1
    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. Efy.
    Replies:
    2
    Views:
    1,081
  2. Joe

    Control Guru Needed

    Joe, Jan 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    357
  3. John Thompson

    ASP.NET Image Upload... Guru needed

    John Thompson, Jun 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,136
    Steve C. Orr [MVP, MCSD]
    Jun 30, 2004
  4. Andreas Klemt

    Regular Expressions Guru needed. Please help!

    Andreas Klemt, Aug 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    280
    Andreas Klemt
    Aug 18, 2004
  5. Joe

    Control Guru Needed

    Joe, Jan 20, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    113
Loading...

Share This Page