Solved

dropdown inside repeater need to find index

Posted on 2008-10-07
7
439 Views
Last Modified: 2013-12-17
Hi all, wondering if i get some tips on how to get the index from dropdownlist inside a control. If you look at the code snippet below i need to get the basketItemId and related quantity.

thanks

<asp:Repeater ID="BasketItemRepeater" runat="server" 
                    onitemdatabound="BasketItemRepeaterDataBound">
                <ItemTemplate>                        
                  <%# DataBinder.Eval(Container.DataItem, "BasketItemId")%>
     
                            <asp:DropDownList ID="QuantityDropDownList" runat="server" AutoPostBack="True" onselectedindexchanged="UpdateQuantity" EnableViewState="true" >
                            </asp:DropDownList>                           
 
                    </ItemTemplate>
 </asp:Repeater>
 
    protected void UpdateQuantity(object sender, EventArgs e)
    {
        DropDownList d = (DropDownList) sender;
 
        // in here i need to get the basketItemId and update the quantity
    }

Open in new window

0
Comment
Question by:frosty1
  • 3
  • 3
7 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22662277
you have to iterate through the controls if they are inside of grids, repeaters, etc....
0
 

Author Comment

by:frosty1
ID: 22662450
thanks for the speedy response. In order to use Item.FindControl i have change EventArgs to RepeaterItemEventArgs. However now i am getting

Compiler Error Message: CS0123: No overload for 'UpdateQuantity' matches delegate 'System.EventHandler'

Here is my code:

          protected void UpdateQuantity(object sender, RepeaterItemEventArgs e)
    {
        DropDownList d = (DropDownList) sender;

        HiddenField basketItemId;
        basketItemId = (HiddenField)e.Item.FindControl("HiddenFieldBasketItemId");


        string mystring = d.Text + basketItemId.Value;

        Response.Write(mystring);
     

    }
0
 
LVL 21

Expert Comment

by:silemone
ID: 22662703
oh...you can't do that...you have to leave the argument that you had before in there...that's why you're getting that exception, because UpdateQuantity is expecting EventArgs e....reason being is that the sender is a Dropdownlist, so the Generic or DDL event must be associated with that sender...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:silemone
ID: 22663288
What you can do is just use the RepeaterID and access the Item.FindControl that way...
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22664153
Try this:

protected void UpdateQuantity(object sender, EventArgs e)
{
	DropDownList d = (DropDownList) sender;
 
	// in here i need to get the basketItemId and update the quantity
	RepeaterItem item = (RepeaterItem)d.NamingContainer;
	int i = item.ItemIndex;   // here you get the index of the row that fires the event
	// Continue your update process with the index 
}

Open in new window

0
 

Author Comment

by:frosty1
ID: 22664345
am i using the right method or should i be using ItemCommand. Here what i've got


    protected void UpdateQuantity(object sender, EventArgs e)
    {
        DropDownList quantityDropDownList = (DropDownList)BasketItemRepeater.FindControl("QuantityDropDownList");
        string myString = quantityDropDownList.SelectedValue;
        Label2.Text = myString;
    }
0
 

Author Comment

by:frosty1
ID: 22664435
sorry i prairiedog i was writting my response to the previous post.

Anyway, working perfectly. Thanks silemone:& prairiedog very handy

I'll post the full code tomorrow for reference.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question