I had a bug that drove me crazy: in one of my ASP.NET pages, none of the dynamic control worked, meaning when I clicked a button, nothing happened, and the callback function wasn't called.
I suspected AJAX, but even after I removed all traces of AJAX in the page, the controls still didn't work.
I started removing code from the page, until all the code-behind file was almost empty, to no avail.
So I went to the .aspx file, and started removing controls from the page.
Until I removed one control, and suddenly my dynamic controls started working fine.
The control that caused all the problem was a required field validation.
My error was that I forgot to assign a ValidationGroup property to the validator. Since it didn't have any validation group, it got activated every time any control was clicked on the page, and caused the action to fail using client call, before any code got to the server code-behind of the page.
I hope this will save some time for another programmer with similar problem.
Showing posts with label Dynamic Controls. Show all posts
Showing posts with label Dynamic Controls. Show all posts
Monday, April 26, 2010
Sunday, February 15, 2009
A simple date control
I needed a simple date control in my ASP.NET C# application. The calendar control provided with ASP.NET didn't work well for me because I wanted to make it easy to enter any year without the need to navigate through the years in the calendar. After I couldn't find such a control in a quick search I wrote a class that creates the controls I need dynamically.
I have a drop-down control for the day, with values 1 to 31, another drop down for the month, from "Jan" to "Dec", and a text control for the year.
Here is the class:
public class DateControl
{
static List _days = new List();
static List _months = new List();
public DateControl()
{
// fill days list
for (int i = 1; i <= 31; i++)
_days.Add(i.ToString());
// fill months list
_months.Add("Jan");
_months.Add("Feb");
_months.Add("Mar");
_months.Add("Apr");
_months.Add("May");
_months.Add("Jun");
_months.Add("Jul");
_months.Add("Aug");
_months.Add("Sep");
_months.Add("Oct");
_months.Add("Nov");
_months.Add("Dec");
} // DateControl
public void Create(Control container, string pref, string ID,
DateTime dateStart)
{
// day drop down
DropDownList dropDay = new DropDownList();
dropDay.ID = pref + "_drop_day_" + ID;
foreach (string strDay in _days)
dropDay.Items.Add(new ListItem(strDay));
dropDay.SelectedIndex = dateStart.Day - 1;
container.Controls.Add(dropDay);
// space
Literal li = new Literal();
li.Text = " ";
container.Controls.Add(li);
// months
DropDownList dropMonth = new DropDownList();
dropMonth.ID = pref + "_drop_month_" + ID;
foreach (string strMonth in _months)
dropMonth.Items.Add(new ListItem(strMonth));
dropMonth.SelectedIndex = dateStart.Month - 1;
container.Controls.Add(dropMonth);
// space
li = new Literal();
li.Text = " ";
container.Controls.Add(li);
// year
TextBox txt = new TextBox();
txt.ID = pref + "_txt_year_" + ID;
txt.MaxLength = 5;
txt.Columns = 5;
txt.Text = dateStart.Year.ToString();
container.Controls.Add(txt);
} // Create
public void Create(Control container, string pref, string ID)
{
Create(container, pref, ID, DateTime.Today);
}
And here is an example of how to use it:
DateControl date = new DateControl();
date.Create(panel, "prefix", uniq_ID, initialDate);
Where panel is a Panel control that will contain the date controls, "prefix" is some prefix all 3 control ID's will have, uniq_ID is some string that will appear at the end of each control's ID, and initialDate is a DateTime variable with the initial date value of the controls. This variable can be omitted and then the controls will have the initial value of today's date.
I have a drop-down control for the day, with values 1 to 31, another drop down for the month, from "Jan" to "Dec", and a text control for the year.
Here is the class:
public class DateControl
{
static List
static List
public DateControl()
{
// fill days list
for (int i = 1; i <= 31; i++)
_days.Add(i.ToString());
// fill months list
_months.Add("Jan");
_months.Add("Feb");
_months.Add("Mar");
_months.Add("Apr");
_months.Add("May");
_months.Add("Jun");
_months.Add("Jul");
_months.Add("Aug");
_months.Add("Sep");
_months.Add("Oct");
_months.Add("Nov");
_months.Add("Dec");
} // DateControl
public void Create(Control container, string pref, string ID,
DateTime dateStart)
{
// day drop down
DropDownList dropDay = new DropDownList();
dropDay.ID = pref + "_drop_day_" + ID;
foreach (string strDay in _days)
dropDay.Items.Add(new ListItem(strDay));
dropDay.SelectedIndex = dateStart.Day - 1;
container.Controls.Add(dropDay);
// space
Literal li = new Literal();
li.Text = " ";
container.Controls.Add(li);
// months
DropDownList dropMonth = new DropDownList();
dropMonth.ID = pref + "_drop_month_" + ID;
foreach (string strMonth in _months)
dropMonth.Items.Add(new ListItem(strMonth));
dropMonth.SelectedIndex = dateStart.Month - 1;
container.Controls.Add(dropMonth);
// space
li = new Literal();
li.Text = " ";
container.Controls.Add(li);
// year
TextBox txt = new TextBox();
txt.ID = pref + "_txt_year_" + ID;
txt.MaxLength = 5;
txt.Columns = 5;
txt.Text = dateStart.Year.ToString();
container.Controls.Add(txt);
} // Create
public void Create(Control container, string pref, string ID)
{
Create(container, pref, ID, DateTime.Today);
}
And here is an example of how to use it:
DateControl date = new DateControl();
date.Create(panel, "prefix", uniq_ID, initialDate);
Where panel is a Panel control that will contain the date controls, "prefix" is some prefix all 3 control ID's will have, uniq_ID is some string that will appear at the end of each control's ID, and initialDate is a DateTime variable with the initial date value of the controls. This variable can be omitted and then the controls will have the initial value of today's date.
Wednesday, February 11, 2009
Where to craete dymanically created controls in ASP.NET web site?
I have an AST.NET web site with controls I create dynamically according to data I read from the database.
At first I created the controls in the Page_Load event handler. I had a strange problem: sometimes the control's event handler was not called. This happened with check box controls, and mysteriously only the uncheck event of the first checkbox control was not handled.
I found this article about creating dynamic controls:
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
And moved the call to the function that creates these controls to the Page_Init event handler of my page. Now all events are handled correctly.
At first I created the controls in the Page_Load event handler. I had a strange problem: sometimes the control's event handler was not called. This happened with check box controls, and mysteriously only the uncheck event of the first checkbox control was not handled.
I found this article about creating dynamic controls:
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
And moved the call to the function that creates these controls to the Page_Init event handler of my page. Now all events are handled correctly.
Labels:
ASP.NET,
Check Box,
Dynamic Controls,
Page_Init,
Page_Load
Subscribe to:
Posts (Atom)