问题是: 在回发时,该表没有动态创建的行,rowcount为0。
我知道有很多代码,但它是我认为必须发布的最少的代码,因此回答者可以调试。请注意我已经努力但无法得到解决方案。
以下是代码:
public partial class DaySelection : System.Web.UI.Page
{
Table table1 = new Table();
Button button1 = new Button();
string the_id_checkbox;
string the_id_label;
//The need of the table ENDS
DropDownList selectdays = new DropDownList();
Label theselecteddate = new Label();
Button extract_the_selected = new Button();
Literal selected_values=new Literal();
int number_of_row = -1;
protected void Page_Load(object sender, EventArgs e)
{
CheckBox check_or_not = new CheckBox();
try
{
selected_values.Text = "";
form1.Controls.Remove(selected_values);
form1.Page.Response.Write("inside try");
for (int i = 0; i < table1.Rows.Count; i++)
{
Response.Write("inside for");
the_id_checkbox = "checkmate" + i;
the_id_label = "The_text" + i;
check_or_not = (CheckBox)table1.Rows[i].FindControl(the_id_checkbox);
if (check_or_not.Checked == true)
{
form1.Page.Response.Write("inside if");
selected_values.Text = selected_values.Text + "<br /> " + check_or_not.Checked.ToString();
selected_values.Text = selected_values.Text + " and the day is: " + ((Label)table1.Rows[i].FindControl(the_id_label)).Text;
}
else
{
Response.Write(" selection no detect");
}
}
form1.Controls.AddAt(1, selected_values);
Response.Write(selected_values.Text);
}
catch (NullReferenceException nn)
{
form1.Page.Response.Write("inside catch" + nn.Message.ToString() + nn.StackTrace);
}
extract_the_selected.Text = "Extract it";
form1.Controls.AddAt(2,extract_the_selected);
selectdays.AutoPostBack = true;
ArrayList thedays = new ArrayList();
thedays.Add("Monday" + DateTime.Now);
thedays.Add("Tuesday");
thedays.Add("Wednesday");
thedays.Add("Thursday");
thedays.Add("Friday");
thedays.Add("Saturday");
thedays.Add("Sunday");
selectdays.DataSource = thedays;
selectdays.DataBind();
form1.Controls.AddAt(3,selectdays);
Calendar1.SelectionChanged += new EventHandler(Calendar1_SelectionChanged);
}
void Calendar1_SelectionChanged(object sender, EventArgs e)
{
DateTime startdate;
string month;
month = Calendar1.SelectMonthText;
startdate = Calendar1.SelectedDate;
days_date(startdate);
}
void selectdays_SelectedIndexChanged(object sender, EventArgs e)
{
display_dates_of_day(DateTime.Parse("9-1-2010"), DateTime.Parse("9-30-2010"), selectdays.SelectedItem.Text);
}
public void days_date(DateTime startdate)
{
int noofdays;
DateTime enddate = new DateTime();
noofdays = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
enddate = startdate.AddDays(noofdays);
Response.Write("<br /> end date is " + enddate);
Response.Write("<br /> start date is " + startdate);
display_dates_of_day( startdate, enddate, selectdays.SelectedItem.Text);
}
void display_dates_of_day(DateTime startDate, DateTime endDate, string selectedday)
{
int Count = 0;
for (DateTime dt = startDate; dt <= endDate; dt = dt.AddDays(1.0))
{
if (dt.DayOfWeek.ToString() == selectedday)
{
table1.ID = "table1";
number_of_row = number_of_row + 1;
string date = dt.Date.ToString("dd-MMMM-yyyy");
for (int adding_rows = 0; adding_rows < 1; adding_rows++)
{
TableRow table_row1 = new TableRow();
TableCell table_cell1 = new TableCell();
TableCell table_cell2 = new TableCell();
Label The_label = new Label();
CheckBox checkmate = new CheckBox();
The_label.Text = date + " (<---date)" + number_of_row;
the_id_checkbox = "checkmate" + number_of_row;
checkmate.ID = the_id_checkbox;
the_id_label = "The_text" + number_of_row;
The_label.ID = the_id_label;
table_cell2.Controls.Add(checkmate);
table_cell1.Controls.Add(The_label);
table_row1.Controls.AddAt(0, table_cell1);
table_row1.Controls.AddAt(1, table_cell2);
table1.Rows.Add(table_row1);
}
button1.Text = "click me to export the value";
form1.Controls.Add(table1);
form1.Controls.AddAt(1, selected_values);
Count++;
}
}
Response.Write("<br /> The count of days by traversing: " + Count);
}
}
答案 0 :(得分:3)
您看到这种看似“奇怪”的行为的原因是您正在动态构建Table1
的内容并将其添加到.Controls
中的display_dates_of_day
个网页集中方法,由:
selectdays_SelectedIndexChanged
Calendar1_SelectionChanged
(间接)这意味着当您在回发后重新构建页面时,控件不存在。如果您在浏览器中“查看源代码”,您会发现在单击按钮触发回发后,您无法在标记中找到字符串“Table1”,但如果您在单击日期后执行此操作日历,你可以。那是因为在“点击按钮”方案中,控件永远不会填充并添加到页面
我会提出一些建议来解决这个问题并解决问题:
Table1
成为.aspx
页面中声明的控件。