Marc
Marc

Reputation: 16512

asp.net dropdown always no selected value on server side

I have an asp.net dropdown like this

 <asp:DropDownList width="95%" ID="RessourceComposantes" runat="server"    
       DataSourceID="Composantes" 
       DataTextField="Description" DataValueField="ComposanteID">
 </asp:DropDownList>

<asp:SqlDataSource ID="RessourceComposantes" runat="server" 
        ConnectionString="<%$ ConnectionStrings:OraEntities %>" 
        SelectCommand="SELECT [Blabla], [blablabla] FROM [blablablaa] 
        ORDER BY [blablablabla]">
</asp:SqlDataSource>

Is it normal that when I do dd_ressource_composante.selectedvalue on the server side I have no value. It's always "".

the source looks like this :

     <select name="ctl00$Tab$dd_ressource_composante" id="ctl00_Tab_dd_ressource_composante" style="width:95%;">
        <option value="1">Composante</option>
        <option value="3">DGAG</option>
        <option value="2">DSF</option>
        <option value="5">Test</option>
        <option value="6">Tous</option>

        <option value="4">VMD</option>

    </select>

I just tried to fill my dropdown in vb.net and I have the same result. The dropdown is full but when I do a postback I have no selected value

Actualy on the server side when I put a breakpoint on the dropdown, the item count is 0. I don't understand why... It's like the 8th dropdown list in this project and everything is the same but this one doesn't work.

I don't do any binding on the page load. it's all in the aspx file

well it works ONLY when I add autopostback="true" SelectedIndexChanged="dd_ressource_composante_SelectedIndexChanged" for the dropdown.

And theres no code in dd_ressource_composante_SelectedIndexChanged

It's vb.net for the server side

Thank you

Upvotes: 1

Views: 3151

Answers (5)

Marc
Marc

Reputation: 16512

Well that's weird. That control was in a table and I added a row like this

tblRessourcesProjet.Rows.Insert(1, tr)

and I changed it for

tblRessourcesProjet.Rows.Add(tr)

I don't understand quite well why it was a problem but it works now. Thank you everyone!

Upvotes: 1

Mubarek
Mubarek

Reputation: 2689

If you are calling DataBind() method somewhere in your page_load event please comment that otherwise wrap it in

 IF NOT IsPostBack Then
   DataBind()
 EndIF

This problem is most likely caused by rebinding the control before the event you are using is fired. If you are getting the selectedItem.Text the SelectedValue should also be there.

Upvotes: 1

mclark1129
mclark1129

Reputation: 7592

Where are you calling your dd_ressource_composantes.DataBind() method in the code-behind? Is it in the Page_Load event? If so, have you wrapped that statement in an IsPostback check so that you're not binding it everytime the page loads? Not doing so would reset your DataSource, and any selected value, every time the page posts back.

If (Not Page.IsPostBack) Then
    dd_ressource_composantes.DataBind()
End If

NOT

' No IsPostBack check
dd_ressource_composantes.DataBind()

Upvotes: 2

Mike Hofer
Mike Hofer

Reputation: 17022

When setting SelectedValue, the value must match the value of the Value attribute. If that's not the case, the results are, for all intents and purposes, undefined.

Upvotes: 1

Anshul
Anshul

Reputation: 1382

Also, doesn't your DataSourceID have to be "RessourceComposantes" and not "Composantes" since that's the ID of your datasource?

Upvotes: 1

Related Questions