Solved

ASP.Net - handling event from Dropdown inside a Repeater

Posted on 2008-10-22
5
782 Views
Last Modified: 2013-11-07
Hello,
I have dropdown list inside each row of my repeater control. What I need to do is handle the event when a user changes the selected item.

I have attached my repeater code. Here is the code I use for populating the drop downs:

 
    protected void rptTable_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRow row = e.Item.DataItem as DataRow;
            DropDownList ddlReimbursement = e.Item.FindControl("ddlStatus") as DropDownList;

            ListItem li0 = new ListItem("No Reimbursement", "1");
            ListItem li1 = new ListItem("Partially Reimbursed", "2");
            ListItem li2 = new ListItem("Fully Reimbursed", "3");

            ddlReimbursement.Items.Add(li0);
            ddlReimbursement.Items.Add(li1);
            ddlReimbursement.Items.Add(li2);

            DataRowView drv = e.Item.DataItem as DataRowView;
            ddlReimbursement.SelectedValue = drv.Row.ItemArray[16].ToString();
        }
    }

Please can somebody help me or point me in the right direction for handling the dropdownList selectedIndexChanged event? Thanks


<asp:Repeater ID="rptTable" runat="server" EnableViewState="false" OnItemDataBound="rptTable_ItemDataBound" OnItemCommand="rptTable_ItemCommand">
    <HeaderTemplate>
     </HeaderTemplate>
    <ItemTemplate>
        <tr>     
            <td><asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="true" EnableViewState="false"></asp:DropDownList></td>
       </tr>
    </ItemTemplate>

Open in new window

0
Comment
Question by:CodeSmarterNotHarder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 450 total points
ID: 22775330
ddlReimbursement.SelectedIndexChanged += delegate(object sender,EventArgs e)
{
      DropDownList lst = sender as DropDownList;
      //do something here
};
0
 

Author Comment

by:CodeSmarterNotHarder
ID: 22775437
Hi Crazyman,
I've added this to my code but seem to have something wrong as I'm getting build errors.

Error      3      A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something

Code behind

    protected void rptTable_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRow row = e.Item.DataItem as DataRow;
            DropDownList ddlReimbursement = e.Item.FindControl("ddlStatus") as DropDownList;

            ListItem li0 = new ListItem("No Reimbursement", "1");
            ListItem li1 = new ListItem("Partially Reimbursed", "2");
            ListItem li2 = new ListItem("Fully Reimbursed", "3");

            ddlReimbursement.Items.Add(li0);
            ddlReimbursement.Items.Add(li1);
            ddlReimbursement.Items.Add(li2);

            DataRowView drv = e.Item.DataItem as DataRowView;
            ddlReimbursement.SelectedValue = drv.Row.ItemArray[16].ToString();

            ddlReimbursement.SelectedIndexChanged += delegate(object sender, EventArgs e) { };
        }
    }


    protected void ddlReimbursement_SelectedIndexChanged(object sender, EventArgs e)
    {
    }


Thanks
0
 

Author Comment

by:CodeSmarterNotHarder
ID: 22775488
Ah, sorry I have got it to build by renaming e and sender. That code compiles but I have added the handler and put a break point on it and it is not being hit when I select any of the dropdown items. It posts back but does not break on the SelectedIndexChanged event!!

    protected void rptTable_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //populating the drop downs
            DataRow row = e.Item.DataItem as DataRow;
            DropDownList ddlReimbursement = e.Item.FindControl("ddlStatus") as DropDownList;

            ListItem li0 = new ListItem("No Reimbursement", "1");
            ListItem li1 = new ListItem("Partially Reimbursed", "2");
            ListItem li2 = new ListItem("Fully Reimbursed", "3");

            ddlReimbursement.Items.Add(li0);
            ddlReimbursement.Items.Add(li1);
            ddlReimbursement.Items.Add(li2);

            //create the handler
            ddlReimbursement.SelectedIndexChanged += delegate(object sender1, EventArgs ex) { };
        }
    }

    //neither of these breakpoints are being hit :(
    protected void ddlReimbursement_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
0
 
LVL 13

Assisted Solution

by:crazyman
crazyman earned 450 total points
ID: 22775692
It wont because your using an anonymous delegate!
The code that will fire will be inside the
delegate(object sender1, EventArgs ex) { };


If you want to do it like that simply add
<asp:DropDownList ID="ddlStatus" runat="server" OnSelectecIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true" EnableViewState="false"></asp:DropDownList>
0
 
LVL 6

Assisted Solution

by:AmarIs26
AmarIs26 earned 50 total points
ID: 22775921
ddlReimbursement.SelectedIndexChanged += delegate(object sender1, EventArgs ex) { };
The above line should be
ddlReimbursement.SelectedIndexChanged
+=new EventHandler(ddlReimbursement_SelectedIndexChanged);
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I hide MVVM menu items in WPF/XAML? 1 36
Reactjs with .NET 3 76
linq .concat equivalent with expression syntax 4 25
Converting PHP triple des decrypt to c# 9 75
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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