DropDownList in a ListView Edit Problem (ASP/C#)

Soldato
Joined
13 Dec 2002
Posts
7,646
Location
Manchester City Centre
I have a simple admin panel for a table in a db which has a gridview of the indexes from the table, when these are selected it brings up a listview showing the full details for that specific record. These are editable.

I want to restrict the input of edited values quite heavily, and would like to do this with drop down lists for about half of the fields. This is where i've struck a bit of difficulty, having dug around various bits pulled from google I have the standard textbox bound to the field but with visible set to false. Following other examples I then want to set the appropriate value of the dropdownlist on the ItemDataBound Event

i've tried to do this with the following code, but it doesnt seem to be retrieving the dropdownlist, ddl just comes back as null

Code:
if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                DropDownList ddl = (DropDownList)e.Item.FindControl("CampusDDL");
                if (ddl != null)
                {
                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(((TextBox)e.Item.FindControl("CampusText")).Text));
                }
            }

any ideas would be much appreciated, or a better way of acheiving the same result, I've been chipping away at this for a while but not being able to find the dropdownlist keeps seeming to block me
 
ok, slight developments my first problem was because I had hidden the dropdownlist within an ajax tabpanel which in turn was within a tabcontainer, so the code now looks like:
Code:
protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                if (e.Item.HasControls())
                {
                    AjaxControlToolkit.TabContainer tc = (AjaxControlToolkit.TabContainer)e.Item.FindControl("tabsModManager");
                    if (tc != null)
                    {
                        AjaxControlToolkit.TabPanel tp = (AjaxControlToolkit.TabPanel)tc.FindControl("tabpnlApproveNew");
                        DropDownList ddl = (DropDownList)tp.FindControl("CampusDDL");
                        if (ddl != null)
                        {
                            ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(((TextBox)e.Item.FindControl("CampusText")).Text));
                        }
                    }
                }
            }
        }

So it now finds the dropdownlist which is good, but although it sees it exists it sees it as being empty, even though the items are hardcoded into the dropdownlist

Code:
<asp:DropDownList ID="CampusDDL" runat="server">
<asp:ListItem Text="City" Value="1" />
<asp:ListItem Text="Fallowfield" Value="2" />
<asp:ListItem Text="Victoria Park" Value="3" />
</asp:DropDownList>
 
finally got it to display the correct value as the selected item in the drop down
Code:
protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                if (e.Item.HasControls())
                {
                    AjaxControlToolkit.TabContainer tc = (AjaxControlToolkit.TabContainer)e.Item.FindControl("tabsModManager");
                    if (tc != null)
                    {
                        AjaxControlToolkit.TabPanel tp = (AjaxControlToolkit.TabPanel)tc.FindControl("tabpnlApproveNew");
                        DropDownList ddl = (DropDownList)tp.FindControl("CampusDDL");
                        if (ddl != null)
                        {
                            TextBox tb = (TextBox)tp.FindControl("CampusText");
                            ddl.SelectedValue = tb.Text;
                        }
                    }
                }
            }
        }

now just got to get it working in reverse to set the value to be sent back :s
 
Back
Top Bottom