Solved

ASP.Net - handling event from Dropdown inside a Repeater

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now