おのれ鍋奉行が!

ascxファイルの配置

最終更新:

lmes2

- view
メンバー限定 登録/ログイン

ascxファイルの配置


概要

webユーザーコントロールファイル「WebUserControl.ascx」を作成し、default.aspx から参照・操作する。

参照


参考


前提条件


手順

[ソリューションエクスプローラ] -> webサイトを右クリック -> 「新しい項目の追加」

not found (257.jpg)

「web ユーザー コントロール」を選択 -> 「追加」

not found (258.jpg)

「WebUserControl.ascx」が増えているのを確認。

not found (259.jpg)

default.aspx を以下のように変更。

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Reference Control="WebUserControl.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>無題のページ</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:placeholder id="PlaceHolder" 
        runat="server" />
 
    </div>
    </form>
</body>
</html>
 

default.aspx.vb を以下のように変更。

Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load
 
        ' ユーザーコントロールを取得
        Dim ctrl As WebUserControl = _
         CType(Page.LoadControl("WebUserControl.ascx"), WebUserControl)
 
        ' ユーザーコントロールのプロパティに代入
        ctrl.LabelText = "Hello World!"
 
        ' プレイスホルダーに追加
        PlaceHolder.Controls.Add(ctrl)
 
    End Sub
End Class
 

WebUserControl.ascx.vb を以下のように変更。

Partial Class WebUserControl
    Inherits System.Web.UI.UserControl
 
    Dim _labelText As String
 
    ' プロパティの設定
    Public Property LabelText() As String
        Get
            Return _labelText
        End Get
        Set(ByVal value As String)
            If Not String.IsNullOrEmpty(value) Then
                _labelText = Server.HtmlEncode(value)
            End If
        End Set
    End Property
 
    ' メソッドの設定
    Sub label1_init(ByVal sender As Object, _
      ByVal e As EventArgs)
        label1.Text = LabelText
    End Sub
 
End Class
 

WebUserControl.ascx を以下のように変更。

<%@ Control Language="VB" AutoEventWireup="false" 
CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
 
<asp:label id="label1" runat="server" Text="" oninit="label1_init" />
 

実行すると以下のように表示される。

not found (260.jpg)
記事メニュー
目安箱バナー