Web User Control a reprezentace vlastností v návrhu   zodpovězená otázka

ASP.NET WebForms

Dobrý den, vytvořil jsem si Web User Control, který obsahuje Label a TextBox. Po vložení Controlu na stránku bych chtěl, při nastavení LabelText="Jméno" Width="80" Text="Test", aby se tyto vlatnosti reprezentovaly v návrhovém režimu. Jde to?

Stránka:

1
2
3
4
5
6
7
8
9
10
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="TestControl2.aspx.vb" Inherits="TestControl2" %>
<%@ Register Src="MyTools/LabelTextBox.ascx" TagName= "MyLabelTextBox1" TagPrefix="MyLabelTextBox"  %>
 
<asp:Content ID="Content5" ContentPlaceHolderID="MasterPageMainNadpis" Runat="Server">
Test
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="MasterPageMain" Runat="Server">
    <MyLabelTextBox:MyLabelTextBox1 ID="Jmeno" runat="server" LabelText="Jméno" Width="80" Text="Test" />
    <MyLabelTextBox:MyLabelTextBox1 ID="Prijmeni" runat="server"  LabelText="Příjmení" Width="80" Text="" />
</asp:Content>

Control:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="LabelTextBox.ascx.vb" Inherits="MyTools_WebUserControl" %>
 
<script runat="server">
 
    Partial Class MyTools_WebUserControl
        Inherits System.Web.UI.UserControl
        Protected currentColorIndex As Integer = 0
 
 
        Private mLabel As String
        Public Property LabelText() As String
            Get
                Return mLabel
            End Get
            Set(ByVal value As String)
                mLabel = value
            End Set
        End Property
 
        Private mText As String
        Public Property Text() As String
            Get
                Return mText
            End Get
            Set(ByVal value As String)
                mText = value
            End Set
        End Property
 
        Private mWidth As Integer
        Public Property Width() As Integer
            Get
                Return mWidth
            End Get
            Set(ByVal value As Integer)
                mWidth = value
            End Set
        End Property
 
 
        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Me.MyLabel.Text = LabelText
            Me.MyTextBox.Text = Text
            Me.MyTextBox.Width = Width
        End Sub
 
    End Class
</script>
 
<asp:Table ID="Table1" runat="server" style="float: left;">
    <asp:TableRow>
        <asp:TableCell>
            <asp:Label ID="MyLabel" runat="server" CssClass="Label" Text="Label"></asp:Label>
            <br />
            <asp:TextBox ID="MyTextBox" Wrap="true" Width="40px" Text="" runat="server"></asp:TextBox>
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Zaprvé vlastnost Width byste měl reprezentovat datovým typem Unit, který je na to určen. Dále v tomto případě může getter a setter vlastností vracet, resp. nastavovat přímo na vlastnosti komponent:

1
2
3
4
5
6
7
8
Public Property Width() As Unit
    Get
        Return MyTextBox.Width
    End Get
    Set(ByVal value As Integer)
        MyTextBox.Width = value
    End Set
End Property

Pokud se hodnota vlastnosti nemapuje přímo na vlastnosti komponent, v drtivé většině případů se ukládá do ViewState:

1
2
3
4
5
6
7
8
9
10
<Bindable(True), Category("Appearance"), DefaultValue(Nothing)> _
Public Property ImageWidth() As Unit
    Get
        If ViewState("ImageWidth") Is Nothing Then ViewState("ImageWidth") = Unit.Parse("600px")
        Return CType(ViewState("ImageWidth"), Unit)
    End Get
    Set(ByVal value As Unit)
        ViewState("ImageWidth") = value
    End Set
End Property

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Zdravím, zkouším a nic. Samozřejmě hledám na googlu a nemohu najít nic co by odpovídalo mé představě. Možná pomůže tato ukázka.

Pracuji ve: Microsoft Visual Web Developer 2008 Express Edition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@ Control Language="VB" ClassName="WebUserControl" %>
 
<script runat="server">
    Private _text As String
    Public Property text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property
 
    Public Property Width() As Unit
        Get
            Return TextBox1.Width
        End Get
        Set(ByVal value As Unit)
            TextBox1.Width = value
        End Set
    End Property
 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.TextBox1.Text = text
    End Sub
</script>
 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default4.aspx.vb" Inherits="Default4" %>
<%@ Register Src="MyTools/WebUserControl.ascx" TagName= "WebUserControl1" TagPrefix="WebUserControl"  %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Text="Tento text se v Design modu zobrazuje." Width="600px"></asp:TextBox><br />
        <WebUserControl:WebUserControl1 ID="WebUserControl11" text="Tento text se nezobrazuje v Design modu, ale já bych chtěl aby se tam zobrazoval, jde to?" Width="600px" runat="server" />
    </div>
    </form>
</body>
</html>

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Tahle to asi nepůjde, budu muset vytvořit serverový ovládací prvek.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Serverový ovládací prvek (komponentu, UserControl) právě vytváříte. Akorát máte špatně vlastnost Text, tu také musíte upravit tak, aby nastavovala / vracela TextBox1.Text a ne proměnnou _text, která se nikde jinde nepoužívá.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

přihlásit pomocí externího účtu

přihlásit pomocí jména a hesla

Uživatel:
Heslo:

zapomenuté heslo

 

založit nový uživatelský účet

zaregistrujte se

 
zavřít

Nahlásit spam

Opravdu chcete tento příspěvek nahlásit pro porušování pravidel fóra?

Nahlásit Zrušit

Chyba

zavřít

feedback