• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

get selected item from drop downlist

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.
0
fwstealer
Asked:
fwstealer
1 Solution
 
Obadiah ChristopherCommented:
Are you checking the

if(!isPostBack) property before binding the dropdownlist?
0
 
fwstealerAuthor 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now