Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Adding and populating a custom property for a user control

Reply
Thread Tools

Adding and populating a custom property for a user control

 
 
Mike Owen
Guest
Posts: n/a
 
      07-20-2007
I have built and used a custom control which works fine.

I now want to add a property that I can set from the various pages where I
use the control.

I have added the following property in the code behind file:

Public Property ContentTypeID() As Int16
Get

Return mContentTypeID

End Get
Set(ByVal value As Int16)

mContentTypeID = value

End Set
End Property

and set it in the page as follows:

<%@ Register TagPrefix="uc1" TagName="_Content" Src="~/_Content.ascx" %>
<uc1:_Content id=_Content ContentTypeID=10 runat="server"></uc1:_Content>

However in design time the error 'Attribute ContentTypeID is not a valid
attribute of element _Content' comes up for the property/attribute
ContentTypeID, and of course the value of 10 is not passed through to the
code behind file.

Any ideas what I am doing wrong?


Thanks, Mike.
 
Reply With Quote
 
 
 
 
Manish Bafna
Guest
Posts: n/a
 
      07-21-2007
Hi,
Following code is working perfectly well in my machine:
[1]Code in WebUserControl.ascx.vb
Partial Class WebUserControl
Inherits System.Web.UI.UserControl
Dim mContentTypeID As Int16

Public Property ContentTypeID() As Int16
Get
Return mContentTypeID
End Get
Set(ByVal value As Int16)

mContentTypeID = value
End Set
End Property
End Class
[2]The aspx page in which usercontrol is included(Default.vb)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<%@ Register Src="~/WebUserControl.ascx" TagName="_Content" TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:_Content id="_Content" ContentTypeID="10"
runat="server"></uc1:_Content>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>

</body>
</html>
[3]Code in 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
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label1.Text = _Content.ContentTypeID
End Sub
End Class

Compare with your code and figure out where you have missed out
--
Hope this answers your question.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"Mike Owen" wrote:

> I have built and used a custom control which works fine.
>
> I now want to add a property that I can set from the various pages where I
> use the control.
>
> I have added the following property in the code behind file:
>
> Public Property ContentTypeID() As Int16
> Get
>
> Return mContentTypeID
>
> End Get
> Set(ByVal value As Int16)
>
> mContentTypeID = value
>
> End Set
> End Property
>
> and set it in the page as follows:
>
> <%@ Register TagPrefix="uc1" TagName="_Content" Src="~/_Content.ascx" %>
> <uc1:_Content id=_Content ContentTypeID=10 runat="server"></uc1:_Content>
>
> However in design time the error 'Attribute ContentTypeID is not a valid
> attribute of element _Content' comes up for the property/attribute
> ContentTypeID, and of course the value of 10 is not passed through to the
> code behind file.
>
> Any ideas what I am doing wrong?
>
>
> Thanks, Mike.

 
Reply With Quote
 
 
 
 
Mike Owen
Guest
Posts: n/a
 
      07-22-2007
Hi Manish,

I was actually trying to do it in a more abstract way because I am using
skins on the site, but having looked at it it was essential that I did it my
way, the only benefit being it fitted in with the rest of the code on the
site.

I tried it your way and it worked fine.

Thanks for your help.


Cheers, Mike.

"Manish Bafna" wrote:

> Hi,
> Following code is working perfectly well in my machine:
> [1]Code in WebUserControl.ascx.vb
> Partial Class WebUserControl
> Inherits System.Web.UI.UserControl
> Dim mContentTypeID As Int16
>
> Public Property ContentTypeID() As Int16
> Get
> Return mContentTypeID
> End Get
> Set(ByVal value As Int16)
>
> mContentTypeID = value
> End Set
> End Property
> End Class
> [2]The aspx page in which usercontrol is included(Default.vb)
> <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
> Inherits="_Default" %>
> <%@ Register Src="~/WebUserControl.ascx" TagName="_Content" TagPrefix="uc1" %>
> <!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>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <uc1:_Content id="_Content" ContentTypeID="10"
> runat="server"></uc1:_Content>
> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
> <asp:Button ID="Button1" runat="server" Text="Button" />
> </div>
> </form>
>
> </body>
> </html>
> [3]Code in 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
> End Sub
> Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> Label1.Text = _Content.ContentTypeID
> End Sub
> End Class
>
> Compare with your code and figure out where you have missed out
> --
> Hope this answers your question.
> Thanks and Regards.
> Manish Bafna.
> MCP and MCTS.
>
>
>
> "Mike Owen" wrote:
>
> > I have built and used a custom control which works fine.
> >
> > I now want to add a property that I can set from the various pages where I
> > use the control.
> >
> > I have added the following property in the code behind file:
> >
> > Public Property ContentTypeID() As Int16
> > Get
> >
> > Return mContentTypeID
> >
> > End Get
> > Set(ByVal value As Int16)
> >
> > mContentTypeID = value
> >
> > End Set
> > End Property
> >
> > and set it in the page as follows:
> >
> > <%@ Register TagPrefix="uc1" TagName="_Content" Src="~/_Content.ascx" %>
> > <uc1:_Content id=_Content ContentTypeID=10 runat="server"></uc1:_Content>
> >
> > However in design time the error 'Attribute ContentTypeID is not a valid
> > attribute of element _Content' comes up for the property/attribute
> > ContentTypeID, and of course the value of 10 is not passed through to the
> > code behind file.
> >
> > Any ideas what I am doing wrong?
> >
> >
> > Thanks, Mike.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asp.net User Control or Custom control Property doesnt accept inlineasp.net constract <%= %> is there a workaround? AleXmanFree ASP .Net 3 10-09-2008 07:39 PM
Adding and populating a custom property for a user control Mike Owen ASP .Net Web Controls 0 07-20-2007 05:46 PM
feeding a SQLDataSource embedded in an .ascx user control a custom property assigned to that control ASP .Net 4 07-16-2006 12:20 PM
Dynamically Adding User Control with Child User Control Travis Pruitt ASP .Net Building Controls 0 01-13-2005 08:49 PM
Help!!Adding Property(XmlDocument ) to your custom control !! Ozan Kozan via .NET 247 ASP .Net 0 08-04-2004 12:50 PM



Advertisments