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 C#. Show all posts
Showing posts with label C#. Show all posts
Monday, April 26, 2010
Monday, February 16, 2009
Regular Expressions in ASP.NET
I worked with PERL for a long time and of course I used the power of regular expressions intensively. I was glad to read that ASP.NET has built-in support for regular expressions, but after reading about it I realized I can say bye-bye to the elegant one-line commands I am used to from PERL.
After reading documentations, examples, and playing with the code myself, I can finally understand how to achieve what was so easy to do in PERL using the class hierarchy of ASP.NET.
This post is intended for people who know what are regular expressions, and want to understand how to implement them in C#.
This is what I needed to do:
I had a string consisting of some prefix, underscore and then a number: {prefix}_{num}
I wanted to get the number from that string.
The PERL way would be something like
my ($num) = $my_string =~ /_([0-9]+)$/
That's it... so simple.
Now this is how I did it in C#:
Match match = Regex.Match(my_string, @"_(?<num>[0-9]+)$");
int num = Int32.Parse(match.Groups["num"].ToString());
I used named grouping - I called the group I was looking for by a name, "num".
First I tried it without naming - omitting the ? from the pattern, and look for the first group: match.Groups[0] but what I got was the whole _ string, not only the number I was looking for. When I used named grouping I managed to get only the number, but it puzzled me, so I looked at it a little more. After some digging I found out that the first group - Group[0] - is the whole match, and the variables start at Group[1], so if I didn't want to use named groups, I would have to use Group[1] instead of Group[0].
It is quite confusing (and not really documented, at least where I was looking) so it seems clearer to me to remain with the named groups.
After reading documentations, examples, and playing with the code myself, I can finally understand how to achieve what was so easy to do in PERL using the class hierarchy of ASP.NET.
This post is intended for people who know what are regular expressions, and want to understand how to implement them in C#.
This is what I needed to do:
I had a string consisting of some prefix, underscore and then a number: {prefix}
I wanted to get the number from that string.
The PERL way would be something like
my ($num) = $my_string =~ /_([0-9]+)$/
That's it... so simple.
Now this is how I did it in C#:
Match match = Regex.Match(my_string, @"_(?<num>[0-9]+)$");
int num = Int32.Parse(match.Groups["num"].ToString());
I used named grouping - I called the group I was looking for by a name, "num".
First I tried it without naming - omitting the ?
It is quite confusing (and not really documented, at least where I was looking) so it seems clearer to me to remain with the named groups.
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.
Subscribe to:
Comments (Atom)