FrankTan
FrankTan

Reputation: 1686

asp DropDownList always select 0 index with postback check

i have aspx and aspx.cs files when i try to get the selected item of the

DropDownList

it always get the item at 0 index

<div style="text-align:center">
             <b>Scegliere il nome del report *  </b>  &nbsp;
             <asp:DropDownList id="dropdownlist1" style="width:250px;" runat="server"></asp:DropDownList>
            <br />
            <br /> 

            <br /> 
            <b>Scegliere la data del report </b>  &nbsp; 
            <asp:TextBox runat="server" ID="txtSchedDate" name="txtSchedDate" type="text" cssclass="datebox" style="height:20px;" ReadOnly="true"/>
            <br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Pubblica" OnClientClick="" OnClick="Button1_Click" />
            <br />
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" Font-Names="Arial" Font-Size="15px" runat="server" ForeColor="Red" Font-Bold="True" Text=""></asp:Label>
            <div id="divUpload" style="display:none">
            <div  style="width:200pt;;text-align:center;">Pubblicando...</div>
            </div>
        </div>

the c# code

  protected double size = 1;
  private string connectionString;
  private OracleConnection connection;
  private OracleCommand processNumQuery;
  private int indexdropitem;

  protected void Page_Load(object sender, EventArgs e)
  {


    if (Request["CONNECTSTRING"] == null && Session["CONNECTSTRING"] == null)
    {
        Response.Redirect("sessionup.asp?type=Pubreport");
    }
    else
    {
        if (Request["CONNECTSTRING"] != null)
        {
            connectionString = Request["CONNECTSTRING"].ToString();
        }
        else
        {
            connectionString = Session["CONNECTSTRING"].ToString();
        }


        if (connectionString.IndexOf("DSN=") >= 0)
        {
            Utility util = new Utility();
            connectionString = util.ConnStr(connectionString);
        }
        Session["CONNECTSTRING"] = connectionString;
        connection = new OracleConnection(connectionString);
        connection.Open();

    }
    if (!IsPostBack)
    {
            processNumQuery = new OracleCommand("select distinct nome_report from rpg_notification",connection);
            OracleDataReader reader = processNumQuery.ExecuteReader();

            while (reader.Read())
            {
                dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), ""));
            }
            reader.Close();
    }
    }


     protected void Button1_Click(object sender, EventArgs e)
     {

    Response.Write("try :" + dropdownlist1.SelectedIndex + " - " + txtSchedDate.Text + " - " + dropdownlist1.Items[dropdownlist1.SelectedIndex].Text + " - " + Request["txtSchedDate"] + " - ");
     }

i awalys get the 0 index please what i have to do??

Upvotes: 1

Views: 1684

Answers (2)

Shadow Wizard
Shadow Wizard

Reputation: 66389

ASP.NET is not "smart" enough to pass the actual selected index of submitted drop down list. Instead, it depends on the browser sending the selected value, then depending on the items having different value.

If there are items with the same value and one of them is selected, the server side SelectedIndex will return the index of the first item having that value. I have just created a quick test and was proven right. (not familiar with any .NET fiddle, sorry)

In your specific case, all items had empty value due to that line:

dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), ""));

To "fix" your problem just add a value:

string myValue = reader.GetString(0);
dropdownlist1.Items.Insert(0, new ListItem(myValue, myValue));

Upvotes: 1

RASMiranda
RASMiranda

Reputation: 381

that should work...just for debuging proposes, try to call dropdownlist1.SelectedItem.Text instead of dropdownlist1.Items[dropdownlist1.SelectedIndex].Text and see what you get

Upvotes: 0

Related Questions