473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Attribute to UserControl

I'm fairly weak with ASP.NET. What I'm trying to do is pass an attribute to
a UserControl. The attribute is a value from the CodeBehind for the page.

So in my .aspx I have the following:

<cm:Comments runat="server" ID="Comments1" CommentType="News" ItemID="<%=
NewsItemID %>" />

NewsItemID is a property in the CodeBehind for the page.

However when I get to the Page_Load of the UserControl and do the following:

int itemID = Convert.ToInt32(this.Attributes["ItemID"]);

it throws an exception because the value of this.Attributes["ItemID"] is <%=
NewsItemID %>

Obviously, not what I wanted.

Is there a proper way to do this?

Thanks.

Pete
Nov 19 '05 #1
6 1905
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemId = newsItemId
end sub

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:q6********************@giganews.com...
I'm fairly weak with ASP.NET. What I'm trying to do is pass an attribute
to
a UserControl. The attribute is a value from the CodeBehind for the page.

So in my .aspx I have the following:

<cm:Comments runat="server" ID="Comments1" CommentType="News" ItemID="<%=
NewsItemID %>" />

NewsItemID is a property in the CodeBehind for the page.

However when I get to the Page_Load of the UserControl and do the
following:

int itemID = Convert.ToInt32(this.Attributes["ItemID"]);

it throws an exception because the value of this.Attributes["ItemID"] is
<%=
NewsItemID %>

Obviously, not what I wanted.

Is there a proper way to do this?

Thanks.

Pete

Nov 19 '05 #2
Okay, but how is the Comments UserControl's CodeBehind going to know what
the NewsItemID is from the page's CodeBehind? Does that make sense? The
CodeBehind for the page has the NewsItemID. I'm trying to pass that to the
UserControl.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemId = newsItemId
end sub

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:q6********************@giganews.com...
I'm fairly weak with ASP.NET. What I'm trying to do is pass an attribute
to
a UserControl. The attribute is a value from the CodeBehind for the page.
So in my .aspx I have the following:

<cm:Comments runat="server" ID="Comments1" CommentType="News" ItemID="<%= NewsItemID %>" />

NewsItemID is a property in the CodeBehind for the page.

However when I get to the Page_Load of the UserControl and do the
following:

int itemID = Convert.ToInt32(this.Attributes["ItemID"]);

it throws an exception because the value of this.Attributes["ItemID"] is
<%=
NewsItemID %>

Obviously, not what I wanted.

Is there a proper way to do this?

Thanks.

Pete


Nov 19 '05 #3
You can take a look at:
http://www.openmymind.net/index.aspx?documentId=9#3.1

But why does the comment's user control need to know WHERE the NewsItemId
comes from? It shouldn't. It should simply need to know WHAT it is....

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:1M********************@giganews.com...
Okay, but how is the Comments UserControl's CodeBehind going to know what
the NewsItemID is from the page's CodeBehind? Does that make sense? The
CodeBehind for the page has the NewsItemID. I'm trying to pass that to the
UserControl.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemId = newsItemId
end sub

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:q6********************@giganews.com...
> I'm fairly weak with ASP.NET. What I'm trying to do is pass an
> attribute
> to
> a UserControl. The attribute is a value from the CodeBehind for the page. >
> So in my .aspx I have the following:
>
> <cm:Comments runat="server" ID="Comments1" CommentType="News" ItemID="<%= > NewsItemID %>" />
>
> NewsItemID is a property in the CodeBehind for the page.
>
> However when I get to the Page_Load of the UserControl and do the
> following:
>
> int itemID = Convert.ToInt32(this.Attributes["ItemID"]);
>
> it throws an exception because the value of this.Attributes["ItemID"]
> is
> <%=
> NewsItemID %>
>
> Obviously, not what I wanted.
>
> Is there a proper way to do this?
>
> Thanks.
>
> Pete
>
>



Nov 19 '05 #4
I think we're really miscommunicating here.

You provided the code:

page_load
Comments1.ItemId = newsItemId
end sub

So my question is: Where does newsItemId come from? Originally, it's in the
page code behind.. I can't get it into the attributes for the UserControl
the way I'm trying (please see original question as to why).

The page you referred me to shows how to send static attributes to a user
control, but my attribute is dynamic.

