Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Problem
I have a problem using a DropDownList control inside a ListView - when the selected index of the DropDownList is changed, the OnItemCommand event of the ListViewItem isn't being fired.

I'm not sure whether I'm configuring the controls correctly, so any advice would be appreciated.

More detail
I have a DropDownList inside of an ItemTemplate for a ListView, defined in my aspx.

When the user selects a different index in the DropDownList, I want to perform a postback and capture the newly chosen DropDownList index, as well as the ListViewItem that contains said DropDownList.

To achieve this, I am setting the AutoPostBack property of the DropDownList to "true" and am trying to capture the ListViewItem in the OnItemCommand command of the ListView.

Debugging reveals that the postback is occuring, but the OnItemCommand event isn't being triggered.

It might also be worth noting that I am NOT adding the DropDownList to the ListView controls in the ItemCreated event on the server, as this causes the DropDownList to render outside of the ListViewItem and doesn't improve the problem. I can only assume I am doing this the wrong way, or there is something I'm overlooking.

Please can anyone advise?

Thanks
Posted
Updated 24-May-19 0:29am
v2

Catch it on ListView ItemDataBound

Plesae dont copy and paste, try to understand.

example.aspx

XML
<asp:listview id="ListView1" runat="server" onitemdatabound="ListView1_ItemDataBound" xmlns:asp="#unknown">
        DataSourceID="SqlDataSource1" ItemPlaceholderID="SqlItemContainer" DataKeyNames="cid"&gt;
.......................
 <itemtemplate>
            <asp:dropdownlist id="ddl" runat="server" autopostback="true" onselectedindexchanged="ddl_SelectedIndexChanged">
                <asp:listitem text="Select 1" value="One" selected="True">Review</asp:listitem>
                <asp:listitem text="Select 2" value="Two">Send Back to Level1</asp:listitem>
            </asp:dropdownlist>
        </itemtemplate>
<asp:label id="ddlval" text="" runat="server"></asp:label&gt;


...........................

example.cs

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlval.Text = ((DropDownList)sender).SelectedValue;
    }
    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                DropDownList ddl = e.Item.FindControl("ddl") as DropDownList;
                ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);

            }
        }
 
Share this answer
 
v3
Comments
anthasaurus 9-Feb-11 5:15am    
Thanks Monjurul and Abhishek,

I have tested your suggestion, but I don't seem to be any better off. It makes me wonder whether it is because I am not adding the DropDownList control to the collection of controls on the ListViewItem properly? I.e.

ListViewItem lvi = e.Item;
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
lvi.Controls.Add(ddl);

I have tried the above code I added in the ItemCreated event of the ListView and also the ItemDataBound event and neither method fixes the problem of the ItemCommand event not firing for the ListView.

I wonder if anything more sinister is going on here?!
You should rather capture SelectedIndexChanging [^]or SelectedIndexChanged [^]event.
 
Share this answer
 
Comments
anthasaurus 8-Feb-11 11:36am    
Thanks for your reply Manas,

I'm not convinced that would be the way to do it - how would one go about obtaining the DataItem for the ListViewItem that the DropDownList belongs to?

The SelectedIndexChanging/Changed server event only passes 'EventArgs' as a parameter back to the server, but the DataItem of the ListView cannot be obtained from this.
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList ddlListFind = (DropDownList)sender;
       ListViewItem item1 = (ListViewItem)ddlListFind.NamingContainer;
       DropDownList getDDLList = (DropDownList)item1.FindControl("dropdownlist1");

       Label lblMessage = (Label)item1.FindControl("lblMsg");
       lblMessage.Visible = true; lblMessage.Text = "dropDown text is : " + getDDLList.SelectedItem.Text + " and value is : " + getDDLList.SelectedItem.Value;
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900