单击表格单元格时,动态创建的Asp表将不可见

时间:2014-05-29 08:47:10

标签: c# asp.net sql

我正在开发一个项目,因为我有一个选择类的下拉列表。然后显示所选类的时间表。我已经动态地使用asp表创建了这个时间表,并在表格单元格上添加了click事件。但是当我单击表格单元格时,整个表格变得不可见。我之前使用过asp表格做同样的事情,那时我也得到了同样的错误,但是当我把创建表格方法放在帖子之外时它就解决了在页面加载事件中返回方法。但是在这里我调用我的创建时间表方法下拉选择索引更改。我已经尝试将我的asp表放在更新面板中,但这也没有解决我的问题。

以下是我的aspx代码

<asp:Panel ID="pnltimetable" runat="server" Visible="false">
       <asp:Table ID="tbltimetable" CssClass="timetable" runat="server"  CellSpacing="25" GridLines="Both"></asp:Table>
    </asp:Panel>

以下是我的cs代码

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["UserType"] == null)
    {
        Response.Redirect("Login.aspx");
    }
    if (!IsPostBack)
    {
        filldrpclass();
    }
}
//method for making timetable
protected void makeTimetable()
{
    string periodno = "";
    //getting all periods
    sql = "SELECT * FROM tblperiodtime order by periodnumber";
    ds = obj.openDataset(sql, Session["schoolcode"].ToString());
    //getting timetable of the selected class
    sql = "SELECT t.*,s.subjectname,tc.teachername FROM tbltimetable t join tblsubject s on s.sshortname=t.subject join tblteacher tc on tc.tshortname=t.tchrshortnm where t.classcode='" + drpclass.SelectedItem.Value + "' order by t.period";
    DataSet dsTimetbl = new DataSet();
    dsTimetbl = obj.openDataset(sql, Session["schoolcode"].ToString());
    DataRow[] drtimetbl;        
    //To clear previously added rows
    tbltimetable.Rows.Clear();        
    //some entries exists for periods
    if (ds.Tables[0].Rows.Count != 0)
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count + 1; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < 7; j++)
            {
                TableCell tc = new TableCell();
                if (i == 0)
                {
                    tc.CssClass = "periodTableCelldays";
                    tc.HorizontalAlign = HorizontalAlign.Center;
                    if (j == 0)
                        tc.Text = "Periods";
                    else
                    {
                        //get week day name from integer value
                        string weekday = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[j];
                        //for displaying text in the form of Weekday eg MON
                        tc.Text = (weekday.Substring(0, 3)).ToUpper();
                    }
                    tr.Cells.Add(tc);
                    tbltimetable.Rows.Add(tr);
                }
                else
                {
                    tc.HorizontalAlign = HorizontalAlign.Center;
                    if (j == 0)
                    {
                        //showing period number and timing
                        tc.CssClass = "periodtime";
                        tc.Text = "0" + ds.Tables[0].Rows[i - 1]["periodnumber"].ToString() + "<br/>" + ds.Tables[0].Rows[i - 1]["starttime"].ToString() + "-" + ds.Tables[0].Rows[i - 1]["stoptime"].ToString();
                        periodno = "0" + ds.Tables[0].Rows[i - 1]["periodnumber"].ToString();
                        tr.Cells.Add(tc);
                    }
                    else
                    {                           
                        //selecting particular period
                        drtimetbl = dsTimetbl.Tables[0].Select("period='" + periodno + "' and weekday='" + j + "'");
                        clickablecell ctCell = new clickablecell();
                        ctCell.HorizontalAlign = HorizontalAlign.Center;
                        if (drtimetbl.Length != 0)
                        {
                            ctCell.CssClass = "viewlessonplan";
                            Random rnd = new Random();
                            ctCell.ID = j + "-" + periodno + "-" + ds.Tables[0].Rows[i - 1]["starttime"].ToString() + "-" + ds.Tables[0].Rows[i - 1]["stoptime"].ToString() + "-" + "Edit-" + drtimetbl[0]["id"].ToString() + "-" + "Subject-" + drtimetbl[0]["subjectname"].ToString() + "-" + "Teacher-" + drtimetbl[0]["teachername"].ToString();
                            //ctCell.ID = drtimetbl[0]["id"].ToString() + "-" + (i * j + i + j).ToString();
                            ctCell.Text = drtimetbl[0]["subjectname"].ToString() + "<br/> by<br/>" + drtimetbl[0]["teachername"].ToString() + "<br/>Edit Period";
                            ctCell.Attributes.Add("onmouseover", "defColor=this.style.backgroundColor;  this.style.backgroundColor='LightGray';");
                            ctCell.Attributes.Add("onmouseout", "this.style.backgroundColor=defColor;");
                            ctCell.Click += new clickablecell.ClickEventHandler(textcell_Click);
                        }
                        else
                        {                               
                            ctCell.Text = "Create Period";
                            //weekday-period-starttime-stoptime
                            ctCell.ID = j + "-" + periodno + "-" + ds.Tables[0].Rows[i - 1]["starttime"].ToString() + "-" + ds.Tables[0].Rows[i - 1]["stoptime"].ToString()+"-Create-0-Subject-null-Teacher-null";
                            ctCell.CssClass = "noperiod";
                        }
                        tr.Cells.Add(ctCell);
                    }
                    tbltimetable.Rows.Add(tr);
                }
            }
        }
        tbltimetable.Visible = true;
        pnltimetable.Visible = true;
    }
    //if no entries exists for periods
    else
    {
        tbltimetable.Visible = false;
        pnltimetable.Visible = false;
        lblalerts.Text = "Time Table/Lesson Plan not created yet";
        lblalerts.Visible = true;
        divalerts.Visible = true;
    }
}
//table cell's click event
void textcell_Click(object sender, EventArgs e)
{
    makeTimetable();
    clickablecell _ctcell = (clickablecell)sender;
    string weekday = (_ctcell.ID.Split('-')[0]).ToString();
    string period = (_ctcell.ID.Split('-')[1]).ToString();
    string starttime = (_ctcell.ID.Split('-')[2]).ToString();
    string stoptime = (_ctcell.ID.Split('-')[3]).ToString();
    string operationtype = (_ctcell.ID.Split('-')[4]).ToString();
    string timetableid = (_ctcell.ID.Split('-')[5]).ToString();
    string subjectname = (_ctcell.ID.Split('-')[7]).ToString();
    string teachername = (_ctcell.ID.Split('-')[9]).ToString();
    if (operationtype == "Edit")
    {
        lbledit_heading.Text = "Edit Period";
        //for preselecting subjects when edit period is selected
        foreach (ListItem li in drpSubjects.Items)
        {
            if (li.Text == subjectname)
                li.Selected = true;
        }
        //for preselecting days
        foreach (ListItem li in drpDays.Items)
        {
            if (li.Value == weekday)
                li.Selected = true;
        }
        //for preselecting teachers
        foreach (ListItem li in drpteachers.Items)
        {
            if (li.Text == teachername)
                li.Selected = true;
        }
    }
    else
        lbledit_heading.Text = "Create Period";
    //filling controls in popup
    filldrpsubjects();
    filldrpteachers();
    lnkHidden_ModalPopupExtender.Show();
}

请帮忙。谢谢

1 个答案:

答案 0 :(得分:0)

尝试删除

Visible="false"

来自你的asp:Panel的定义。

如果您希望在初始加载时将其隐藏,请将其添加到PageLoad方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["UserType"] == null)
    {
        Response.Redirect("Login.aspx");
    }
    if (!IsPostBack)
    {
        filldrpclass();
        pnltimetable.Visible = false; //initially hidden
    }
}