473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Ne ws" 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.Attribute s["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 1927
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
news:q6******** ************@gi ganews.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="Ne ws" 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.Attribute s["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******** ********@TK2MSF TNGP10.phx.gbl. ..
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
news:q6******** ************@gi ganews.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="Ne ws" 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.Attribute s["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******@NOSPA M.hotmail.com> wrote in message
news:1M******** ************@gi ganews.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******** ********@TK2MSF TNGP10.phx.gbl. ..
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
news:q6******** ************@gi ganews.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="Ne ws" 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.Attribute s["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 miscommunicatin g here.

You provided the code:

page_load
Comments1.ItemI d = 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******** ******@TK2MSFTN GP15.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******@NOSPA M.hotmail.com> wrote in message
news:1M******** ************@gi ganews.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******** ********@TK2MSF TNGP10.phx.gbl. ..
Pete:
You normally do all of this in codebehind

in the page, you'd do

page_load
Comments1.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
news:q6******** ************@gi ganews.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="Ne ws"

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.Attribute s["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...newsItem Id comes from wherever
your <%=NewsItemId %> came from...

in the page you had:

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

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.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
news:15******** ************@gi ganews.com...
I think we're really miscommunicatin g here.

You provided the code:

page_load
Comments1.ItemI d = 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******** ******@TK2MSFTN GP15.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******@NOSPA M.hotmail.com> wrote in message
news:1M******** ************@gi ganews.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******** ********@TK2MSF TNGP10.phx.gbl. ..
>> Pete:
>> You normally do all of this in codebehind
>>
>> in the page, you'd do
>>
>> page_load
>> Comments1.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
>> news:q6******** ************@gi ganews.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="Ne ws"
> 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.Attribute s["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.datai tem.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.NewsIte mId)

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 miscommunicatin g here.

You provided the code:

page_load
Comments1.ItemI d = 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******** ******@TK2MSFTN GP15.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******@NOSPA M.hotmail.com> wrote in message
news:1M******** ************@gi ganews.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******** ********@TK2MSF TNGP10.phx.gbl. ..
> Pete:
> You normally do all of this in codebehind
>
> in the page, you'd do
>
> page_load
> Comments1.ItemI d = 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******@NOSPA M.hotmail.com> wrote in message
> news:q6******** ************@gi ganews.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="Ne ws"
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.Attribute s["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
395
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 be wired up? All the examples I've seen wire up the event programatically, thus making this declarative attribute irrelevant. e.g.
8
1952
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 that category, the results are paged 10 to a page and the user can page them. As this "Category" usercontrol hardly ever changes I wanted to setup...
2
4614
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 for me! I apologize for the length of this posting, but I wanted to make sure that I get an answer other than "Hey man, just use LoadControl!",...
41
4278
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 on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. ...
12
2191
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 server. It "serves" back the required control (either WebControl or UserControl) based on the contents of an xml file. The code in the webform places...
9
14432
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 does not. The problem is that the webcontrols get created on the OnLoad event of the usercontrol and the event handlers are assigned at the same...
6
12127
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 binding like normal. The problem I am running into is that my UserControl ends up with a different BindingContext then the ParentForm it is...
0
2112
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 sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or...
5
3669
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 UserControl I have a number of Javscript functions. Everything on the page works fine, until I try to add the OnLoad event attribute to the Body tag which...
0
8156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7910
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.