Attribute to UserControl

P

Pete Davis

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
 
P

Pete Davis

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 said:
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 said:
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
 
K

Karl Seguin

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 said:
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 said:
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 said:
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
 
P

Pete Davis

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 said:
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 said:
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 said:
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!)
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
 
K

Karl Seguin

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 said:
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 said:
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 said:
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 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!)
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
 
G

Guest

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 said:
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 said:
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 said:
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 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!)
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
 

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