开发者

Why do the rows of Table1 on post back become null?

开发者 https://www.devze.com 2023-01-14 05:44 出处:网络
The problem is: On postback, the table does not have the rows that were dynamically created, the rowcount is 0.

The problem is: On postback, the table does not have the rows that were dynamically created, the rowcount is 0.

  • Click on the button, it should detect the checked checkboxes within the table dynamically generated.
  • The table is made by code when the "day" is selected using the drop down list and "the starting date" is selected using the calender.

I know there's a lot of code, but it's the least I thought I had to post, so answerers can debug. Please note I have tried hard but cannot get the solution to this.

Here’s the code:

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);
    }
}


The reason you're seeing this seemingly "strange" behaviour is that you're dynamically constructing the contents of Table1 and adding it to the pages .Controls collection only in the display_dates_of_day method, which is called by:

  • selectdays_SelectedIndexChanged
  • Calendar1_SelectionChanged (indirectly)

This means that when your page is re-contructed on post-back, the controls don't exist. If you "View Source" in your browser, you'll find that after clicking the button to trigger a post-back you can't find the string "Table1" in the markup, but if you do it after clicking on a date in the calendar, you can. That's because in the "clicking the button" scenario, the control is never populated and added to the page

I'd make a few suggestions to get your head round this and solve the problem:

  1. Start with a much simplified version of this to help you understand the asp.net page lifecycle and how it impacts on what you're doing.
  2. Try to ensure your code adds as few controls as possible to the page dynamically as this makes things a lot simpler. i.e. Make Table1 a control that's declared in the .aspx page.
0

精彩评论

暂无评论...
验证码 换一张
取 消