Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList issue 1

Status
Not open for further replies.

bobetko

Programmer
Jan 14, 2003
155
US
I have detailsView control which is in Edit mode by default.
Control is bound to SQL table. In the table there is field thumbsize which can only have following three values: 1, 2 or 3

I want to have dropdown which will show the current value of thumbsize field, and I want to be able to change value selecting different value in the dropdown list.

Notice that values in the dropdown are predefined to 1,2 and 3.

thanks.



Code:
<asp:DetailsView ID="DVOptions" runat="server"
            AutoGenerateRows="False" DataKeyNames="pkID"
            DataSourceID="SqlDSOptions" Height="50px"
            DefaultMode="Edit">
  <Fields>
    <asp:BoundField DataField="pkID" 
        HeaderText="pkID" SortExpression="pkID" />
    <asp:BoundField DataField="userID" 
        HeaderText="userID" SortExpression="userID" />
    <asp:BoundField DataField="thumbs" 
        HeaderText="thumbs" SortExpression="thumbs" />
    <asp:BoundField DataField="thumbsize"
        HeaderText="thumbsize" SortExpression="thumbsize" />
    <asp:TemplateField HeaderText="thumb size">
     <EditItemTemplate>
       <asp:DropDownList ID="DropDownList1" runat="server"
          DataValueField='<%# Bind("thumbsize") %>'>
          <asp:ListItem Value="1">1</asp:ListItem>
          <asp:ListItem Value="2">2</asp:ListItem>
          <asp:ListItem Value="3">3</asp:ListItem>
       </asp:DropDownList>
      </EditItemTemplate>
     </asp:TemplateField>
    </Fields>
</asp:DetailsView>
 
Use the SelectedIndex property of the DropDown to set the value for you.
 
You can use the text_changed event for the textbox to update the value of the dropdown list and vice versa.
 
I want to have dropdown which will show the current value of thumbsize field
Use the RowDataBound event of the GridView
Code:
If e.Row.RowType = DataControlRowType.DataRow Then
   Dim ddl As New DropDownList
   ddl = e.Row.FindControl("DropDownList1")
   ddl.Items.FindByValue(e.Row.Cells(3).Text).Selected = True
End If
Jim
 
jbenson,

Thanks for that, I never used DropDownList.Items.FindByValue. That will make things much easier! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top