how to access user control in aspx file

G

Guest

Hi,
I guess I am confused. In aspx script, I mean (you won't use
Codebehind="enrollinfo.aspx.vb", but mix code with html and code together)
You can access user control's property directly. Since I am useing visual
studio .net,
the html and code are seperated.
I have the following aspx code which has two user controls:
<%@ Register TagPrefix="Subway" TagName="Peopleinfo" Src="Peopleinfo.ascx" %>
<%@ Register TagPrefix="Subway" TagName="Address" Src="Address.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="enrollinfo.aspx.vb" Inherits="subway.enrollinfo"%>
....
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<SUBWAY:ADDRESS id="address" runat="server" Caption="Home
Address"></SUBWAY:ADDRESS>
<p></p>
<SUBWAY:pEOPLEINFO id="Peopleinfo" runat="server" Caption="Employee
Information"></SUBWAY:pEOPLEINFO>
<p></p>
<asp:hyperlink id="Dependentlink" runat="server"
NavigateUrl="Peopleinfo1.aspx?SSN=" text="Add Dependents for
"></asp:hyperlink>
<asp:Button id="Button1" runat="server" Text="Add Dependents"></asp:Button>
</form>
</body>
</HTML>
and in enrollinfo.aspx.vb
I didn't see user controls' declaration in enrollinfo.aspx.vb, only two
server controls appear:
Protected WithEvents Dependentlink As System.Web.UI.WebControls.HyperLink
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

so I added "Public People As PeopleInfo" myself, and later on,
and I have the following:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
People = New PeopleInfo
If People.SSN <> "" Then
Response.Redirect("Peopleinfo1.aspx?SSN" + People.SSN.ToString)
End If
End Sub

Peopleinfo is user control class and I tried to access its SSN property ,
but it gives
me error message something like: Null reference object accessed.
It seems that People.SSN is a wrong way, can you give me a hint? I have
properties for peopleinfo control already, here it is:

Public Class PeopleInfo
Inherits System.Web.UI.UserControl

Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents TxtSSN As System.Web.UI.WebControls.TextBox
Protected WithEvents TxtPlan As System.Web.UI.WebControls.TextBox
Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Private designerPlaceholderDeclaration As System.Object

Public Caption As String = "Dependent Information"
Private _ShowCaption As Boolean = True

Public Property ShowCaption() As Boolean
Get
Return _ShowCaption
End Get
Set(ByVal Value As Boolean)
_ShowCaption = value
End Set
End Property

Public Property FirstName() As String
Get
Return txtFirstName.Text
End Get
Set(ByVal Value As String)
txtFirstName.Text = Value
End Set
End Property

Public Property LastName() As String
Get
Return txtLastName.Text
End Get
Set(ByVal Value As String)
txtLastName.Text = Value
End Set
End Property

Public Property SSN() As String
Get
Return TxtSSN.Text
End Get
Set(ByVal Value As String)
TxtSSN.Text = Value
End Set
End Property

Public Property Plan() As String
Get
Return TxtPlan.Text
End Get
Set(ByVal Value As String)
TxtPlan.Text = Value
End Set
End Property


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

End Class
Betty
 
G

Guest

Hi,
I got it.
I just declare Protected WithEvents Peopleinfo1 As PeopleInfo (which I
thought the .net development tool will do it for me just like it did for
regular server controls.)
Then I access it in
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If Peopleinfo1.SSN <> "" And Peopleinfo1.LastName <> "" Then
Response.Redirect("Peopleinfo1.aspx?SSN=" +
Peopleinfo1.SSN.ToString + "LastName=" + Peopleinfo1.LastName.ToString)
End If
End Sub

It worked out fine. But I didn't figure it out yet what the exact difference
from what I
did before.
 
G

Guest

Just be careful not to design a real-life application that passes the SSN as
a QueryString parameter. This piece of information is confidential.
 
S

Steven Cheng[MSFT]

Hi betty,

yes, you can just explicitly declare the member control field in page
class(add withevents if using vb.net) so that the runtime will associate it
with the correct control instance... Also, in ASP.NET 2.0, we do not need
to do this since the new dynamic compilation model will automatically
generate the member field into page class.... In addition, we can also
programmatically find the control through Form1's Controls collection, we
can first get the Form1's reference and loop the sub controls to find all
the sub Usercontrols. e.g:

=====================
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim form1 As HtmlForm = Page.FindControl("form1")


Dim ctrl As Control

For Each ctrl In form1.Controls
Response.Write("<br>" & ctrl.GetType().ToString())
Next
End Sub
======================

