|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Messaggi: n/a
|
[ASPX] User Web Control e PlaceHolder
Salve a tutti,
una domanda: creo un controllo web in cui metto dentro un campo di testo e un bottone (componenti asp lato server): Codice:
<%@ 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" /> Codice:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Ciao mondo";
}
Codice:
DemoWUC d = (DemoWUC)LoadControl("DemoWUC.ascx");
PlaceHolder1.Controls.Add(d);
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. |
|
|
|
#2 |
|
Messaggi: n/a
|
Ragazzi ... con questi placeholder sto impazzendo.
Allora, creo due Web User Control: Ecco il primo Codice:
<%@ 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>
Codice:
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;
}
}
}
}
Codice:
<%@ 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>
Codice:
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;
}
}
}
}
Li uso nella pagina: Codice:
<%@ 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>
Codice:
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);
}
}
}
}
Ultima modifica di fdfdfdddd : 06-06-2008 alle 13:31. |
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:58.


















