User Control not showing properties

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

In VB 2008, I have a user control added to the page in the PageLoad event -
but the properties are causing me an error.

The program (TakeSurveyTest.aspx) using the control (ContactInfo):

<%@ Page Language="VB" AutoEventWireup="true" Trace="true"
CodeFile="TakeSurveyTest.aspx.vb" Inherits="TakeSurveyTest" %>
....
<div visible="true" runat="server">
<asp:PlaceHolder ID="ContactInfo" runat="server"/> <br />
<--------------The error
<asp:Button ID="SubmitContact" Text="Submit Contact"
OnClick="SaveContact_Click" runat="server" /><br />
</div>

The ContactInfo control (ContactInfo.ascx):

<%@ Control Language="VB" AutoEventWireup="true"
CodeFile="ContactInfo.ascx.vb" Inherits="ContactInfo" %>

<td class="style4">
<asp:TextBox ID="objFirstName" Width="120px" runat="server" />
</td>
<td class="style2">
<asp:TextBox ID="objMiddleName" Width="120px" runat="server" />
</td>

And the ContactInfo.ascx.vb:

Imports System.Data
Imports System.Data.SqlClient
Imports MyFunctions.BitHandling
Imports System.Drawing
Imports MyFunctions
Imports RolesBasedAuthentication

Partial Class ContactInfo
Inherits System.Web.UI.UserControl
....
Public Property FirstName() As String
Get
Return objFirstName.Text
End Get
Set(ByVal value As String)
objFirstName.Text = Value
End Set
End Property

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


So the properties are being defined but in the TakeSurveyTest.aspx.vb file
is saying ContactInfo doesn't have these properties. I load the conrol in
my PageLoad as:

ContactInfo.Controls.Add(LoadControl("~/ContactInfo.ascx"))

But these lines give me the error:

parameters(1).Value = ContactInfo.FirstName
parameters(2).Value = ContactInfo.LastName

The error is:

'FirstName' is not a member of 'System.Web.UI.WebControls.PlaceHolder'

Which is true, but how do access the ContactInfo parameters?

Thanks,

Tom
 
I can statically do this by registering the control:

<%@ Register TagPrefix="uc" TagName="ContactInfo" Src="contactInfo.ascx" %>

and then change the Placeholder line to:

<uc:ContactInfo ID="ContactInfo" runat="server" />

Then the information errors go away because not the "ContactInfo" points to
UserControl (ControlInfo.ascx).

But if I use loadControl and Add to the control, I can't seem to use the
properties.

There must be a way to do this?

Thanks,

Tom
 
Yes there is. In the first example, you have a placeholder. A placeholder
just holds controls so there's no way you're going to reference any sort of
data such as FirstName because it is of type
System.Web.UI.WebControls.PlaceHolder. Your control is a member of the
ContactInfo placeholder's control collection.

First, don't name the placeholder the same name as a class you're going to
use, it just leads to more confusion. Next, you have to really be sure
exactly what the position of your ContactInfo control is going to be at in
order to access it from the placeholder. Your best bet though, make use if
the control before you add it to the placeholder. You do this by creating a
variable of the type of your control, accessing it's properties, then adding
it to the placeholder. This way you have a strongly typed control that you
can talk to, and when you add it to the placeholder it will then get
rendered.

I'm going to use C# for this example because I haven't used VB in years.

ContactInfo myControl = (ContactInfo)LoadControl("~/ContactInfo.ascx");

Now, I'm using ContactInfo here not as the name of the placeholder, but as a
strongly typed control. Rename the placeholder control to make it easier.
Let's call it plc_ContactInfo for ease.

