?
Solved

ASP.Net - handling event from Dropdown inside a Repeater

Posted on 2008-10-22
5
Medium Priority
?
795 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 1800 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 1800 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 200 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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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