So I'm still not sure what you're trying to tell me.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:er**************@TK2MSFTNGP15.phx.gbl...
You can take a look at:
http://www.openmymind.net/index.aspx?documentId=9#3.1

But why does the comment's user control need to know WHERE the NewsItemId
comes from? It shouldn't. It should simply need to know WHAT it is....

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:1M********************@giganews.com...
Okay, but how is the Comments UserControl's CodeBehind going to know what the NewsItemID is from the page's CodeBehind? Does that make sense? The
CodeBehind for the page has the NewsItemID. I'm trying to pass that to the UserControl.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemId = newsItemId
end sub

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:q6********************@giganews.com...
> I'm fairly weak with ASP.NET. What I'm trying to do is pass an
> attribute
> to
> a UserControl. The attribute is a value from the CodeBehind for the

page.
>
> So in my .aspx I have the following:
>
> <cm:Comments runat="server" ID="Comments1" CommentType="News"

ItemID="<%=
> NewsItemID %>" />
>
> NewsItemID is a property in the CodeBehind for the page.
>
> However when I get to the Page_Load of the UserControl and do the
> following:
>
> int itemID = Convert.ToInt32(this.Attributes["ItemID"]);
>
> it throws an exception because the value of this.Attributes["ItemID"]
> is
> <%=
> NewsItemID %>
>
> Obviously, not what I wanted.
>
> Is there a proper way to do this?
>
> Thanks.
>
> Pete
>
>



Nov 19 '05 #5
My exmaple is the codebehind from the page...newsItemId comes from wherever
your <%=NewsItemId%> came from...

in the page you had:

<cm:Comments runat="server" ID="Comments1" CommentType="News" />

in the page's codebehind you have

Protected comments1 as Comments

public sub page_load
dim newsItemId as integer = 0 'in your code get this dynamically from
querystring, db, doesn't matter
comments1.ItemId = newsItemId
end sub
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:15********************@giganews.com...
I think we're really miscommunicating here.

You provided the code:

page_load
Comments1.ItemId = newsItemId
end sub

So my question is: Where does newsItemId come from? Originally, it's in
the
page code behind.. I can't get it into the attributes for the UserControl
the way I'm trying (please see original question as to why).

The page you referred me to shows how to send static attributes to a user
control, but my attribute is dynamic.

So I'm still not sure what you're trying to tell me.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:er**************@TK2MSFTNGP15.phx.gbl...
You can take a look at:
http://www.openmymind.net/index.aspx?documentId=9#3.1

But why does the comment's user control need to know WHERE the NewsItemId
comes from? It shouldn't. It should simply need to know WHAT it is....

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:1M********************@giganews.com...
> Okay, but how is the Comments UserControl's CodeBehind going to know what > the NewsItemID is from the page's CodeBehind? Does that make sense? The
> CodeBehind for the page has the NewsItemID. I'm trying to pass that to the > UserControl.
>
> Pete
>
> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
>> Pete:
>> You normally do all of this in codebehind
>>
>> in the page, you'd do
>>
>> page_load
>> Comments1.ItemId = newsItemId
>> end sub
>>
>> karl
>>
>> --
>> MY ASP.Net tutorials
>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>> annoying)
>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>> come!)
>> "Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
>> news:q6********************@giganews.com...
>> > I'm fairly weak with ASP.NET. What I'm trying to do is pass an
>> > attribute
>> > to
>> > a UserControl. The attribute is a value from the CodeBehind for the
> page.
>> >
>> > So in my .aspx I have the following:
>> >
>> > <cm:Comments runat="server" ID="Comments1" CommentType="News"
> ItemID="<%=
>> > NewsItemID %>" />
>> >
>> > NewsItemID is a property in the CodeBehind for the page.
>> >
>> > However when I get to the Page_Load of the UserControl and do the
>> > following:
>> >
>> > int itemID = Convert.ToInt32(this.Attributes["ItemID"]);
>> >
>> > it throws an exception because the value of
>> > this.Attributes["ItemID"]
>> > is
>> > <%=
>> > NewsItemID %>
>> >
>> > Obviously, not what I wanted.
>> >
>> > Is there a proper way to do this?
>> >
>> > Thanks.
>> >
>> > Pete
>> >
>> >
>>
>>
>
>



