Non è esattamente come dici: perchè una cosa è usare il controllo x da solo, una cosa è usarlo in un controllo utente e un'atra cosa è usarlo in controllo utente dentro una masterpage.
Comunque, visto che servirà pure a me, ti propongo un esempio minimale, ma funzionante.
La masterpage ha una tabella con due righe e due colonne; a sinistra due Usercontrol con DropDown, a destra due label per il risultato.
Il primo funziona lato server, il secondo lato client.
master
codice:
<%@ Master Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<asp:ContentPlaceHolder id="head" runat="server">
<script type="text/javascript" src="../js/libreria.js"></script>
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder></td>
<td><asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server"></asp:ContentPlaceHolder></td>
</tr>
<tr>
<td><asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder></td>
<td><asp:ContentPlaceHolder ID="ContentPlaceHolder4" runat="server"></asp:ContentPlaceHolder></td>
</tr>
</table>
</form>
</body>
</html>
contenuto
codice:
<%@ Page Language="VB" MasterPageFile="~/prove/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="prove_Default" title="Untitled Page" %>
<%@ Register src="mio_user_control.ascx" tagname="mio_user_control" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<uc1:mio_user_control ID="mio_user_control1" runat="server" />
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="ContentPlaceHolder2">
<uc1:mio_user_control ID="mio_user_control2" runat="server" />
</asp:Content>
<asp:Content ID="Content3" runat="server" contentplaceholderid="ContentPlaceHolder3">
<asp:Label ID="Label1" runat="server" Text="" EnableViewState="false"></asp:Label>
</asp:Content>
<asp:Content ID="Content4" runat="server" contentplaceholderid="ContentPlaceHolder4">
<asp:Label ID="Label2" runat="server" Text="" EnableViewState="false"></asp:Label>
</asp:Content>
contenuto codice
codice:
Option Strict On
Partial Class prove_Default
Inherits System.Web.UI.Page
Protected Sub mio_user_control1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mio_user_control1.SelectedIndexChanged
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Me.Label1.Text = ddl.SelectedItem.Text
End Sub
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Me.mio_user_control1.DropDownList.AutoPostBack = True
Me.mio_user_control2.DropDownList.Attributes.Add("onchange", String.Format("$(""{0}"").innerHTML = testo_lista_selezionato($(""{1}""));", Me.Label2.ClientID, Me.mio_user_control2.DropDownList.ClientID))
End Sub
End Class
user control
codice:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="mio_user_control.ascx.vb" Inherits="prove_mio_user_control" %>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0" Text=""/>
<asp:ListItem Value="1" Text="Stefano"/>
<asp:ListItem Value="2" Text="Carlo"/>
<asp:ListItem Value="3" Text="Nicola"/>
</asp:DropDownList>
user control codice
codice:
Option Strict On
Partial Class prove_mio_user_control
Inherits System.Web.UI.UserControl
Public Event SelectedIndexChanged As EventHandler
Public Property DropDownList() As DropDownList
Get
Return Me.DropDownList1
End Get
Set(ByVal value As DropDownList)
Me.DropDownList1 = value
End Set
End Property
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
RaiseEvent SelectedIndexChanged(sender, e)
End Sub
End Class