Solved

ASP.Net - handling event from Dropdown inside a Repeater

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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