user399356
user399356

Reputation:

Dropdownlist problem

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..

Upvotes: 1

Views: 157

Answers (4)

sam
sam

Reputation: 685

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.

Upvotes: 1

naval
naval

Reputation: 1413

i Think That when you click the button then page load performed and you calearing the all the data from ddl at the click or after performing son action so when click event performed and you had put the bindcontrol() under the ISPOSTBACK requirements ddl are going to bind the first time when page is load so change the code according the requirements.

Upvotes: 0

cjk
cjk

Reputation: 46485

You have to bind the grid both on a fresh page load and on a postback.

Upvotes: 0

Robert MacLean
Robert MacLean

Reputation: 39291

Remove the IsPostBack check - when you click the button the code is not refilling the drop down so it is empty and the selected value no longer exists.

Upvotes: 0

Related Questions