 |
| ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.1 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
|
|
|
|

October 18th, 2004, 01:17 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Composite Control in DataList not retaining proper
Heyah Folks-
Strange behavior I can't seem to explain.
I have a composite server control embedded in a Data List. It is basically an image button with some added functionality to fit some specs. It works just fine when outside of a datalist - that is when it is explicitly typed in the aspx page. I assign it the proper properties in the code-behind, and then processes everything once the imagebutton is clicked.
...but it doesn't seem to work when embedded in a DataList template.
Using the ItemDataBound event of the Datalist, I assign the server control the necessary properties as such:
AddToShoppingCart AddToCart1 = (AddToShoppingCart)e.Item.FindControl("AddToCart1" );
AddToCart1.ProductID = Convert.ToInt32(productID.Text);
AddToCart1.CustomerID = Convert.ToInt32(PortalSettings.CurrentUser.Identit y.ID);
If I click on one of the server controls in runtime (which are iterated for each item in the DataList), it does indeed fire the OnClick event of the Imagebutton - but does not pass along the properties. I have verified all this in debug.
For some reason, the properties of the ServerControl are not being sent as arguments when they are embedded in a DataList.
Any help would be most appreciated.
Here is some of the code for the Server Control:
_____
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RevWorks.Modules.Ecommerce.Business;
namespace Rainbow.DesktopModules.Store.Orders.Controls
{
public class AddToShoppingCart : Control, INamingContainer
{
protected override void CreateChildControls()
{
ImageButton button = new ImageButton();
button.ImageUrl = imageUrl;
Controls.Add(button);
button.Click += new ImageClickEventHandler(this.ButtonClicked);
}
private void ButtonClicked(Object sender, ImageClickEventArgs e)
{
...process code using properties
}
public int CustomerID
{
get{...}
set{...}
}
public int ProductID
{
get{return productID;}
set{productID=value;}
}
protected int Quantity
{
get
{...}
}
public string RedirectUrl
{
get{...}
set{...}
}
private string imageUrl;
private int customerID;
private int productID;
private string redirectUrl;
}
}
|
|

October 18th, 2004, 02:14 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hello,
I would post the code for what you do with it in the Data List (both HTML and code-behind).
Brian
|
|

October 18th, 2004, 04:15 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Indeed:
DATALIST:
<asp:datalist id="resultsList" Runat="server">
<ItemTemplate>
<cc1:addtoshoppingcart id="AddToCart1" runat="server" ImageUrl="../../images/addtocart.gif" />
</ItemTemplate>
</asp:datalist>
CODEBEHIND:
private void resultsList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
AddToShoppingCart AddToCart1 = (AddToShoppingCart)e.Item.FindControl("AddToCart1" );
AddToCart1.ProductID = ...
AddToCart1.CustomerID = ...
AddToCart1.RedirectUrl = ...
}
|
|

October 19th, 2004, 06:47 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
Instead of manually supplying the properties using the ItemDataBound event, try binding them directly as such:
<cc1:addtoshoppingcart id="AddToCart1" runat="server" ImageUrl="../../images/addtocart.gif"
ProductID= '<%# Databinder.Eval(Container.DataItem, "ProductID") %>'
CustomerID = '<%# Databinder.Eval(Container.DataItem, "CustomerID") %>'
RedirectUrl = '<%# Databinder.Eval(Container.DataItem, "RedirectUrl") %>'
/>
Brian
|
|

October 19th, 2004, 05:28 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Nope...doesn't seem to keep it. When the code enters the server control after the postback, it just uses a productID of zero.
Any thoughts?
|
|

October 20th, 2004, 07:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
Are you sure that when you access an item, you don't have to reference the cell, as is with the datagrid? I don't know the data list much, but does this work:
e.item.cells(0).findcontrol("AddToCart1")
Brian
|
|

October 20th, 2004, 01:46 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey Brian-
Nope...no cells member of the item class. And all of the other controls in the DataList are found, populated, and present during ensuing postbacks.
I am convinced that it has something to do with the contruction of the server control - can you see anything there that I am lacking? I have done a silly amount of research on this, but couldn't find anything. the difficulty of not knowing what to look for doesn't help.
I tried rebuilding the entire DataList fresh with no clutter aside from the embedded server control - no luck.
Ben
|
|

October 20th, 2004, 02:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Well, your issue maybe databinding. Here is a site I found on databound controls. You have to dig down through it. Maybe it will help you: http://www.codeproject.com/books/ASPNETCookbook.asp. It maybe how you retain the data in private variables; that may not be sufficient.
HTH, Brian
|
|

October 20th, 2004, 03:34 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
YES!
thank you, brian. The section in the article on adding viewstate support did the trick.
In the control, I needed to change the Property ID to this:
public int ProductID
{
get
{
if(ViewState["productID"].ToString() !=null)
return Convert.ToInt32(ViewState["productID"]);
else
return -1;
}
set
{
ViewState["productID"] = value;
}
}
|
|
 |