ASP.Net/User Control and Master Page/Code Behind — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:53, 26 мая 2010

Define page controls in code behind (VB.net)

<%@ Page 
    language=VB 
    debug=true 
    src="ControlsInCodeBehind.vb" 
    Inherits="clsControlsInCodeBehind" 
%>
<HTML>
<HEAD>
<TITLE>Inheriting Controls in a Code-Behind Files</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:label
    id="lblMessage1"
    runat="Server"
/>
<BR><BR>
<asp:label
    id="lblMessage2"
    runat="Server"
/>
<BR><BR>
<asp:label
    id="lblMessage3"
    runat="Server"
/>
<BR><BR>
<asp:textbox
    id="txtSample1"
    runat="Server"
/>
</form>
</BODY>
</HTML>
<%--
Imports System
Public Class clsControlsInCodeBehind
    Inherits System.Web.UI.Page
    Protected WithEvents txtSample1 As _
        System.Web.UI.WebControls.TextBox
    Protected WithEvents lblMessage1 As _
        System.Web.UI.WebControls.Label
    Protected WithEvents lblMessage2 As _
        System.Web.UI.WebControls.Label
    Protected WithEvents lblMessage3 As _
        System.Web.UI.WebControls.Label
    Private Sub Page_Init(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
        lblMessage1.Text = "invisible"
        lblMessage1.Visible = False
        lblMessage2.Text = Date.Now
        lblMessage3.Text = "Server Name: " & Server.MachineName
        txtSample1.MaxLength = 30
        txtSample1.Text = "Enter some text!" 
    End Sub
End Class
--%>



User control with code behind (VB.net)

<%@ Page Language="vb" %>
<%@ Register TagPrefix="MyTag" TagName="Header" Src="header_CB.ascx" %>
<%@ Register TagPrefix="MyTag" TagName="FeaturedBooks" Src="FeaturedBooks.ascx" %>
<html>
<head>
  <title>User Control Examples</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <form runat="server" method="post">
    <MyTag:Header id="MyHeader" runat="Server" /><br />
    <table>
      <tr>
        <td width="30%">
          <MyTag:FeaturedBooks id="MyFeaturedBooks" runat="server" />
        </td>
      </tr>
    </table>
  </form>
</body>
</html>
<%-- header_CB.ascx
<%@ Control inherits="HeaderClass" src="header_CB.vb"  %>
<table style="background-color:#cc0033" width="100%" cellpadding="10" 
                                                                 cellspacing="0">
  <tr>
    <td width="60%"> <font face="verdana,arial" size="4" color="yellow">
      <asp:label id="WelcomeMessage" runat="Server">
        Welcome to the shop!
      </asp:label> </font>
    </td>
    <td width="30%">
      <font face="verdana,arial" size="2" color="lightyellow">
      Select your Language:<br />
      <asp:DropDownList id="LanguageList" runat="Server" 
                  OnSelectedIndexChanged="DropList_Changed" AutoPostBack="True"/>
      </font>
    </td>
  </tr>
</table>

--%>

<%-- header_CB.vb
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Public Class HeaderClass : Inherits UserControl
    
  Private Languages As New Hashtable()
  Public LanguageList As DropDownList
  Public WelcomeMessage As Label
    
  Public Sub Page_Load()
    
    Languages.Add("English", "Hello, and welcome to the shop")
    Languages.Add("French", "Bonjour, et bienvenue a le magasin")
    Languages.Add("Spanish", "Buenas Dias, e bienvenido a la tienda")
    Languages.Add("German", "Guten Tag, und wilkommen ins geschaeft")
     
    If Not Page.IsPostback
   
      LanguageList.Datasource = Languages.Keys
      Page.DataBind()
    
    End If
    
  End Sub
    
  Public Sub DropList_Changed(Sender As Object, E As EventArgs)
    
    WelcomeMessage.text = Languages(Languagelist.SelectedItem.Text)
      
  End Sub
End Class
--%>

<%-- FeaturedBooks.ascx
<%@ import Namespace="System.Data" %>
<%
  Dim ResultString as String
  ResultString = "<table><tr><td class="datatablehead">Today"s Featured Books:</td></tr>"
  ResultString += "<tr><td class="datatable">"
  ResultString += "fake book data"
  ResultString += "</table>"

  Response.Write (ResultString)
%>
--%>