WebForm TableRow删除

时间:2014-02-10 14:56:28

标签: c# asp.net

添加和删除TableRow

时遇到一个小问题

我在我的代码后面创建了一个表:

    private void RemplirTab(){
            Table1.Rows.Clear();
            /*
             * METHODE TO CREATE TABLE 
             */
            TableHeaderRow ligneEntete = new TableHeaderRow();

            TableHeaderCell entete1 = new TableHeaderCell();
            entete1.Text = "Identifiant";

            TableHeaderCell entete2 = new TableHeaderCell();
            entete2.Text = "Description";

            TableHeaderCell entete3 = new TableHeaderCell();
            entete3.Text = "Prix";

            TableHeaderCell entete4 = new TableHeaderCell();
            entete3.Text = "Action";

            ligneEntete.Cells.Add(entete1);
            ligneEntete.Cells.Add(entete2);
            ligneEntete.Cells.Add(entete3);
            ligneEntete.Cells.Add(entete4);

            Table1.Rows.Add(ligneEntete);
            foreach (Produit px in listeP)
            {
                TableRow r = new TableRow();

                TableCell colId = new TableCell();
                colId.Text = Convert.ToString(px.Id);

                TableCell colDescription = new TableCell();
                colDescription.Text = px.Description;

                TableCell colPrix = new TableCell();
                colPrix.Text = Convert.ToString(px.Prix);

                TableCell colAction = new TableCell();
              //  colAction.Text = "<a href='~/PageListeTable.aspx?Id="+px.Id+"'>Supprimer<a>";

                HyperLink lienSupr = new HyperLink();
              //  lienSupr.ID = "lienSupr";
                lienSupr.Text = "Supprimer";
                lienSupr.NavigateUrl = "~/PageListeTable.aspx?Id=" + px.Id;
                colAction.Controls.Add(lienSupr);

                r.Cells.Add(colId);
                r.Cells.Add(colDescription);
                r.Cells.Add(colPrix);
                r.Cells.Add(colAction);
                Table1.Rows.Add(r);
            }
}

在我的aspx页面中,我将代码放在下面:

<asp:Table ID="Table1" runat="server">
    </asp:Table> <br />

<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
    <br />

     <asp:Label ID="Label1" runat="server" Text="Id : " 
        AssociatedControlID="Id"></asp:Label>
        <asp:TextBox ID="Id" runat="server" Width="167px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ErrorMessage="Champ obligatoir " ControlToValidate="Id">*</asp:RequiredFieldValidator>
        <br />


        <asp:Label ID="Label2" runat="server" Text="Description :" 
            AssociatedControlID="Descrip"></asp:Label>
        <asp:TextBox ID="Descrip" runat="server"></asp:TextBox>
        <br />
       <asp:Label ID="Label3" runat="server" Text="Prix :" 
            AssociatedControlID="prix"></asp:Label>
        <asp:TextBox ID="prix" runat="server" Width="165px"></asp:TextBox>
        <asp:RangeValidator ID="RangeValidator1" runat="server" 
        ErrorMessage="Prix erroné " ControlToValidate="prix" MaximumValue="225" 
        MinimumValue="10" Type="Double">*</asp:RangeValidator>
        <br />
    <asp:Button ID="Button1" runat="server" Text="Ajouter" onclick="Button1_Click" />
</asp:Content>

在我的Page_Load()中,我调用了方法来创建一个表:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (listeP == null)
            {
                listeP = new List<Produit>();
                listeP.Add(new Produit(1, "ordi", 200));

                Produit p = new Produit(2, "clavier", 20);
                listeP.Add(p);
            }



            string pId = Request["Id"];

            if (pId != null)
            {
                int id = Convert.ToInt32(pId);
                int pos = listeP.IndexOf(new Produit(id,null,0));
                if(pos!=-1)
                listeP.RemoveAt(pos);
            }

            RemplirTab();

        }

问题是当我尝试使用表格添加表格行时,最后一个删除链接被称为 lienSupr.NavigateUrl =“〜/ PageListeTable.aspx?Id =”+ px.Id; 所以具有最后一个ID的行正在删除,而新行在

之后创建

有人可以帮我解决这个问题

0 个答案:

没有答案