J
Joe
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();
}
}
}
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();
}
}
}