I developed a Date Picker web control using C# and .net framework 1.1
I added my control to Visual Studio 2003 IDE toolbar.
When I drag and drop my control to design web page, the control renders
correctly.
If I change to HTML the generated html is
<cc1:IT24DateTi me id="IT24DateTim e1" runat="server"
Type="Date"></cc1:IT24DateTim e>
.... and that works correctly.
The problem is when in design time I change some property of my
control.
For example I changed the control property "Type" from "Date" to
"DateTime". In design time is all right.
When I change to HTML appears the rendered html inside the web control
tag:
<cc1:IT24DateTi me id="IT24DateTim e1" runat="server" Type="Date">
<asp:TableRow >
<asp:TableCel l>
<asp:TextBox runat="server" Width="80px" CssClass="input Text"
ID="txtdate_IT2 4DateTime1" MaxLength="10"> </asp:TextBox>
</asp:TableCell>
<asp:TableCel l>
<asp:Image runat="server" ImageUrl="Image s/Calendar.gif"
OnClick="popUpC alendar(documen t.getElementByI d('txtdate_IT24 DateTime1'),
document.getEle mentById('txtda te_IT24DateTime 1'), 'mm/dd/yyyy');"
style="cursor:p ointer;"></asp:Image>
</asp:TableCell>
<asp:TableCel l>
<asp:DropDownLi st runat="server" CssClass="dropd own">
<asp:ListItem Selected="True" ></asp:ListItem>
<asp:ListItem Value="0">00:00 AM</asp:ListItem>
<asp:ListItem Value="1">01:00 AM</asp:ListItem>
<asp:ListItem Value="2">02:00 AM</asp:ListItem>
<asp:ListItem Value="3">03:00 AM</asp:ListItem>
<asp:ListItem Value="4">04:00 AM</asp:ListItem>
</asp:DropDownLis t>
</asp:TableCell>
</asp:TableRow>
</cc1:IT24DateTim e>
.... and when I run the application, generate duplicated controls.
My question is, how can I do to avoid the control doesn't render the
controls when I change somo property?
There exist any way to protect it?
I allways would like to see in html
<cc1:IT24DateTi me id="IT24DateTim e1" runat="server"
Type="Date"></cc1:IT24DateTim e>
Here is my web control:
/// <summary>
/// Summary description for TextBoxColumn.
/// </summary>
[DefaultProperty ("Text"),
ToolboxData("<{ 0}:IT24DateTime runat=server></{0}:IT24DateTim e>"),
ToolboxBitmapAt tribute(typeof( Calendar))]
public class IT24DateTime : Table
{
private string _imgDirectory = "Images/Calendar/";
public string ImageDirectory
{
set { _imgDirectory = value; }
}
private string _error_CssClass = string.Empty;
public string ErrorCssClass
{
get { return _error_CssClass ; }
set { _error_CssClass = value; }
}
private string _separator = string.Empty;
public string Separator
{
get { return _separator; }
set { _separator = value; }
}
private bool _requireValidat ion = false;
public bool RequireValidati on
{
get { return _requireValidat ion; }
set { _requireValidat ion = value; }
}
private string _dateFormat = "MM/dd/yyyy";
public string DateFormat
{
get { return _dateFormat; }
set { _dateFormat = value; }
}
private string _timeFormat = "hh:mm tt";
public string TimeFormat
{
get { return _timeFormat; }
set { _timeFormat = value; }
}
private string _dateCssClass = "inputText" ;
public string DateCssClass
{
set { _dateCssClass = value; }
}
private string _timeCssClass = "dropdown";
public string TimeCssClass
{
set { _timeCssClass = value; }
}
private string _calendarImage = "Images/Calendar.gif";
public string CalendarImage
{
get { return _calendarImage; }
set { _calendarImage = value; }
}
[
Bindable(true),
Category("Appea rance"),
Description("Ty pe of Control. Date and Time, Date, or Time.")
]
private Date_Control_Ty pe _date_Control_T ype =
Date_Control_Ty pe.DateTime;
public Date_Control_Ty pe Type
{
get { return _date_Control_T ype; }
set { _date_Control_T ype = value; }
}
private DateTime _value = DateTime.Now;
public DateTime Value
{
get
{
//Make sure the child controls are accessible
this.EnsureChil dControls();
if(Rows.Count>0 )
{
DateTime dt = new DateTime();
switch(_date_Co ntrol_Type)
{
case Date_Control_Ty pe.Date:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Con trols[0];
dt = Utilities.GetDa teTime(date.Tex t);
break;
}
case Date_Control_Ty pe.Time:
{
// Get Time Control from container table
TableCell tc = Rows[0].Cells[0];
DropDownList time = (DropDownList)t c.Controls[0];
dt =
DateTime.Now.Ad dHours(Convert. ToDouble(time.S electedValue));
break;
}
case Date_Control_Ty pe.DateTime:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Con trols[0];
// Get Time Control from container table
tc = Rows[0].Cells[2];
DropDownList time = (DropDownList)t c.Controls[0];
if(!time.Select edValue.Equals( string.Empty))
{
dt = Utilities.GetDa teTime(date.Tex t,time.Selected Value);
}
else
{
dt = Utilities.GetDa teTime(date.Tex t);
}
break;
}
}
return dt;
}
return DateTime.Now;
}
set
{
//Make sure the child controls are accessible
this.EnsureChil dControls();
_value = value;
if(Rows.Count>0 )
{
string strDate = _value.ToString (_dateFormat);
string strTime =
_value.AddMinut es(60-_value.Minute). ToString(_timeF ormat);
switch(_date_Co ntrol_Type)
{
case Date_Control_Ty pe.Date:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Con trols[0];
date.Text = strDate;
break;
}
case Date_Control_Ty pe.Time:
{
// Get Time Control from container table
TableCell tc = Rows[0].Cells[0];
DropDownList time = (DropDownList)t c.Controls[0];
setValueInDropd own(ref time, strTime);
break;
}
case Date_Control_Ty pe.DateTime:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Con trols[0];
date.Text = strDate;
// Get Time Control from container table
tc = Rows[0].Cells[2];
DropDownList time = (DropDownList)t c.Controls[0];
setValueInDropd own(ref time, strTime);
break;
}
}
}
}
}
protected override void OnPreRender(Eve ntArgs e)
{
if (!Page.IsClient ScriptBlockRegi stered("jsCalen dar"))
{
Page.RegisterCl ientScriptBlock ("jsCalendar ", placeJavascript ());
}
base.OnPreRende r(e);
}
protected override void Render(HtmlText Writer output)
{
// Render base object that contains all controls
if ((Context==null ) || ((Site != null) && Site.DesignMode ))
return;
base.Render(out put);
}
protected override void OnInit(EventArg s e)
{
TableRow tr = new TableRow();
TableCell tc;
//string date = _dateTime.ToStr ing(_dateFormat );
//string time =
_dateTime.AddMi nutes(60-_dateTime.Minut e).ToString(_ti meFormat);
if(_date_Contro l_Type.Equals(D ate_Control_Typ e.DateTime)
|| _date_Control_T ype.Equals(Date _Control_Type.D ate))
{
TextBox txtDate = new TextBox();
txtDate.ID = "txtdate_" + ID;
txtDate.MaxLeng th = 10;
txtDate.Width = Unit.Pixel(80);
txtDate.CssClas s = _dateCssClass;
//txtDate.Text = date;
tc = new TableCell();
tc.Controls.Add (txtDate);
tr.Cells.Add(tc );
Image imgCalendar = new Image();
imgCalendar.Ima geUrl = _calendarImage;
imgCalendar.Att ributes.Add("On Click",
"popUpCalendar( document.getEle mentById('" + txtDate.ID + "'),
document.getEle mentById('" + txtDate.ID + "'), '" +
_dateFormat.ToL ower() + "');");
imgCalendar.Sty le.Add("cursor" , "pointer");
tc = new TableCell();
tc.Controls.Add (imgCalendar);
tr.Cells.Add(tc );
}
if(_date_Contro l_Type.Equals(D ate_Control_Typ e.DateTime)
|| _date_Control_T ype.Equals(Date _Control_Type.T ime))
{
DropDownList cmbMinutes = new DropDownList();
cmbMinutes.CssC lass = _timeCssClass;
loadMinutes(ref cmbMinutes);
//setValueInDropd own(ref cmbMinutes, time);
tc = new TableCell();
tc.Controls.Add (cmbMinutes);
tr.Cells.Add(tc );
}
Rows.Add(tr);
}
Any support or help would be appreciated.
Thank you to everyone...
Regards,
Pablo
pa*********@gma il.com