Last post Nov 22, 2007 10:27 AM by finely_tuned
Mar 26, 2007 07:26 AM|spnz|LINK
I am using a detailsview to add data into my database with the add of using a business object.
I want to change the detailsview around a little as one of the fields I want to add information is related to another table. Currently I have a textbox where I have to manually add the fields ID number instead of been able to select the value from a dropdown.
Can anyone assist with my problem?
Thanks for your time!
Mar 26, 2007 07:37 AM|Fredrik N|LINK
Select your DetailsView in Design mode. Use the DetailsView task menu. Select Edit Fields. Select the field that should instead be displayed as a DropDownList. Select the "Convert thios field into a TemplateField". When you do so, it will make sure that
you use ta TemplateField, with a tempalte, you can specify what control or controls that should be displayed for the current field. You can in this case add a DropDownList and for example bind it to a data source control that will get the items that should
be added to the list. By using the SelectedValue of the DropDownList, you can bind it to your data source. Make sure you use the Bind expression:
<asp:DropDownList SelectedValue='<%# Bind("your field name")%>' ....>
You also need to make sure that the value you bind to the DropDownList, is added to the DropDownList.
EDIT: Here is an example:
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:TemplateField HeaderText="ContactTitle" SortExpression="ContactTitle">
<asp:DropDownList SelectedValue='<%# Bind("ContactTitle") %>' ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" .../></EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ContactTitle") %>'></asp:Label>
Mar 26, 2007 09:06 AM|spnz|LINK
Thanks Fredrik thats solved my problem.
Quick question how do I change the 1st dropdown value to show a different value e.g "Select Room" that isn't bond to a record within the database?
Mar 26, 2007 09:09 AM|Fredrik N|LINK
Mar 26, 2007 09:11 AM|EricJohansson|LINK
Try catching the DataBound event and add it to the dropdowns items collection.
Mar 26, 2007 09:36 AM|spnz|LINK
Cool Thanks for your help that's now working perfectly!!
Nov 22, 2007 10:27 AM|finely_tuned|LINK
Thanks Fredrik, good example, with your help I have improved the quality of data going into my database in a few easy steps