表c#上的动态行

时间:2014-11-13 15:15:44

标签: c# asp.net browser

两部分问题:

1

我正在使用会话状态刷新来保存页面重新加载的计数,但是当使用一系列if语句时,验证不起作用。代码在底部。我究竟做错了什么?

2 使用会话状态对回发似乎不是最有效的方法,如果我添加了另一个具有相同功能的表,计数将影响两个表。 例如,如果我只希望人们能够在一个表中添加最多三行,则在两个表上,他们只能添加四行。

存储单独刷新计数的更好方法是什么?

更新 我发现代码没有向现有行添加行的问题,但我不确定如何修复它。

基本上每次迭代都会删除现有行,然后再次添加一行。所以永远不会超过一行。

有什么想法吗?

namespace FormTest
{
    public partial class About : Page
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            tbl.BorderStyle = BorderStyle.Inset;
            tbl.BorderWidth = Unit.Pixel(1);

        if (!Page.IsPostBack)
        {
            Session["count"] = 0;
        }
        else
        {
            int count = (int)Session["count"];
            count++;
            Session["count"] = count;
        }
    }

    protected void cmdCreate_Click(object sender, System.EventArgs e)
    {
        tbl.Controls.Clear();

        int rows = 1;
        int cols = 4;

        if ((int)Session["count"] == 0)
        {
            for (int i = 0; i < rows; i++)
            {
                TableRow rowNew = new TableRow();
                tbl.Controls.Add(rowNew);
                for (int j = 0; j < cols; j++)
                {
                    TableCell cellNew = new TableCell();
                    Label lblNew = new Label();
                    lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";

                    TextBox tbNew = new TextBox();
                    cellNew.Controls.Add(lblNew);
                    cellNew.Controls.Add(tbNew);

                    rowNew.Controls.Add(cellNew);
                }
            }
        }
        else
        {
            if ((int)Session["count"] == 1)
            {
                for (int i = 0; i < rows; i++)
                {
                    TableRow rowNew = new TableRow();
                    tbl.Controls.Add(rowNew);
                    for (int j = 0; j < cols; j++)
                    {
                        TableCell cellNew = new TableCell();
                        Label lblNew = new Label();
                        lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";

                        TextBox tbNew = new TextBox();
                        cellNew.Controls.Add(lblNew);
                        cellNew.Controls.Add(tbNew);

                        rowNew.Controls.Add(cellNew);
                    }
                }
            }
            else
            {
                if ((int)Session["count"] == 2)
                {
                    for (int i = 0; i < rows; i++)
                    {
                        TableRow rowNew = new TableRow();
                        tbl.Controls.Add(rowNew);
                        for (int j = 0; j < cols; j++)
                        {
                            TableCell cellNew = new TableCell();
                            Label lblNew = new Label();
                            lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";

                            TextBox tbNew = new TextBox();
                            cellNew.Controls.Add(lblNew);
                            cellNew.Controls.Add(tbNew);

                            rowNew.Controls.Add(cellNew);
                        }
                    }
                }
                else
                {
                    if ((int)Session["count"] == 3)
                    {
                        for (int i = 0; i < rows; i++)
                        {
                            TableRow rowNew = new TableRow();
                            tbl.Controls.Add(rowNew);
                            for (int j = 0; j < cols; j++)
                            {
                                TableCell cellNew = new TableCell();
                                Label lblNew = new Label();
                                lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";

                                TextBox tbNew = new TextBox();
                                cellNew.Controls.Add(lblNew);
                                cellNew.Controls.Add(tbNew);

                                rowNew.Controls.Add(cellNew);
                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("http://www.google.co.uk");
                    }
                }
            }
        }

    }//end cmdCreate_Click
}
}

1 个答案:

答案 0 :(得分:0)

您不需要if条件列表,只需要确保行数小于您想要的限制。如果/当添加更多表时,您还应该存储每个表的限制以避免计数冲突。

我就是这样做的:

(注意:这不会“原样” - 我假设您已移除代码以在tbl内分配cmdCreate_Click()并保留原样清晰度)

namespace FormTest
{

public partial class About : Page
{

    private int myFirstTableMaxRows = 3;

    protected void Page_Load(object sender, System.EventArgs e)
    {
        tbl.BorderStyle = BorderStyle.Inset;
        tbl.BorderWidth = Unit.Pixel(1);

        if (!Page.IsPostBack)
        {
            Session["myFirstTable_count"] = "0";
        }
        else
        {
            int count = (int)Session["myFirstTable_count"];
            Session["myFirstTable_count"] = ++count;
        }
    }

    protected void cmdCreate_Click(object sender, System.EventArgs e)
    {
        tbl.Controls.Clear();

        int cols = 4;
        int currentRowCount = (int)Session["myFirstTable_count"];
        if(currentRowCount <= myFirstTableMaxRows)
        {
            TableRow rowNew = new TableRow();
            tbl.Controls.Add(rowNew);
            for (int j = 0; j < cols; j++)
            {
                TableCell cellNew = new TableCell();
                Label lblNew = new Label();
                lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";

                TextBox tbNew = new TextBox();
                cellNew.Controls.Add(lblNew);
                cellNew.Controls.Add(tbNew);

                rowNew.Controls.Add(cellNew);
            }
        }
        else
        {
            Response.Redirect("http://www.google.co.uk");
        }

    }//end cmdCreate_Click
}

问题修改后更新

现有的行将由代码中的这一行引起:

tbl.Controls.Clear();

正在清除表格,然后根据cmdCreate_Click()方法中的逻辑添加一行。