Nesse post iremos ver como trocar a masterpage dinamicamente, a partir da opção selecionada no dropdownlist .
Crie um novo projeto e adicione duas masterpage com os seguintes nomes: MasterBlue.master e MasterGreen.master
No Código MasterBlue.master insira:
<%@ Master Language="VB" Inherits="MasterBlue" CodeFile="MasterBlue.master.vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Master Blue</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
Master Blue<br />
<div style="background-color:Blue">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Já na MasterGreen.master:
<%@ Master Language="VB" Inherits="MasterGreen" CodeFile="MasterGreen.master.vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Master Green</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
Master Green
<div style="background-color:Green">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Clique na solução do seu projeto e adicione uma classe com o nome TesteMaster.vb e digite o código abaixo:
Imports Microsoft.VisualBasic
Public Class TesteMaster
Public Sub SelecionaMaster(ByRef pagina As System.Web.UI.Page, ByVal master As String)
If master = "1" Then
pagina.MasterPageFile = "~/MasterGreen.master"
Else
pagina.MasterPageFile = "~/MasterBlue.master"
End If
End Sub
End Class
Edite a página Default.aspx do seu projeto para que fique da seguinte forma:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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:DropDownList ID="ddl_escolha" runat="server">
<asp:ListItem Value="1">Master Green</asp:ListItem>
<asp:ListItem Value="2">Master Blue</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnOK" runat="server" Text="OK" />
</div>
</form>
</body>
</html>
No código Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
Response.Redirect(String.Format("Default2.aspx?master={0}", Server.UrlEncode(ddl_escolha.SelectedValue)))
End Sub
End Class
O que irá acontecer: ao selecionar a opção do dropdownlist e clicar no botão ok, será passado o parâmetro selecionado para classe, onde vai ser verificado qual das duas masterpage vai ser utilizada e então na página Default2.aspx ela será carregada de acordo com a opção escolhida.
Crie a página Default2.aspx e selecione uma das masterpage:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterBlue.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk MASTER PAGE DINÂMICA kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk<br />
</asp:Content>
Note que mesmo contendo a propriedade “MasterPageFile” no cabeçalho da página, caso a opção Master Green for escolhida no combo, será a MasterGreen.master que será carregada.
É essa propriedade é necessária na criação da pagina assim como as tags Content, onde estará o conteúdo da página.
Código Default2.aspx.vb:
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim escolha As New TesteMaster()
escolha.SelecionaMaster(Me, Request("master"))
End Sub
End Class
Caso a opção Master Green seja selecionada a página Default2.aspx ficará como a imagem abaixo:
Se for a opção Master Blue ficará como a imagem abaixo:
Espero que esse post ajude muita gente.
Comentem!
