Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Dropdownlist value always is the first value

We're using RadControls (Telerik). Similar to AJAX.
I have 4 items in the ddl. Whatever i choose and then click a button, the value is always the first item in the ddl... not sure what i'm missing,,this is what i have:

In ItemDataBound, I loop thru the items but it's always the first ddl value:

 foreach (GridDataItem dataItem in grdItems.MasterTableView.Items)  
            {

                DropDownList test = (dataItem.FindControl("ddlStatus") as DropDownList);
                string tt = test.SelectedValue; //**** always "WH" !!!

            }
<rad:GridTemplateColumn HeaderText="New Status">
                                                                 <ItemTemplate><asp:DropDownList ID="ddlStatus" runat="server">
                                                                                 <asp:ListItem Value="WH"  Text="Sent"></asp:ListItem>
                                                                                 <asp:ListItem Value="NA" Text="NA"></asp:ListItem>
                                                                                 <asp:ListItem Value="Canceled" Text="Canceled"></asp:ListItem>
                                                                                 <asp:ListItem Value="Hold" Text="Hold"></asp:ListItem>
                                                                               </asp:DropDownList>
                                                                 </ItemTemplate>
                                                                </rad:GridTemplateColumn>

Open in new window

Avatar of rpkhare
rpkhare
Flag of India image

This is because you are retrieving the SelectedValue. By default the first item is selected. You are just asking to show the selected item.

What you are actually trying to achieve?
Avatar of GreymanMSC
GreymanMSC

I suspect  you may wish to bind the control to the container's dataobject.
<ItemTemplate><asp:DropDownList ID="ddlStatus" runat="server"
        SelectedValue='<%# Bind("Status") %>'
    >
        <asp:ListItem Value="WH"  Text="Sent"></asp:ListItem>
        <asp:ListItem Value="NA" Text="NA"></asp:ListItem>
        <asp:ListItem Value="Canceled" Text="Canceled"></asp:ListItem>
        <asp:ListItem Value="Hold" Text="Hold"></asp:ListItem>
    </asp:DropDownList>
</ItemTemplate>

Open in new window

Avatar of Camillia

ASKER

I will try GreymanMSC's method when i get to work...

>> This is because you are retrieving the SelectedValue
If I have 4 items in the ddl, and user selects item2, I want that item2. Whatever the user selected from ddl... I think that's correct: selectedValue ...no?

No, this line is not correct. There's no "SelectedValue" for ddl's main tag:

<asp:DropDownList ID="ddlStatus" runat="server"
        SelectedValue='<%# Bind("Status") %>'   >
I tried this as well:

string tt = test.SelectedItem.Value.ToString();

But still defaults to first ddl item
ASKER CERTIFIED SOLUTION
Avatar of Camillia
Camillia
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Although it does not show up in the autofill options, SelectedValue is the correct attribute to which you bind.