PDA

View Full Version : ASP .net: Retriving values from controls


oracleguy
07-09-2003, 09:37 PM
I have a problem with retriving values from controls in asp .net

Here is the code:


<asp:Repeater id="rptProducts" runat="server">
<ItemTemplate>
<h8>
<b> <%# Container.DataItem ("ProductName")%></b>
<asp:Button id="btnNewPhase" runat="server" BackColor="Blue" ForeColor="White" Text="New Phase" Font-Size="XX-Small" Width="60px" ToolTip="Click this button to create a new phase for this product"></asp:Button>
</h8>
<asp:DataList ID="dlstPhases" Runat="Server" DataSource='<%# FilterPhases( Container.DataItem( "ProductID" ) )%>' RepeatColumns='5' RepeatDirection="Horizontal" CellSpacing="20">
<ItemTemplate>
<a href='<%# String.Format( "CDContentsEdit.aspx?id={0}&product={1}",Container.DataItem( "CDID" ),"Product Name") %>' > <%# Container.DataItem( "Phase" ) %> </a>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:Repeater>

I'm trying to obtain the value of the data item "ProductName" from rptProducts and insert it where the red text is. However I can figure out how to obtain values from controls higher up.

Any ideas?

ReyN
07-10-2003, 03:51 PM
hi

if you meant accessing a data item in the parent repeater's itemtemplate, try Parent.Container.DataItem ( "Productname" ).

also, String.Format expects the same number of parameters as the number of objects you refer to. you are referring only to objects {0} and {1}, but you are passing three parameters.

oracleguy
07-10-2003, 07:22 PM
Originally posted by ReyN
hi

if you meant accessing a data item in the parent repeater's itemtemplate, try [b]Parent.Container.DataItem ( "Productname" ).


That didn't seem to do it. It says Container isn't a member of Parent.

ReyN
07-11-2003, 02:58 AM
sorry, Parent returns a generic Control class that represents the object which contains the DataList, in this case a RepeaterItem.

it would be easier to reference this object in the onItemDataBound of the child DataList. so this should work:

in C#

( ( RepeaterItem ) dlstPhases.Parent ).DataItem [ "ProductName" ]

in VB.NET, that should be

CType ( dlstPhases.Parent, RepeaterItem ).DataItem ( "ProductName" )

to get to the parent object in question.

oracleguy
07-11-2003, 04:26 AM
Okay... I'll give it a try when I get to work tomorrow.

oracleguy
07-11-2003, 07:05 PM
That didn't exactly do it, but it got me thinking and I figured it out.

I had to do it on the datalist item created event, the other one happened too late.

Here is the code:

Sub Item_Created(sender As Object, e As DataListItemEventArgs)
strProductName=CType ( sender.Parent, RepeaterItem ).DataItem ( "ProductName" )
End Sub

And I added: OnItemCreated="Item_Created" to the datalist control.

It's amazing how difficult they make these things in .net

It should just let me say dlstPhases.Items.DataItem("Product Name") but it just gives me "dlstPhases is not declared" :mad:

But I did figure it out nonetheless. Thanks ReyN. :thumbsup: