aspx sidan ser ut så här Hej Fredrik, pröva att göra så här istället: Istället för vadå? japp, Hämta värden från UserControl via CodeBehind
<code>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Address.aspx.vb" Inherits="ControlTest.Address1"%>
<%@ Register TagPrefix="MyControl" TagName="Address" Src="controls/Address.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Address</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<MyControl:Address id="ctrlAddress" runat="server" />
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 168px" runat="server"
Text="Button"></asp:Button>
<asp:Label id="lblStreet" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 168px"
runat="server" Width="200px"></asp:Label>
</form>
</body>
</HTML>
</code>
Dess codebehind så här
<code>
Public Class Address1
Inherits System.Web.UI.Page
Dim ctlControl As Control
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents lblStreet As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ctlControl = LoadControl("controls/Address.ascx")
lblStreet.Text = CType(ctlControl, Address).Street
CType(ctlControl, Address).City = "CITY"
End Sub
End Class
</code>
När jag klickar så vill jag att lblStreet.Text ska visa det som står i UserControlens Street fält.
Kör jag detta direkt i script taggar på aspx sidan så funkar det, vad gör jag för fel?
Property från usercontrolen
<code>
Public Property Street() As String
Get
Return txtStreet.Text
End Get
Set(ByVal Value As String)
txtStreet.Text = Value
End Set
End Property
</code>Sv: Hämta värden från UserControl via CodeBehind
...
Protected WithEvents Address1 As Address
...
lblStreet.Text = Address1.City
...
Sv:Hämta värden från UserControl via CodeBehind
Provade lägga in detta, fick exception.
Den ville ha en instans av objektet, skapar man det så får jag fel ändå.. i propertyn.
Du får gärna ge ett utförligt svar till varför!?Sv: Hämta värden från UserControl via CodeBehind
använd namnet ctrlAddress istället för Adress1