开发者

Dropdownlist problem

I don't understand why I can't get the ddl selected value..

ASP CODE:

<table>
    <tr>
      开发者_JS百科<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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