View Full Version : [ASPX] User Web Control e PlaceHolder
fdfdfdddd
20-05-2008, 14:26
Salve a tutti,
una domanda: creo un controllo web in cui metto dentro un campo di testo e un bottone (componenti asp lato server):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DemoWUC.ascx.cs" Inherits="DemoWebUserControl.DemoWUC" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
poi associo un evento al click del mouse
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Ciao mondo";
}
a questo punto in un'altra pagina ASPX ho necessità di inserire in un placeholder più controllo web di questo tipo, alla pressione di un tasto:
DemoWUC d = (DemoWUC)LoadControl("DemoWUC.ascx");
PlaceHolder1.Controls.Add(d);
Tuttavia facendo così ho (1) non si aggiungono nuovi controlli, ma se ne vede sempre uno (infatti il Placeholder1.Controls.Count è 0 ogni volta che scatta l'evento del click) e (2) quando clicco sul bottone del controllo il contenuto del placeholder s'azzera.
Ho rozzamente mezzo risolto facendo si che nel click sul pulsante della pagina aggiungo ad un arraylist un oggetto "DemoWUC" e nel load della pagina un ciclo che si smazzetta tutti gl'oggetti dell'arraylist e li aggiunge.
Tuttavia non funziona granché bene perché l'evento onload della pagina scatta "prima" del click sul bottone!! Come posso fare?
Grazie 1000 anticipatamente a tutti.
fdfdfdddd
06-06-2008, 13:08
Ragazzi ... con questi placeholder sto impazzendo.
Allora, creo due Web User Control:
Ecco il primo
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GT_Content1.ascx.cs" EnableViewState="true" Inherits="DemoPlaceHolder.Control.GT_Content1" %>
<table>
<tr>
<td><asp:TextBox ID="txtDemo1" EnableViewState="true" runat="server"></asp:TextBox></td>
<td><asp:CheckBox ID="checkRemove" EnableViewState="true" runat="server" /></td>
</tr>
</table>
Il cui code behind è
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace DemoPlaceHolder.Control
{
public partial class GT_Content1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public Boolean Remove
{
get
{
return checkRemove.Checked;
}
}
public String Demo1
{
get
{
return txtDemo1.Text;
}
set
{
txtDemo1.Text = value;
}
}
}
}
Ecco il secondo
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GT_Content2.ascx.cs" EnableViewState="true" Inherits="DemoPlaceHolder.Control.GT_Content2" %>
<table>
<tr>
<td><asp:TextBox ID="txtDemo2" EnableViewState="true" runat="server"></asp:TextBox></td>
<td><asp:CheckBox ID="checkRemove" EnableViewState="true" runat="server" /></td>
</tr>
</table>
il cui code behind è
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace DemoPlaceHolder.Control
{
public partial class GT_Content2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public Boolean Remove
{
get
{
return checkRemove.Checked;
}
}
public String Demo2
{
get
{
return txtDemo2.Text;
}
set
{
txtDemo2.Text = value;
}
}
}
}
Si ... sono praticamente identici a parte il nome di un metodo.
Li uso nella pagina:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo2.aspx.cs" Inherits="DemoPlaceHolder.Demo2" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Reference Control="~/Control/GT_Content1.ascx" %>
<%@ Reference Control="~/Control/GT_Content2.ascx" %>
<!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>Pagina senza titolo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager id="scriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel EnableViewState="true" ID="update1" runat="server">
<ContentTemplate>
<asp:PlaceHolder EnableViewState="true" ID="content1" runat="server"></asp:PlaceHolder>
<br />
<asp:Button ID="btnAggiungi1" OnClick="btnAggiungi1_Click" Text="Aggiungi" runat="server" />
<asp:Button ID="btnRimuovi1" OnClick="btnRimuovi1_Click" Text="Rimuovi" runat="server" />
<br /><br />
<asp:PlaceHolder EnableViewState="true" ID="content2" runat="server"></asp:PlaceHolder>
<br />
<asp:Button ID="btnAggiungi2" OnClick="btnAggiungi2_Click" Text="Aggiungi" runat="server" />
<asp:Button ID="btnRimuovi2" OnClick="btnRimuovi2_Click" Text="Rimuovi" runat="server" />
<br /><br />
<asp:Button ID="btnSalva" OnClick="btnSalva_Click" Text="Salva" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Il cui code behind è
sing System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DemoPlaceHolder.Control;
namespace DemoPlaceHolder
{
public partial class Demo2 : System.Web.UI.Page
{
private static int counter1;
private static int counter2;
protected void Page_Load(object sender, EventArgs e)
{
refreshPlaceholder();
}
protected void btnAggiungi1_Click(object sender, EventArgs e)
{
counter1++;
GT_Content1 t = (GT_Content1)LoadControl("./Control/GT_Content1.ascx");
content1.Controls.Add(t);
}
protected void btnRimuovi1_Click(object sender, EventArgs e)
{
}
protected void btnAggiungi2_Click(object sender, EventArgs e)
{
counter2++;
GT_Content2 t = (GT_Content2)LoadControl("./Control/GT_Content2.ascx");
content2.Controls.Add(t);
}
protected void btnRimuovi2_Click(object sender, EventArgs e)
{
}
protected void btnSalva_Click(object sender, EventArgs e)
{
String t = "";
for (int i = 0; i < counter1; i++)
{
t = t + ((GT_Content1)content1.Controls[i]).Demo1;
}
t = t + " - ";
for (int i = 0; i < counter2; i++)
{
t = t + ((GT_Content2)content2.Controls[i]).Demo2;
}
Response.Write("alert('" + t + "')");
}
private void refreshPlaceholder()
{
for (int i = 0; i < counter1; i++)
{
GT_Content1 t = (GT_Content1)LoadControl("./Control/GT_Content1.ascx");
content1.Controls.Add(t);
}
for (int i = 0; i < counter2; i++)
{
GT_Content2 t = (GT_Content2)LoadControl("./Control/GT_Content2.ascx");
content2.Controls.Add(t);
}
}
}
}
vBulletin® v3.6.4, Copyright ©2000-2026, Jelsoft Enterprises Ltd.