Solved

ASP.Net - handling event from Dropdown inside a Repeater

Posted on 2008-10-22
5
790 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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