How to add a dropdownlist to a repeater - C#

  • Thread starter Thread starter Darren
  • Start date Start date
D

Darren

Hi There,

I am developing a shopping cart web application in C#. Product
pricing for an item changes based on the weight of product purchased.
ie: 100g=$4.95, 200g=$7.95, etc. Pricing is different for each
product.

When viewing the cart, I would like the the user to have the ability
to select a different weight, specific to that item, which would then
modify the price of that cart item.

Question: How would I add a dropdownlist(specific to each product) to
the repeater?

Thanks in advance,

Darren
 
Darren,
Use the ItemCommand event of your repeater:
rpt.DataSource = dt;
rpt.ItemCommand += new RepeaterCommandEventHandler(rpt_ItemCommand);
rpt.DataBind();

or in VB.net, something like:
 
Darren,
Use the ItemCommand event of your repeater:
rpt.DataSource = dt;
rpt.ItemCommand += new RepeaterCommandEventHandler(rpt_ItemCommand);
rpt.DataBind();

or in VB.net, something like:
rpt.DataSource = dt
AddHandler rpt.ItemCommand, AddressOf rpt_ItemCommand
rpt.DataBind()

Then, in your rpt_ItemCommand function do something like:

private void rpt_ItemCommand(object source, RepeaterCommandEventArgs
e) {
if(e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item){
DropDownList weight =
(DropDownList)e.Item.FindControl("weight");
if(weight != null){
//bind weight here
}
}
}

that's assuming your dropdownlist has an id of "weight". You can access
e.Item.DataItem (cast it to the right type) and access the row information
to figure out what item type it is and thus figure out what to bind.

Karl
 
Hi Karl,

Thanks for your prompt reply.

I attempted to implement your example and I think my order of events is
a little off. I don't quite understand when and where the initial
specific datasource for that repeater item is databinded to the
dropdownlist. Hopefully I am being clear on my problem.

Page_Load - Databinds repeater to datasource
Below is the code for the repeater:

private void rptCart_ItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{

rptCart.ItemCommand += new
RepeaterCommandEventHandler(rptCart_ItemCommand);
rptCart.DataBind();

if(e.Item.ItemType == ListItemType.Item)
{
DropDownList ddnWeight = (DropDownList)e.Item.FindControl("ddnWeight");
if(ddnWeight !=null)
{
//Items will always be null as nothing is bound to that control.
}
}


When run, the dropdownlist is obviously empty. So to pose the question
once again, when, where and how would I bind the dropdownlist to a
subset datasource housing individual sizes for that specific product?
 
in page_load ->
datasource = xxx
itemCommand += new RepeaterEventHander(...)
databind()


in rptCart_ItemCommand-->
if (e...)

)

in other words, put the rptCart.ItemCommand += in the page_load
and don't rembind in the rptCart_ItemCommand function.

Karl
 
Thanks Karl,

One last question: Since each repeater item has a different weight/price
structure, am I to use a conventional loop to 1)obtain unique id for
that product 2)create datasource specifying unique weight/price records
and then bind to that products dropdown list in that repeater row?
 
Not sure I follow the question. If you are simply asking what the best
structure to hold and bind the information...off the top of my head:

I'd have a single datatable with the following columns:
recordId int, displayName string, itemTypeId int

Then as you are binding, I'd get the item's typeId and filter the datatable
on itemTypeID column..if that makes _any_ sense..

Karl
 
Back
Top