Solved

dropdown inside repeater need to find index

Posted on 2008-10-07
7
438 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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