Hope also helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Thread-Topic: how to access user control in aspx file
| thread-index: AcYXFeSnfIbuzmBbQECFQCOO+OLYnw==
| X-WBNR-Posting-Host: 12.25.78.238
| From: "=?Utf-8?B?YzY3NjIyOA==?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: how to access user control in aspx file
| Date: Wed, 11 Jan 2006 17:17:02 -0800
| Lines: 159
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:370228
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Can I use cookie, what do you think is the best way?
| --
| Betty
|
|
| "Phillip Williams" wrote:
|
| > Just be careful not to design a real-life application that passes the
SSN as
| > a QueryString parameter. This piece of information is confidential.
| > --
| > HTH,
| > Phillip Williams
| > http://www.societopia.net
| > http://www.webswapp.com
| >
| >
| > "c676228" wrote:
| >
| > > Hi,
| > > I got it.
| > > I just declare Protected WithEvents Peopleinfo1 As PeopleInfo (which
I
| > > thought the .net development tool will do it for me just like it did
for
| > > regular server controls.)
| > > Then I access it in
| > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > > System.EventArgs) Handles Button1.Click
| > >
| > > If Peopleinfo1.SSN <> "" And Peopleinfo1.LastName <> "" Then
| > > Response.Redirect("Peopleinfo1.aspx?SSN=" +
| > > Peopleinfo1.SSN.ToString + "LastName=" +
Peopleinfo1.LastName.ToString)
| > > End If
| > > End Sub
| > >
| > > It worked out fine. But I didn't figure it out yet what the exact
difference
| > > from what I
| > > did before.
| > > --
| > > Betty
| > >
| > >
| > > "c676228" wrote:
| > >
| > > > Hi,
| > > > I guess I am confused. In aspx script, I mean (you won't use
| > > > Codebehind="enrollinfo.aspx.vb", but mix code with html and code
together)
| > > > You can access user control's property directly. Since I am useing
visual
| > > > studio .net,
| > > > the html and code are seperated.
| > > > I have the following aspx code which has two user controls:
| > > > <%@ Register TagPrefix="Subway" TagName="Peopleinfo"
Src="Peopleinfo.ascx" %>
| > > > <%@ Register TagPrefix="Subway" TagName="Address"
Src="Address.ascx" %>
| > > > <%@ Page Language="vb" AutoEventWireup="false"
| > > > Codebehind="enrollinfo.aspx.vb" Inherits="subway.enrollinfo"%>
| > > > ...
| > > > <body MS_POSITIONING="GridLayout">
| > > > <form id="Form1" method="post" runat="server">
| > > > <SUBWAY:ADDRESS id="address" runat="server" Caption="Home
| > > > Address"></SUBWAY:ADDRESS>
| > > > <p></p>
| > > > <SUBWAY:pEOPLEINFO id="Peopleinfo" runat="server"
Caption="Employee
| > > > Information"></SUBWAY:pEOPLEINFO>
| > > > <p></p>
| > > > <asp:hyperlink id="Dependentlink" runat="server"
| > > > NavigateUrl="Peopleinfo1.aspx?SSN=" text="Add Dependents for
| > > > "></asp:hyperlink>
| > > > <asp:Button id="Button1" runat="server" Text="Add
Dependents"></asp:Button>
| > > > </form>
| > > > </body>
| > > > </HTML>
| > > > and in enrollinfo.aspx.vb
| > > > I didn't see user controls' declaration in enrollinfo.aspx.vb, only
two
| > > > server controls appear:
| > > > Protected WithEvents Dependentlink As
System.Web.UI.WebControls.HyperLink
| > > > Protected WithEvents Button1 As System.Web.UI.WebControls.Button
| > > >
| > > > so I added "Public People As PeopleInfo" myself, and later on,
| > > > and I have the following:
| > > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > > > System.EventArgs) Handles Button1.Click
| > > > People = New PeopleInfo
| > > > If People.SSN <> "" Then
| > > > Response.Redirect("Peopleinfo1.aspx?SSN" +
People.SSN.ToString)
| > > > End If
| > > > End Sub
| > > >
| > > > Peopleinfo is user control class and I tried to access its SSN
property ,
| > > > but it gives
| > > > me error message something like: Null reference object accessed.
| > > > It seems that People.SSN is a wrong way, can you give me a hint? I
have
| > > > properties for peopleinfo control already, here it is:
| > > >
| > > > Public Class PeopleInfo
| > > > Inherits System.Web.UI.UserControl
| > > >
| > > > Protected WithEvents txtFirstName As
System.Web.UI.WebControls.TextBox
| > > > Protected WithEvents txtLastName As
System.Web.UI.WebControls.TextBox
| > > > Protected WithEvents TxtSSN As System.Web.UI.WebControls.TextBox
| > > > Protected WithEvents TxtPlan As
System.Web.UI.WebControls.TextBox
| > > > Protected WithEvents HyperLink1 As
System.Web.UI.WebControls.HyperLink
| > > > Protected WithEvents Button1 As System.Web.UI.WebControls.Button
| > > >
| > > > Private designerPlaceholderDeclaration As System.Object
| > > >
| > > > Public Caption As String = "Dependent Information"
| > > > Private _ShowCaption As Boolean = True
| > > >
| > > > Public Property ShowCaption() As Boolean
| > > > Get
| > > > Return _ShowCaption
| > > > End Get
| > > > Set(ByVal Value As Boolean)
| > > > _ShowCaption = value
| > > > End Set
| > > > End Property
| > > >
| > > > Public Property FirstName() As String
| > > > Get
| > > > Return txtFirstName.Text
| > > > End Get
| > > > Set(ByVal Value As String)
| > > > txtFirstName.Text = Value
| > > > End Set
| > > > End Property
| > > >
| > > > Public Property LastName() As String
| > > > Get
| > > > Return txtLastName.Text
| > > > End Get
| > > > Set(ByVal Value As String)
| > > > txtLastName.Text = Value
| > > > End Set
| > > > End Property
| > > >
| > > > Public Property SSN() As String
| > > > Get
| > > > Return TxtSSN.Text
| > > > End Get
| > > > Set(ByVal Value As String)
| > > > TxtSSN.Text = Value
| > > > End Set
| > > > End Property
| > > >
| > > > Public Property Plan() As String
| > > > Get
| > > > Return TxtPlan.Text
| > > > End Get
| > > > Set(ByVal Value As String)
| > > > TxtPlan.Text = Value
| > > > End Set
| > > > End Property
| > > >
| > > >
| > > > 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
| > > >
| > > > End Class
| > > > Betty
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top