0

我有一个页面可以在单击按钮时动态创建网格视图,效果很好。随着它创建一个网格,在网格的顶部有一个链接按钮可以删除它。现在,我想要完成的是,当我删除其中一个网格时,它将从页面中删除网格并重新同步数字。意思是,假设我有以下内容:

Grid1
Grid2
Grid3
Grid4

如果我决定删除 Grid3,实际发生的情况是:

Grid1
Grid2
Grid4

然后,如果我单击页面上的某些内容以重新加载或其他内容,那么它将正确地重新同步数字:

Grid1
Grid2
Grid3

我不知道它为什么这样做,为什么它一开始不这样做。有人可以帮我吗?

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_Init(object sender, EventArgs e)
    {
        string ctrlname = this.Page.Request.Params.Get("__EVENTTARGET");

        if (Session["Tables"] == null)
        {

            Session.Add("Tables", 1);

            DataTable dt = GetTable();

            Session.Add(Session["Tables"].ToString(), dt);
            GridView gv = new GridView();
            gv.ID = "Grid-" + Session["Tables"].ToString();
            gv.DataSource = dt;
            gv.DataBind();
            LinkButton lb = new LinkButton();
            lb.ID = "Delete-Grid-" + Session["Tables"].ToString();
            lb.Text = "Delete Grid " + Session["Tables"].ToString();
            lb.Click += new EventHandler(DeleteGrid);
            PlaceHolder1.Controls.Add(lb);
            PlaceHolder1.Controls.Add(gv);

        }
        if (IsPostBack)
        {
            PlaceHolder1.Controls.Clear();
            int next = (int)Session["Tables"];

            for (int i = 1; i <= (int)Session["Tables"]; i++)
            {
                GridView gv = new GridView();
                gv.ID = "Grid-" + i.ToString();
                gv.DataSource = (DataTable)Session[i];
                gv.DataBind();
                LinkButton lb = new LinkButton();
                lb.ID = "Delete-Grid-" + i.ToString();
                lb.Text = "Delete Grid " + i.ToString();
                lb.Click += new EventHandler(DeleteGrid);
                PlaceHolder1.Controls.Add(lb);
                PlaceHolder1.Controls.Add(gv);
            }
            if (ctrlname == "Button1")
            {
                next = next + 1;
                Session["Tables"] = next;
                DataTable dt = GetTable();
                Session.Add(Session["Tables"].ToString(), dt);
                GridView gv = new GridView();
                gv.ID = "Grid-" + next.ToString();
                gv.DataSource = (DataTable)Session[next];
                gv.DataBind();
                LinkButton lb = new LinkButton();
                lb.ID = "Delete-Grid-" + next.ToString();
                lb.Text = "Delete Grid " + next.ToString();
                lb.Click += new EventHandler(DeleteGrid);
                PlaceHolder1.Controls.Add(lb);
                PlaceHolder1.Controls.Add(gv);
            }

        }
    }



    protected void Button1_Click(object sender, EventArgs e)
    {

    }




    protected void DeleteGrid(object sender, EventArgs e)
    {
        LinkButton gridLink = (LinkButton)sender;
        String gridNum = gridLink.ID.ToString().Split('-').Last();

        GridView grid = (GridView)this.Page.FindControl("Grid-" + gridNum);
        LinkButton lbd = (LinkButton)this.Page.FindControl("Delete-Grid-" + gridNum);


        PlaceHolder1.Controls.Remove(grid);
        PlaceHolder1.Controls.Remove(lbd);

        int next = (int)Session["Tables"];
        next = next - 1;
        Session.Add("Tables", next);
        Session.Remove(gridNum);
    }






    public DataTable GetTable()
    {
        //
        // Here we create a DataTable with a few columns.
        //
        // Create Datatable to store all colums
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Size", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
        dt.Columns.Add(new DataColumn("Unit", typeof(string)));
        dt.Columns.Add(new DataColumn("Duration", typeof(string)));
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
        dt.Columns.Add(new DataColumn("Amount", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["RowNumber"] = 2;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["RowNumber"] = 3;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["RowNumber"] = 4;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["RowNumber"] = 5;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
        return dt;
    }


}
4

1 回答 1

0

您需要先了解一个asp.net 页面的生命周期。请参考这篇文章...

在您的情况下,删除甚至在页面加载事件后触发。因此同步发生在该行被删除之前。可能您需要将同步代码放在另一个方法中并在删除后调用它......

于 2013-10-03T00:59:07.490 回答