We help IT Professionals succeed at work.

get selected item from drop downlist

fwstealer
fwstealer asked
on
i'm not sure why i can't get the selected item from my dropdownlist.

<asp:Label ID="lblWorkSheet" runat="server" Text="Select WorkSheet"></asp:Label>
<asp:DropDownList ID="ddlWorkSheets" runat="server" 
                onselectedindexchanged="ddlWorkSheets_SelectedIndexChanged">
            </asp:DropDownList>


protected void getFile()
        {
            string importPL = string.Empty;

            if (FileUpload1.HasFile)
            {
                string fileName = Server.HtmlEncode(FileUpload1.FileName); //name.xls
                string extension = System.IO.Path.GetExtension(fileName); //.xls xlsx
                string tableName = "PL";

                if ((extension == ".xls" || extension == ".xlsx"))
                {
                    FileInfo fileInfo = new FileInfo(FileUpload1.PostedFile.FileName);
                    SpreadSheet = FileUpload1.PostedFile.FileName;   //fileInfo.Name.ToString();
                    lblSpreadSheet.Text = "SpreadSheet: " + SpreadSheet.ToString(); //fileInfo.Name.ToString();

                    if (SpreadSheet.Trim() != string.Empty)
                    {
try
 {
 ArrayList strTables = GetTableExcel(SpreadSheet);
                            ddlWorkSheets.DataSource = strTables;
                            ddlWorkSheets.DataBind();

                            ListItem itm = new ListItem();
                            itm.Text = "";
                            itm.Value = "-1";
                            itm.Selected = true;
                            ddlWorkSheets.Items.Insert(0, itm);
                            ddlWorkSheets.SelectedIndex = 0;
 }
catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message.ToString();
                        }

                    }
                }
            }

        }


protected void ddlWorkSheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlWorkSheets.SelectedValue == "-1")
            {
                ddlWorkSheets.Focus();
            }
            else
            {
                lblWorkSheet.Text = "Worksheet: " + ddlWorkSheets.SelectedItem.ToString();
            }
        }

Open in new window


i just don't get it.
Comment
Watch Question

Developer User Interface
CERTIFIED EXPERT
Commented:
Are you checking the

if(!isPostBack) property before binding the dropdownlist?

Author

Commented:
duh - set autopostback to true


<asp:DropDownList ID="ddlWorkSheets" runat="server" AutoPostBack="True" onselectedindexchanged="ddlWorkSheets_SelectedIndexChanged">
            </asp:DropDownList>

protected void ddlWorkSheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlWorkSheets.SelectedValue == "-1")
            {
                ddlWorkSheets.Focus();
            }
            else
            {
                lblWorkSheet.Text = "Worksheet: " + ddlWorkSheets.SelectedItem.ToString();

            }
        }

Open in new window


nothing happens

Explore More ContentExplore courses, solutions, and other research materials related to this topic.