Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何删除一个";“世界其他地区”;在“更新”面板中动态创建的控件的数量_C#_Asp.net - Fatal编程技术网

C# 如何删除一个";“世界其他地区”;在“更新”面板中动态创建的控件的数量

C# 如何删除一个";“世界其他地区”;在“更新”面板中动态创建的控件的数量,c#,asp.net,C#,Asp.net,我有一个页面,需要删除动态创建的控件的整行(这不是表,我只是称它为行以提供更好的描述)。 我在创建控件时没有遇到任何问题,但现在发现按钮的事件不起作用 以下是aspx页面(Default.aspx)的代码: 下面是代码隐藏页面(Default.aspx.cs)的代码: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebContro

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

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








下面是代码隐藏页面(Default.aspx.cs)的代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间WebApplication2
{
公共部分类默认值:System.Web.UI.Page
{
静态列表arreglotcondition=new List();
静态列表arreglotvalorvsiperosi=新列表();
静态列表arregloBtnEliminar=新列表();
静态控制;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
ContadorControls=0;
lblContador.Text=“”;
ContadorControls=0;
}
尝试
{
for(int i=0;i”);
}
捕获(例外情况除外)
{
lblContador.Text=ex.消息;
}
}
受保护的无效btnAgregar\u单击(对象发送方,事件参数e)
{
尝试
{
int numerioregistro=contadorControls;
//纽斯特罗斯乳膏对照品
TextBox txtCondicion=新的TextBox();
txtCondicion.ID=“txtPeroSi”+numerioregistro.ToString();
txcondition.Width=400;
arregoltxcondition.Add(txcondition);
TextBox txtValorV=新的TextBox();
txtValorV.ID=“txtPeroSiValorV”+numerioregistro.ToString();
txtValorV.宽度=100;
ArregoltxtValorvsiperosi.Add(txtValorV);
按钮btnEliminar=新按钮();
btnEliminar.ID=“btnEliminar”+numerioregistro.ToString();
btnEliminar.Text=“-Quitar”;
btnEliminar.Click+=neweventhandler(this.btnEliminar\u Click);
arrelobtneliminar.Add(btnEliminar);
//agregamos los控制面板
AgregarControls(TXCondition、txtValorV、btnEliminar);
ContadorControls++;
}
捕获(例外情况除外)
{
lblContador.Text=ex.消息;
}
}
受保护的无效btnEliminar\u单击(对象发送方,事件参数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”)按钮时,该行中的所有控件都将被删除。 你做过类似的事情吗???你知道怎么回事吗


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

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

((按钮)发件人).Parent.Controls.Remove


类似的东西可能适合您。

我强烈建议您使用GridView。我在您的标记中没有看到它。请使用gridview。然后你可以使用模板字段自定义控件。但是我以后如何从文本框中检索数据?就像你现在得到的一样。哈哈,这正是我考虑尝试使用网格的原因,因为我不知道我在哪一行:SYes使用网格是一个更好的选择。但是如果您想使用这个方法,那么您可以获得如下数据:foreach(this.Controls.OfType()中的控件c){string text1=((TextBox)c.FindControl(“textboxid”)).Text;}上面的代码将遍历每一行并从中获取值。
  <%@ 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>
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

        }
    }
}