0
 <asp:TemplateField HeaderText="Upgrade" SortExpression="Upgrade">
            <ItemTemplate>
                <asp:Label ID="LabelUpgrade" runat="server" Text='<%# Eval("Upgrade") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlUpgrade" runat="server" Width="100px">
                    <asp:ListItem Value="1">--Select--</asp:ListItem>
                    <asp:ListItem Value="2">1</asp:ListItem>
                    <asp:ListItem Value="3">2</asp:ListItem>
                    <asp:ListItem Value="4">3</asp:ListItem>
                    <asp:ListItem Value="5">4</asp:ListItem>
                    <asp:ListItem Value="6">5</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>

When I click edit on the GridView I need to take the value of the LabelUpgrade and assign it to the selected item in the DropDownList

Tsukasa
  • 6,342
  • 16
  • 64
  • 96

1 Answers1

1

Try:

<asp:DropDownList ... SelectedValue='<%# Bind("Upgrade") %>'>
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • 2
    In the aspx page. The DropDownList doesn't have a SelectedValue property. You can set the Selected property in the ListItem. – Mike Schwartz Nov 22 '13 at 17:22
  • @MikeSchwartz: Yes it does. It's inherited from [the `ListControl` class](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue%28v=vs.110%29.aspx). – Richard Deeming Nov 22 '13 at 17:24
  • 1
    I don't have an option for SelectedValue – Tsukasa Nov 22 '13 at 17:53
  • @Tsukasa: It's definitely there. If it doesn't appear in the properties list, just type it into the markup. – Richard Deeming Nov 22 '13 at 17:56
  • 1
    @RichardDeeming ***Could you please test it? It throws exception*** - `The 'SelectedValue' property cannot be set declaratively.` – Win Nov 22 '13 at 17:57
  • @RichardDeeming I take it back. I was testing the `DropDownList` without GridView. Besides `SelectedValue` is set as `[Browsable(false)]` which is why we do not get intellisense. – Win Nov 22 '13 at 18:20
  • Ok it did accept that as valid but I am getting has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value. The data in the database is indeed a value in the item list? – Tsukasa Nov 22 '13 at 18:35
  • @Tsukasa: What's the value in the database? – Richard Deeming Nov 22 '13 at 18:37
  • I have tried all of the values from the list and each one of them throws the error. – Tsukasa Nov 22 '13 at 19:01
  • 1
    @Tsukasa - This seems best answer to me... it is working in my test also. I am removing my answer. – afzalulh Nov 22 '13 at 19:04
  • @Tsukasa: Are you using the same values that you posted in your question? If not, bear in mind that the `SelectedValue` is case-sensitive; the value has to match *precisely* with the `Value` set on the `ListItem`. – Richard Deeming Nov 22 '13 at 19:09
  • 1
    Sorry I didn't notice a type, the update work the first time but every other time it failed because I was in fact pointing to the wrong control so the value didn't exist. Thanks for all of your help. – Tsukasa Nov 22 '13 at 19:43