Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2004, 02:36 PM
Authorized User
 
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default UserControls in DataLists

Another similar question, but with a UserControl.
<asp:datalist id="ProdList" runat="server" DataKeyField="productTypeID" OnItemCommand="ProdList_ItemCommand">
<ItemTemplate>
<nolimitengin:AlsoBought id="AlsoBought1" runat="server"></nolimitengin:AlsoBought>
<BR>
<asp:Button id="Button2" runat="server" CssClass="ProductListButton" ToolTip="Show/Hide Details" Width="65px" Text="Details" CommandName="also"></asp:Button>

In the attached .aspx code page is:
public UserControl itemBought;

public void ProdList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) {
// using that index, get a handle on the UserControl
UserControl itemBought = (UserControl)ProdList.Items[ProdList.SelectedIndex].FindControl("AlsoBought1");
. . . .
itemBought.alsoBoughtList.DataSource = productCatalogue.GetProductsAlsoPurchased(productT ypeID1, sqlConnectionType);

and in the UserControl pages _AlsoBought.ascx.cs is
protected System.Web.UI.WebControls.Repeater alsoBoughtList;
protected System.Web.UI.WebControls.HyperLink LinkBought;


and when I run this it gives an error saying itemBought does not have an .alsoBoughtList.

Why doesn't this work? What should the code be to obtain a handle to the repeater and it's hyperlink?

Thanks in advance for your help.


Sandy
__________________
Sandy
 
Old April 2nd, 2004, 10:18 PM
Authorized User
 
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This question never did come up in the active list.

Sandy
 
Old April 2nd, 2004, 10:22 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

It is in the active list now (whether or not it was before).

----------
---Snib---
----------

<><
 
Old April 3rd, 2004, 05:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Sandy,

It looks like you are casting your AlsoBought1 control to a generic UserControl control. Of course, a User Control does not know anything about a alsoBoughtList property.
You'll need to cast your control to its original type:

MyCoolListControl itemBought = (MyCoolListControl)ProdList.Items[ProdList.SelectedIndex].FindControl("AlsoBought1");

(Assuming the type of your control is MyCoolListControl)

Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old April 3rd, 2004, 12:01 PM
Authorized User
 
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've tried changing
public UserControl itemBought;
to
public AlsoBought1 itemBought; produces Error "The type or namespace could not be found"

And I've tried changing
UserControl itemBought = (UserControl)ProdList.Items[ProdList.SelectedIndex].FindControl("AlsoBought1");
to
AlsoBought1 itemBought = (AlsoBought1)ProdList.Items[ProdList.SelectedIndex].FindControl("AlsoBought1");produces Error "The type or namespace could not be found"


The .aspx file contains, at the top:
<%@ Register TagPrefix="NoLimitEngin" TagName="AlsoBought" Src="_AlsoBought.ascx" %>
and inside the DataList
<nolimitengin:AlsoBought id="AlsoBought1" runat="server"></nolimitengin:AlsoBought>



Sandy
 
Old April 3rd, 2004, 04:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Sandy,

You shouldn't cast it to the instance name of the control, but to the type name. Of the class name of the control is AlsoBought, then you should cast it to AlsoBought, and not to AlsoBought1:

public AlsoBought1 itemBought; produces Error "The type or namespace could not be found"

AlsoBought itemBought = (AlsoBought) ProdList.Items[ProdList.SelectedIndex].FindControl("AlsoBought1");

This way, you cast the Control instance called AlsoBought1 to a Control type named AlsoBought.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old April 3rd, 2004, 07:05 PM
Authorized User
 
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

AlsoBought itemBought = (AlsoBought)ProdList.Items[ProdList.SelectedIndex].FindControl("AlsoBought1");

produces the same error.

And the declarations:
public AlsoBought AlsoBought1;
public AlsoBought itemBought;
public AlsoBought1 itemBought; and
public AlsoBought1 AlsoBought;
produces the same error.

In fact, the only declaration I can make up that does not produce that error is:
public UserControl itemBought;

I understand that UserControl itself does not have a AlsoBoughtList, but so far nothing does.



Sandy
 
Old April 3rd, 2004, 07:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, you should be able to cast the UserControl back to its own appropriate type.

Are you sure both controls are in the same namespace?
You can easily check that by looking at the code behind for for your control. You'll see the Namespace and the Class (like public AlsoBought : System.Web......). Make sure that the current page can *see* the User Control; e.g. they are either in the same namespace, or the page uses a using statement to import the namespace where the control lives.

If that doesn't help, can you post a part of the code for the User Control? That is, just the general skeleton, not the implementation?

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old April 4th, 2004, 10:59 PM
Authorized User
 
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar, I really appreciate your help. The .ascx.cs page has:
using System.Web.UI.WebControls;

namespace NoLimitEngin
{
    public abstract class C_AlsoBought : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.Repeater alsoBoughtList;
        protected System.Web.UI.WebControls.HyperLink LinkBought;
        public int ProductTypeID;
. . . . .
and no real code in it. The .aspx.cs page has:
using System.Web.UI.WebControls;

namespace NoLimitEngin
{
    public class ProductsList : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataList ProdList;
        public UserControl itemBought;
        public DataGrid itemGrid;
. . . .
So far there's no code in the UserControl. I'm still trying to get it to exist and compile and run.


Sandy
 
Old April 5th, 2004, 01:25 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Sandy,

Why are you using an abstract class for the User Control? At what point or location are you deriving from this class?

Usually, you'll use something like this in the Code Behind for the User Control:

public class MyControl : System.Web.UI.UserControl

This way, the type of your control is MyControl.

In your situation, your abstract class is called C_AlsoBought, so I would expect another control to derive from it, like this:

public class AlsoBought : C_AlsoBought
{
  // Implementation for non-abstract, implementing class here
}

Then in your ASPX pages, you would use AlsoBought as the type of the control.

However, I don't see the need for this abstract class (although I may be overlooking something). Instead, in your User Control, have a normal class called AlsoBought that directly derives from UserControl. Implement behavior for this class directly.
In your ASPX pages, use this type for your user control, like this:

public AlsoBought itemBought;

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Usercontrols yveerabhadrarao ADO.NET 1 August 3rd, 2007 06:57 AM
Javascript in UserControls chakravarthy_vb Classic ASP Components 1 March 30th, 2005 06:35 AM
Linking UserControls dhanyavnair Pro VB 6 2 December 1st, 2004 02:41 PM
Labels in DataLists shmacgregor ASP.NET 1.0 and 1.1 Basics 4 April 2nd, 2004 03:28 AM
Reusability of UserControls jara VS.NET 2002/2003 0 August 14th, 2003 05:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.