Dropdownlist getting deleted.
Hello All ,
I am writing this code to generate dropdownlist for a month and year and then 31 checkboxes for each day of the month. There are 6 rows like this. After that i wanted that as soon as a user selects month and year all the checkboxes for sundays of that particular month should get disabled and replaced by "s" for that i had added label also and it worked but it is giving strange behavious when the first day of a month is Sunday for example for month = 5 and year = 2011 it displays right for that row but as soon as i select another month in the next row ddl of first row month gets deleted and moreover checkbox of 1 which was disabled also get visible.
Please can any one help me out to resolve this issue ? thanks in advance. Below is the code which i am using.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 6; i++)
{
TableRow t1 = new TableRow();
tbltemp.Rows.Add(t1);
for (int j = 0; j <= 31; j++)
{
TableCell tc = new TableCell();
if (i == 0 && j == 0)
{
tc.Text = "Month / Year";
t1.Cells.Add(tc);
}
else if (i == 0)
{
tc.Text = Convert.ToString(j);
t1.Cells.Add(tc);
}
else if (j != 0)
{
CheckBox chk = new CheckBox();
chk.ID = "chk" + i + j;
chk.Enabled = true;
Label lbl = new Label();
lbl.ID = "lbl" + i + j;
lbl.Text = "";
lbl.Visible = false;
tc.Controls.Add(chk);
tc.Controls.Add(lbl);
t1.Cells.Add(tc);
}
if (i > 0 && j == 0)
{
DropDownList ddl1 = new DropDownList();
ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
ddl1.AutoPostBack = true;
ddl1.ID = "ddlMonth" + i + "1";
ddl1.Items.Add("Select");
for (int k = 1; k <= 12; k++)
ddl1.Items.Add(k.ToString());
tc.Controls.Add(ddl1);
t1.Cells.Add(tc);
DropDownList ddl2 = new DropDownList();
ddl2.SelectedIndexChanged += new EventHandler(ddl2_SelectedIndexChanged);
ddl2.AutoPostBack = true;
ddl2.ID = "ddlYear" + i + "2";
int cyear = DateTime.Now.Year; ;
ddl2.Items.Add("Select");
for (int x = cyear; x <= cyear + 10; x++)
ddl2.Items.Add(x.ToString());
tc.Controls.Add(ddl2);
t1.Cells.Add(tc);
}
}
}
protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlY = (DropDownList)sender;
string ddlname = ddlY.ID.ToString();
string abc = ddlname.Substring(7, 1);
int a = Convert.ToInt32(abc);
string month = "ddlMonth" + a + "1";
DropDownList ddlM = (DropDownList)tbltemp.FindControl(month);
if (ddlM.SelectedValue.ToString() != "Select" && ddlY.SelectedValue.ToString() != "Select")
{
int intMonth = Convert.ToInt32(ddlM.SelectedValue);
int intYear = Convert.ToInt32(ddlY.SelectedValue);
MarkSundays(intMonth, intYear, a);
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlM = (DropDownList)sender;
string ddlname = ddlM.ID.ToString();
string abc = ddlname.Substring(8, 1);
int a = Convert.ToInt32(abc);
string year = "ddlYear" + a + "2";
DropDownList ddlY = (DropDownList)tbltemp.FindControl(year);
if (ddlM.SelectedValue.ToString() != "Select" && ddlY.SelectedValue.ToString() != "Select")
{
int intMonth = Convert.ToInt32(ddlM.SelectedValue);
int intYear = Convert.ToInt32(ddlY.SelectedValue);
MarkSundays(intMonth, intYear, a);
}
}
public void MarkSundays(int Month, int Year,int a)
{
int Month1 = Month;
int Year1 = Year;
int a1 = a;
Label l1 = new Label();
l1.Text = "S";
int intDaysThisMonth = DateTime.DaysInMonth(Year1, Month1);
DateTime oBeginnngOfThisMonth = new DateTime(Year1, Month1, 1);
for (int z = 0; z < intDaysThisMonth ; z++)
{
if (oBeginnngOfThisMonth.AddDays(z).DayOfWeek == DayOfWeek.Sunday)
{
int x =z + 1;
string c = "chk" + a1 + x;
string l = "lbl" + a1 + x;
CheckBox chktemp = (CheckBox)tbltemp.FindControl(c);
chktemp.Visible = false;
//chktemp.Enabled = false;
// chktemp.Text = "S";
}
}
}
}
|