views:

280

answers:

1

I populate DropDownList in ASP.NET webforms:

<asp:DropDownList runat="server" ID="salesman"></asp:DropDownList>


users= Buslayer.GetSalesRep();

foreach (userentity user in users)
{         
      salesman.Items.Add(new ListItem(user.FirstName + " " + user.LastName,
                                      user.UserID.ToString())); 
}

After submission, I am still getting selected index = 0,

I tried all of this but failed:

Response.Write("" + salesman.SelectedValue);
Response.Write("" + salesman.SelectedItem.Value);
Response.Write("" + salesman.SelectedIndex);
A: 

Are you checking for the Page posting back with you databind?

Your page load should look something like:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindData();
    }

}

protected void BindData()
 {
    // Get your data
    ddl.DataSource = yourData;
    ddl.DataBind();

}
TheGeekYouNeed
i think it is not related to that:if(Page.IsPostBack)Response.Write("" + salesman.SelectedIndex);if (!Page.IsPostBack)Response.Write("" + salesman.SelectedValue);both doesnt work
user1111111