views:

50

answers:

0
  • select day using the drop down list
  • select the start date using calender (causes an event), a table is made having the dates of the day selected from the start date and shown to the user.
  • the table also contains checkbox for each row in a cell
  • the user selects the check boxes he wants
  • and clicks the button
  • the button calls the method in the .cs file which SHOULD TRAVERSE THE TABLE ROWS TO FIND THE ticked checked boxes and the rest is self explanatory

the problem

on calender selection change event nothing is lost,

but on button click event all things in table created by code is lost. (as the debugger shows that the row count is zero).

i understand that in the page cycle the rows etc added the form by the date change event of the calender are not preserved. fine.

but whats the solution.

please help i have been searching for a solution desperately.

The .aspx

<asp:Calendar ID="Calendar1" runat="server">
    <SelectedDayStyle BorderStyle="Dotted" BorderWidth="4px" />
    <TodayDayStyle BorderStyle="Solid" BorderWidth="4px" HorizontalAlign="Center" 
        VerticalAlign="Middle" />
</asp:Calendar>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
<br />
<br />
<br />
<br />
<br />
<asp:Table ID="Table1" runat="server">
</asp:Table>

The .aspx.cs

public partial class DaySelection : System.Web.UI.Page
{
    DayDateTable gen_tab = new DayDateTable();
    Label theselecteddate = new Label();
    ArrayList date = new ArrayList();

    public void Page_Init(object sender, EventArgs e)
    {
        Calendar1.SelectionChanged += new EventHandler(selectdate);
        DayDateTable generatetable = new DayDateTable();
        DropDownList1.AutoPostBack = true;
        ArrayList thedays = new ArrayList();
        thedays.Add("Monday");
        thedays.Add("Tuesday");
        thedays.Add("Wednesday");
        thedays.Add("Thursday");
        thedays.Add("Friday");
        thedays.Add("Saturday");
        thedays.Add("Sunday");
        DropDownList1.DataSource = thedays;
        DropDownList1.DataBind();
    }  

    public void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<br /> postback or not " + IsPostBack.ToString()  + "<br />");
    }

    void selectdate(object sender, EventArgs e)
    {
        DateTime startdate;   
        startdate = Calendar1.SelectedDate;
        int noofdays;
        DateTime enddate = new DateTime();
        noofdays = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
        enddate = startdate.AddDays(noofdays);
        int Count = 0;

        for (DateTime dt = startdate; dt <= enddate; dt = dt.AddDays(1.0))
        {
            if (dt.DayOfWeek.ToString() == DropDownList1.SelectedItem.Text)
            {
                date.Add(dt.Date.ToString("dd-MMMM-yyyy"));
                Count++;
            }
        }

        gen_tab.make_table(date.Count, form1, date);
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        gen_tab.find_selected(form1);
    }
}

The DayDateTable.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections;

public class DayDateTable    
{
    Button button1 = new Button();
    string the_id_checkbox;
    string the_id_label;
    Table temp = new Table();

    public DayDateTable()
    {
    }

    public void make_table(int number_of_rows, HtmlForm form1, ArrayList date)
    {
        for (int adding_rows = 0; adding_rows < number_of_rows; 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[adding_rows].ToString();
            the_id_checkbox = "checkmate" + adding_rows;
            checkmate.ID = the_id_checkbox;
            the_id_label = "The_text" + adding_rows;
            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);
            temp = (Table)form1.FindControl("Table1");
            temp.Rows.Add(table_row1);
        }
    }//make table ends

    public void find_selected(HtmlForm form)
    {            
        Table this_table = new Table();
        Label temp_label = new Label();
        this_table = (Table)form.FindControl("Table1");
        temp_label = (Label)form.FindControl("Label1");
        CheckBox check_or_not = new CheckBox();

        temp_label.Text = "you will never solve this HAHAHAHA HELPPP";

        try
        {
            for (int i = 0; i < this_table.Rows.Count; i++)
            {
                the_id_checkbox = "checkmate" + i;
                the_id_label = "The_text" + i;
                check_or_not = (CheckBox)this_table.Rows[i].FindControl(the_id_checkbox);
                if (check_or_not.Checked == true)
                {
                    temp_label.Text = temp_label.Text + ((Label)this_table.Rows[i].FindControl(the_id_label)).Text;
                }//if ends
            }//for ends
        }//try ends
        catch (NullReferenceException nn)
        {
        }    
    }//find selected endss
}