I don’t understand why I can’t get the ddl selected value..
ASP CODE:
<table>
<tr>
<th> Annee:</th>
<td>
<asp:DropDownList ID="ddlAnnee" runat="server" />
</td>
</tr>
<tr>
<th colspan="2">
<asp:Button ID="btnValidate" runat="server" onclick="btnValidate_Click"
Text="Validate" />
</th>
</tr>
</table>
Code BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindControls();
}
}
public void BindControls()
{
this.ddlAnnee.DataSource = new BLL.ANNEE_MANAGER().List(false, true, null);
this.ddlAnnee.DataTextField = "INTITULE_AN";
this.ddlAnnee.DataValueField = "ID_AN";
this.ddlAnnee.DataBind();
}
protected void btnValidate_Click(object sender, EventArgs e)
{
int Id_an = int.Parse( this.ddlAnnee.SelectedValue);
}
}
}
So, when i put a break point at the page load and binding part, it’s ok, the ddl is filled correctly and the page show correctly the ddl. When I click on the button, I arrive in the btnValidate_Click method but the ddl is empy!
I suppose I’ve forgot something.. Please help me!
Thanks..
Do not remove IsPostBack, the BindControl method should be in !IsPostBack Condition otherwise you are never going to get selected value. Just make sure that any of the method not clearing the ddlAnnee dropdown.