Nov 19 '05 #6
Excuse me for chiming in here, but if you're trying to databind the syntax is
this: '<%# NewsItemId %>' (# instead of =), presuming NewsItemId is publicly
accessible (else you might use container.dataitem.NewsItemId if it's coming
from a datasource).

Or you can do it in the code-behind as Karl says. If there's only one
instance of the user control (i.e. it's not a bunch of them in a grid), I
wouldn't bother with databinding, just set the control in the code, e.g. the
user control exposes property NewsItemId, and the parent page sets the value
of that control where it makes sense. Or, on load of the user control, you
can go get the NewsItemId from the parent page, but imho it's generally weak
design for the user control to count on the internals of the parent page
(also, don't forget that init of the user controls happens before init of the
page, but load of the controls happens *after* load of the page).

To make your original example work, in the user control I'd do (vb syntax):

dim myPage as parentPageClass = ctype(me.page, parentPageClass)
dim itemId as integer = convert.toint32(myPage.NewsItemId)

You need to cast the page of the user control to whatever its real type is,
else the system can't know it's got a NewsItemId property.

hth,

Bill

"Pete Davis" wrote:
I think we're really miscommunicating here.

You provided the code:

page_load
Comments1.ItemId = newsItemId
end sub

So my question is: Where does newsItemId come from? Originally, it's in the
page code behind.. I can't get it into the attributes for the UserControl
the way I'm trying (please see original question as to why).

The page you referred me to shows how to send static attributes to a user
control, but my attribute is dynamic.

So I'm still not sure what you're trying to tell me.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:er**************@TK2MSFTNGP15.phx.gbl...
You can take a look at:
http://www.openmymind.net/index.aspx?documentId=9#3.1

But why does the comment's user control need to know WHERE the NewsItemId
comes from? It shouldn't. It should simply need to know WHAT it is....

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:1M********************@giganews.com...
Okay, but how is the Comments UserControl's CodeBehind going to know what the NewsItemID is from the page's CodeBehind? Does that make sense? The
CodeBehind for the page has the NewsItemID. I'm trying to pass that to the UserControl.

Pete

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
> Pete:
> You normally do all of this in codebehind
>
> in the page, you'd do
>
> page_load
> Comments1.ItemId = newsItemId
> end sub
>
> karl
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
> "Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
> news:q6********************@giganews.com...
> > I'm fairly weak with ASP.NET. What I'm trying to do is pass an
> > attribute
> > to
> > a UserControl. The attribute is a value from the CodeBehind for the
page.
> >
> > So in my .aspx I have the following:
> >
> > <cm:Comments runat="server" ID="Comments1" CommentType="News"
ItemID="<%=
> > NewsItemID %>" />
> >
> > NewsItemID is a property in the CodeBehind for the page.
> >
> > However when I get to the Page_Load of the UserControl and do the
> > following:
> >
> > int itemID = Convert.ToInt32(this.Attributes["ItemID"]);
> >
> > it throws an exception because the value of this.Attributes["ItemID"]
> > is
> > <%=
> > NewsItemID %>
> >
> > Obviously, not what I wanted.
> >
> > Is there a proper way to do this?
> >
> > Thanks.
> >
> > Pete
> >
> >
>
>



Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Oisin Grehan | last post by:
Hi, I have a UserControl derived class: <ns:votingbutton runat="server" id="btn1" onclick="votingbuttonclick" /> My question is, what code do I need in place in the codebehind for this to...
8
by: Raed Sawalha | last post by:
Hi, I have a strange problem with a usercontrol on a page. The usercontrol dispalyes three categories (From a database) when the user clicks a category they see all the products in a shop for...
2
by: Sascha | last post by:
Hi there, I searched carefully through the web before finally deciding to post this message, because I could not find a solution for my problem. Hopefully someone will have a hint or explanation...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
6
by: MeowCow | last post by:
I have created a UserControl that encapsulates a third party data grid. My goal was to create my own DataSource and DataMember properties that forward the binding to the third party grid, then use...
0
by: murl | last post by:
I want to create a web user control with a base class of 'GridView' so I can wrap the needed methods to make rows clickable. However when building the project I get the following message: "Make...
5
by: John Kotuby | last post by:
Hi all, My application consists of a MasterPage which is used for all WebForms. I then create UserControls to populate the content area for the different WebForms. In one complicated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.