Error rendering Custom Web Control in VS.NET ide

P

pabloazorin

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:IT24DateTime id="IT24DateTime1" runat="server"
Type="Date"></cc1:IT24DateTime>

.... 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:IT24DateTime id="IT24DateTime1" runat="server" Type="Date">
<asp:TableRow>
<asp:TableCell>
<asp:TextBox runat="server" Width="80px" CssClass="inputText"
ID="txtdate_IT24DateTime1" MaxLength="10"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:Image runat="server" ImageUrl="Images/Calendar.gif"
OnClick="popUpCalendar(document.getElementById('txtdate_IT24DateTime1'),
document.getElementById('txtdate_IT24DateTime1'), 'mm/dd/yyyy');"
style="cursor:pointer;"></asp:Image>
</asp:TableCell>
<asp:TableCell>
<asp:DropDownList runat="server" CssClass="dropdown">
<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:DropDownList>
</asp:TableCell>
</asp:TableRow>
</cc1:IT24DateTime>

.... 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:IT24DateTime id="IT24DateTime1" runat="server"
Type="Date"></cc1:IT24DateTime>

Here is my web control:

/// <summary>
/// Summary description for TextBoxColumn.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:IT24DateTime runat=server></{0}:IT24DateTime>"),
ToolboxBitmapAttribute(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 _requireValidation = false;
public bool RequireValidation
{
get { return _requireValidation; }
set { _requireValidation = 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("Appearance"),
Description("Type of Control. Date and Time, Date, or Time.")
]
private Date_Control_Type _date_Control_Type =
Date_Control_Type.DateTime;
public Date_Control_Type Type
{
get { return _date_Control_Type; }
set { _date_Control_Type = value; }
}

private DateTime _value = DateTime.Now;
public DateTime Value
{
get
{
//Make sure the child controls are accessible
this.EnsureChildControls();
if(Rows.Count>0)
{
DateTime dt = new DateTime();
switch(_date_Control_Type)
{
case Date_Control_Type.Date:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Controls[0];
dt = Utilities.GetDateTime(date.Text);
break;
}
case Date_Control_Type.Time:
{
// Get Time Control from container table
TableCell tc = Rows[0].Cells[0];
DropDownList time = (DropDownList)tc.Controls[0];
dt =
DateTime.Now.AddHours(Convert.ToDouble(time.SelectedValue));
break;
}
case Date_Control_Type.DateTime:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Controls[0];

// Get Time Control from container table
tc = Rows[0].Cells[2];
DropDownList time = (DropDownList)tc.Controls[0];

if(!time.SelectedValue.Equals(string.Empty))
{
dt = Utilities.GetDateTime(date.Text,time.SelectedValue);
}
else
{
dt = Utilities.GetDateTime(date.Text);
}
break;
}
}
return dt;
}
return DateTime.Now;
}
set
{
//Make sure the child controls are accessible
this.EnsureChildControls();
_value = value;

if(Rows.Count>0)
{
string strDate = _value.ToString(_dateFormat);
string strTime =
_value.AddMinutes(60-_value.Minute).ToString(_timeFormat);

switch(_date_Control_Type)
{
case Date_Control_Type.Date:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Controls[0];
date.Text = strDate;
break;
}
case Date_Control_Type.Time:
{
// Get Time Control from container table
TableCell tc = Rows[0].Cells[0];
DropDownList time = (DropDownList)tc.Controls[0];
setValueInDropdown(ref time, strTime);
break;
}
case Date_Control_Type.DateTime:
{
// Get Date Control from container table
TableCell tc = Rows[0].Cells[0];
TextBox date = (TextBox)tc.Controls[0];
date.Text = strDate;

// Get Time Control from container table
tc = Rows[0].Cells[2];
DropDownList time = (DropDownList)tc.Controls[0];
setValueInDropdown(ref time, strTime);

break;
}
}
}
}
}

protected override void OnPreRender(EventArgs e)
{
if (!Page.IsClientScriptBlockRegistered("jsCalendar"))
{
Page.RegisterClientScriptBlock("jsCalendar", placeJavascript());
}
base.OnPreRender(e);
}

protected override void Render(HtmlTextWriter output)
{
// Render base object that contains all controls
if ((Context==null) || ((Site != null) && Site.DesignMode))
return;
base.Render(output);
}

protected override void OnInit(EventArgs e)
{
TableRow tr = new TableRow();
TableCell tc;

//string date = _dateTime.ToString(_dateFormat);
//string time =
_dateTime.AddMinutes(60-_dateTime.Minute).ToString(_timeFormat);

if(_date_Control_Type.Equals(Date_Control_Type.DateTime)
|| _date_Control_Type.Equals(Date_Control_Type.Date))
{
TextBox txtDate = new TextBox();
txtDate.ID = "txtdate_" + ID;
txtDate.MaxLength = 10;
txtDate.Width = Unit.Pixel(80);
txtDate.CssClass = _dateCssClass;
//txtDate.Text = date;

tc = new TableCell();
tc.Controls.Add(txtDate);
tr.Cells.Add(tc);

Image imgCalendar = new Image();
imgCalendar.ImageUrl = _calendarImage;
imgCalendar.Attributes.Add("OnClick",
"popUpCalendar(document.getElementById('" + txtDate.ID + "'),
document.getElementById('" + txtDate.ID + "'), '" +
_dateFormat.ToLower() + "');");
imgCalendar.Style.Add("cursor", "pointer");

tc = new TableCell();
tc.Controls.Add(imgCalendar);
tr.Cells.Add(tc);
}

if(_date_Control_Type.Equals(Date_Control_Type.DateTime)
|| _date_Control_Type.Equals(Date_Control_Type.Time))
{
DropDownList cmbMinutes = new DropDownList();
cmbMinutes.CssClass = _timeCssClass;
loadMinutes(ref cmbMinutes);
//setValueInDropdown(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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top