我有一個頁面,我需要刪除整行(它不是一個表,我只是稱它爲一行以給出更好的描述)動態控制創建。 我沒有創造任何的控制問題,但現在卻變成了,對於按鈕的事件不工作如何刪除更新面板內動態創建的控件的「行」
下面是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」(」刪除「英文)bu tton,該行中的所有控件都將被刪除。 你有沒有做過類似的事情?你有什麼想法是怎麼回事?
任何意見或指導將不勝感激。
我強烈建議爲此使用GridView。我沒有看到它在你的標記。 –
使用gridview。然後,您可以使用模板字段自定義您的控件。 – briskovich