1

我有一个页面,我需要删除动态创建的控件的整行(它不是表格,我只是将其称为一行以提供更好的描述)。我在创建控件时没有任何问题,但现在发现按钮的事件不起作用

这是 aspx 页面 (Default.aspx) 的代码:

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true">
            <ContentTemplate>
                <asp:Label ID="lblContador" runat="server" Text="Label"></asp:Label>
                <br />
                <asp:Label ID="lblMensaje" runat="server" Text="Label"></asp:Label>
                <br />
                <asp:Literal ID="Literal1" runat="server" Text="SI( " />
                <asp:TextBox ID="txtSi" runat="server" Width="400"></asp:TextBox>
                <asp:Literal ID="Literal2" runat="server" Text=": " />
                <asp:TextBox ID="txtValorVSi" runat="server" Width="100"></asp:TextBox>
                <asp:Literal ID="Literal3" runat="server" Text=" )" />
                <asp:Panel ID="pnlMain" runat="server">
                </asp:Panel>
                <br />
                <asp:Button ID="btnAgregar" runat="server" Text="+ Añadir" OnClick="btnAgregar_Click" />
                <br />
                <asp:Literal ID="Literal4" runat="server" Text="SINO ( " />
                <asp:TextBox ID="txtSino" runat="server"></asp:TextBox>
                <asp:Literal ID="Literal5" runat="server" Text=" )" />
                <br />
                <br />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

这是页面背后的代码(Default.aspx.cs):

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

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        static List<TextBox> arregloTxtCondicion = new List<TextBox>();
        static List<TextBox> arregloTxtValorVSiPeroSi = new List<TextBox>();
        static List<Button> arregloBtnEliminar = new List<Button>();
        static int contadorControles;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                contadorControles = 0;
                lblContador.Text = "";
                contadorControles = 0;
            }
            try
            {
                for (int i = 0; i < contadorControles; i++)
                    AgregarControles(arregloTxtCondicion[i], arregloTxtValorVSiPeroSi[i], arregloBtnEliminar[i]);
            }
            catch (Exception ex)
            {
                lblContador.Text = ex.Message;
            }

        }
        protected void AgregarControles(TextBox txtCondicion, TextBox txtValorV, Button btnEliminar)
        {
            try
            {
                pnlMain.Controls.Add(new LiteralControl(" PERO.SI( "));
                pnlMain.Controls.Add(txtCondicion);
                pnlMain.Controls.Add(new LiteralControl(" : "));
                pnlMain.Controls.Add(txtValorV);
                pnlMain.Controls.Add(new LiteralControl(" ) "));
                pnlMain.Controls.Add(btnEliminar);
                pnlMain.Controls.Add(new LiteralControl("<br />"));
            }
            catch (Exception ex)
            {
                lblContador.Text = ex.Message;
            }
        }
        protected void btnAgregar_Click(object sender, EventArgs e)
        {
            try
            {
                int numeroRegistro = contadorControles;

                //creamos nuestros controles
                TextBox txtCondicion = new TextBox();
                txtCondicion.ID = "txtPeroSi" + numeroRegistro.ToString();
                txtCondicion.Width = 400;
                arregloTxtCondicion.Add(txtCondicion);

                TextBox txtValorV = new TextBox();
                txtValorV.ID = "txtPeroSiValorV" + numeroRegistro.ToString();
                txtValorV.Width = 100;
                arregloTxtValorVSiPeroSi.Add(txtValorV);

                Button btnEliminar = new Button();
                btnEliminar.ID = "btnEliminar" + numeroRegistro.ToString();
                btnEliminar.Text = "- Quitar";
                btnEliminar.Click += new EventHandler(this.btnEliminar_Click);
                arregloBtnEliminar.Add(btnEliminar);

                //agregamos los Controles al Panel
                AgregarControles(txtCondicion, txtValorV, btnEliminar);
                contadorControles++;
            }
            catch (Exception ex)
            {
                lblContador.Text = ex.Message;
            }

        }
        protected void btnEliminar_Click(object sender, EventArgs e)
        {
            lblMensaje.Text = "Se elimino elemento: " + sender.ToString();
            //http://stackoverflow.com/questions/7650592/button-inside-update-panel-is-not-triggered-in-asp-net

        }
    }
}

这就是我的页面的外观我 在此处输入图像描述 需要当我按下“- Quitar”(英文中的“remove”)按钮时,该行中的所有控件都将被删除。你做过类似的事情吗???你知道出了什么问题吗?

任何建议或指导将不胜感激。

4

1 回答 1

0

将每一行放在不同的 asp:Panel 中,然后单击按钮,您可以像这样将其删除。

((Button)sender).Parent.Controls.Remove;

像这样的东西可能对你有用。

于 2013-09-05T21:02:33.573 回答