Now that you have myControl, which your user control, you can access it's
properties and assign your parameters. Then add it to your placeholder such
as plc_ContactInfo.Controls.Add(myControl); (sorry, again in C#)

Hope this helps,
Mark Fitzpatrick
 
Mark Fitzpatrick said:
Yes there is. In the first example, you have a placeholder. A placeholder
just holds controls so there's no way you're going to reference any sort
of data such as FirstName because it is of type
System.Web.UI.WebControls.PlaceHolder. Your control is a member of the
ContactInfo placeholder's control collection.

First, don't name the placeholder the same name as a class you're going to
use, it just leads to more confusion. Next, you have to really be sure
exactly what the position of your ContactInfo control is going to be at in
order to access it from the placeholder. Your best bet though, make use if
the control before you add it to the placeholder. You do this by creating
a variable of the type of your control, accessing it's properties, then
adding it to the placeholder. This way you have a strongly typed control
that you can talk to, and when you add it to the placeholder it will then
get rendered.

I'm going to use C# for this example because I haven't used VB in years.

ContactInfo myControl = (ContactInfo)LoadControl("~/ContactInfo.ascx");

Now, I'm using ContactInfo here not as the name of the placeholder, but as
a strongly typed control. Rename the placeholder control to make it
easier. Let's call it plc_ContactInfo for ease.

Now that you have myControl, which your user control, you can access it's
properties and assign your parameters. Then add it to your placeholder
such as plc_ContactInfo.Controls.Add(myControl); (sorry, again in C#)

That makes sense.

Thanks,

Tom
 
Yes there is. In the first example, you have a placeholder. A placeholder
just holds controls so there's no way you're going to reference any sort of
data such as FirstName because it is of type
System.Web.UI.WebControls.PlaceHolder. Your control is a member of the
ContactInfo placeholder's control collection.

First, don't name the placeholder the same name as a class you're going to
use, it just leads to more confusion. Next, you have to really be sure
exactly what the position of your ContactInfo control is going to be at in
order to access it from the placeholder. Your best bet though, make use if
the control before you add it to the placeholder. You do this by creatinga
variable of the type of your control, accessing it'sproperties, then adding
it to the placeholder. This way you have a strongly typed control that you
can talk to, and when you add it to the placeholder it will then get
rendered.

I'm going to use C# for this example because I haven't used VB in years.

ContactInfo myControl = (ContactInfo)LoadControl("~/ContactInfo.ascx");

Now, I'm using ContactInfo here not as the name of the placeholder, but as a
strongly typed control. Rename the placeholder control to make it easier.
Let's call it plc_ContactInfo for ease.

Now that you have myControl, which your user control, you can access it'spropertiesand assign your parameters. Then add it to your placeholder such
as plc_ContactInfo.Controls.Add(myControl); (sorry, again in C#)

Hope this helps,
Mark Fitzpatrick

AFFLUENT INVESTMENTS is your source for world class real estate
investment properties services. There is a reason why we attract
members from all over the world. One simple reason. Returns. That's
why we get more business from referrals than any other source. We know
investment properties. You too can join our world wide network of
investment group members by contacting us at our world headquarters in
Chicago at 312-664-9000.

http://investment.affluentinc.com/
 
In VB 2008, I have a user control added to the page in the PageLoad event-
but thepropertiesare causing me an error.

The program (TakeSurveyTest.aspx) using the control (ContactInfo):

<%@ Page Language="VB" AutoEventWireup="true" Trace="true"
CodeFile="TakeSurveyTest.aspx.vb" Inherits="TakeSurveyTest" %>
...
        <div visible="true" runat="server">
            <asp:PlaceHolder ID="ContactInfo" runat="server"/> <br />
<--------------The error
            <asp:Button ID="SubmitContact" Text="Submit Contact"
OnClick="SaveContact_Click" runat="server" /><br />
        </div>

The ContactInfo control (ContactInfo.ascx):

<%@ Control Language="VB" AutoEventWireup="true"
CodeFile="ContactInfo.ascx.vb" Inherits="ContactInfo" %>

        <td class="style4">
            <asp:TextBox ID="objFirstName" Width="120px" runat="server" />
        </td>
        <td class="style2">
            <asp:TextBox ID="objMiddleName" Width="120px"runat="server" />
        </td>

And the ContactInfo.ascx.vb:

Imports System.Data
Imports System.Data.SqlClient
Imports MyFunctions.BitHandling
Imports System.Drawing
Imports MyFunctions
Imports RolesBasedAuthentication

Partial Class ContactInfo
    Inherits System.Web.UI.UserControl
...
  Public Property FirstName() As String
    Get
      Return objFirstName.Text
    End Get
    Set(ByVal value As String)
      objFirstName.Text = Value
    End Set
  End Property

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

So thepropertiesare being defined but in the TakeSurveyTest.aspx.vb file
is saying ContactInfo doesn't have theseproperties.  I load the conrol in
my PageLoad as:

    ContactInfo.Controls.Add(LoadControl("~/ContactInfo.ascx"))

But these lines give me the error:

    parameters(1).Value = ContactInfo.FirstName
    parameters(2).Value = ContactInfo.LastName

The error is:

'FirstName' is not a member of 'System.Web.UI.WebControls.PlaceHolder'

Which is true, but how do access the ContactInfo parameters?

Thanks,

Tom

http://www.escort-space.com/
 
Back
Top