A
Alan Silver
Hello,
I'm trying to write a simple user control as an exercise (and 'cos it
might be useful). I am trying to do a simple date picker, which consists
of three drop down lists, one for the day, one for the month and one for
the year. I know this isn't earth-shattering stuff, but I'm still
learning, so please be patient!!
I'm having trouble with the properties of the control. I have created
the control OK and it displays fine. I added a method that sets the
range for the year drop down, and in the Form_Load event, I check to see
if it's been called, and if not, I use a default year range. All fine so
far.
Now, I wanted to add a property for the day, so that this could be set
or got. I did the following ...
public int Day {
get {
return System.Convert.ToInt32(drpDay.SelectedItem.Text);
}
set {
if (Day<=0 || Day>31) {
drpDay.SelectedIndex = 0;
} else {
drpDay.SelectedIndex = Day - 1;
}
}
}
where drpDay is the name of the drop down for the day.
In the page I was using to test this, I did the following ...
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
dtpDate.SetYearRange(1990, 2000);
dtpDate.Day = 3;
}
}
which gave a compile error on the line dtpDate.Day = 3;, saying "Object
reference not set to an instance of an object". I'm not sure what the
problem is as drpDate definitely exists, and the Day property exists and
is public.
Anyone able to explain what I did wrong? TIA. The full code is shown
below in case it is needed.
Thanks,
Alan
DatePicker.ascx
============
<script runat="server" Language="C#">
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
int i;
for (i=1; i<=31; i++) {
drpDay.Items.Add(i.ToString());
}
if (drpYear.Items.Count == 0) {
SetYearRange(DateTime.Now.Year, DateTime.Now.Year+10);
}
}
}
public void SetYearRange(int StartYear, int EndYear) {
int i;
for (i=StartYear; i<=EndYear; i++) {
drpYear.Items.Add(i.ToString());
}
}
public int Day {
get {
return System.Convert.ToInt32(drpDay.SelectedItem.Text);
}
set {
if (Day<=0 || Day>31) {
drpDay.SelectedIndex = 0;
} else {
drpDay.SelectedIndex = Day - 1;
}
}
}
</script>
<aspropDownList ID="drpDay" Runat="Server" /> <aspropDownList
ID="drpMonth" Runat="Server"><asp:ListItem Text="January"
/><asp:ListItem Text="February" /><asp:ListItem Text="March"
/><asp:ListItem Text="April" /><asp:ListItem Text="May" /><asp:ListItem
Text="June" /><asp:ListItem Text="July" /><asp:ListItem Text="August"
/><asp:ListItem Text="September" /><asp:ListItem Text="October"
/><asp:ListItem Text="November" /><asp:ListItem Text="December"
/></aspropDownList> <aspropDownList ID="drpYear" Runat="Server"
/>
TestDatePicker.aspx
===============
<%@ Page Language="C#" Debug="true" %>
<%@ Register TagPrefix="Pixata" TagName="DatePicker"
Src="DatePicker.ascx" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
dtpDate.SetYearRange(1990, 2000);
dtpDate.Day = 3;
}
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test of my date picker custom control</title>
</head>
<body>
<form runat="server">
<PixataatePicker ID="dtpDate" Runat="Server" />
</form>
</body>
</html>
I'm trying to write a simple user control as an exercise (and 'cos it
might be useful). I am trying to do a simple date picker, which consists
of three drop down lists, one for the day, one for the month and one for
the year. I know this isn't earth-shattering stuff, but I'm still
learning, so please be patient!!
I'm having trouble with the properties of the control. I have created
the control OK and it displays fine. I added a method that sets the
range for the year drop down, and in the Form_Load event, I check to see
if it's been called, and if not, I use a default year range. All fine so
far.
Now, I wanted to add a property for the day, so that this could be set
or got. I did the following ...
public int Day {
get {
return System.Convert.ToInt32(drpDay.SelectedItem.Text);
}
set {
if (Day<=0 || Day>31) {
drpDay.SelectedIndex = 0;
} else {
drpDay.SelectedIndex = Day - 1;
}
}
}
where drpDay is the name of the drop down for the day.
In the page I was using to test this, I did the following ...
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
dtpDate.SetYearRange(1990, 2000);
dtpDate.Day = 3;
}
}
which gave a compile error on the line dtpDate.Day = 3;, saying "Object
reference not set to an instance of an object". I'm not sure what the
problem is as drpDate definitely exists, and the Day property exists and
is public.
Anyone able to explain what I did wrong? TIA. The full code is shown
below in case it is needed.
Thanks,
Alan
DatePicker.ascx
============
<script runat="server" Language="C#">
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
int i;
for (i=1; i<=31; i++) {
drpDay.Items.Add(i.ToString());
}
if (drpYear.Items.Count == 0) {
SetYearRange(DateTime.Now.Year, DateTime.Now.Year+10);
}
}
}
public void SetYearRange(int StartYear, int EndYear) {
int i;
for (i=StartYear; i<=EndYear; i++) {
drpYear.Items.Add(i.ToString());
}
}
public int Day {
get {
return System.Convert.ToInt32(drpDay.SelectedItem.Text);
}
set {
if (Day<=0 || Day>31) {
drpDay.SelectedIndex = 0;
} else {
drpDay.SelectedIndex = Day - 1;
}
}
}
</script>
<aspropDownList ID="drpDay" Runat="Server" /> <aspropDownList
ID="drpMonth" Runat="Server"><asp:ListItem Text="January"
/><asp:ListItem Text="February" /><asp:ListItem Text="March"
/><asp:ListItem Text="April" /><asp:ListItem Text="May" /><asp:ListItem
Text="June" /><asp:ListItem Text="July" /><asp:ListItem Text="August"
/><asp:ListItem Text="September" /><asp:ListItem Text="October"
/><asp:ListItem Text="November" /><asp:ListItem Text="December"
/></aspropDownList> <aspropDownList ID="drpYear" Runat="Server"
/>
TestDatePicker.aspx
===============
<%@ Page Language="C#" Debug="true" %>
<%@ Register TagPrefix="Pixata" TagName="DatePicker"
Src="DatePicker.ascx" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
dtpDate.SetYearRange(1990, 2000);
dtpDate.Day = 3;
}
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test of my date picker custom control</title>
</head>
<body>
<form runat="server">
<PixataatePicker ID="dtpDate" Runat="Server" />
</form>
</body>
</html>