I have managed to correct the example so that it compiles and it does indeed demonstrate how to populate from one data source and set the selected value from another. The working code is below, but first here are some things that caught me out which may help other beginners:
- Eval is used when you only want to read the value, use bind if you need to read and update it.
- If you are setting the text property of an object via Bind or Eval, the outer and inner quotes need to be different styles one must be single quotes and one must be double quotes but it doesn’t seem to matter which.
- If you are copying and pasting the example you will need to change the Inherits directive in the first line.
Here is the working code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WebApplication6._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource id="SqlDataSource1" runat="server" selectcommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>" updatecommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID">
</asp:sqldatasource>
<asp:FormView DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" ID="FormView1" runat="server">
<EditItemTemplate>
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text="<%# Bind('CompanyName') %>"></asp:Label><br />
CompanyName:
<asp:TextBox ID="CompanyNameTextBox" runat="server" Text="<%# Bind('CompanyName') %>"></asp:TextBox><br />
ContactName:
<asp:TextBox ID="ContactNameTextBox" runat="server" Text="<%# Bind('ContactName') %>"></asp:TextBox><br />
ContactTitle:
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="ContactTitle" DataTextField="ContactTitle"
DataSourceID="SqlDataSource2" SelectedValue="<%# Bind('ContactTitle') %>"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"></asp:SqlDataSource><br />
<asp:LinkButton ID="UpdateButton" runat="server" Text="Update" CommandName="Update" CausesValidation="True"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label><br />
CompanyName:
<asp:Label ID="CompanyNameLabel" runat="server" Text="<%# Bind('CompanyName') %>"></asp:Label><br />
ContactName:
<asp:Label ID="ContactNameLabel" runat="server" Text="<%# Bind('ContactName') %>"></asp:Label><br />
ContactTitle:
<asp:Label ID="ContactTitleLabel" runat="server" Text="<%# Bind('ContactTitle') %>"></asp:Label><br />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit"></asp:Button>
<div>
</div>
</ItemTemplate>
</asp:FormView>
</div>
</form>
</body>
</html>
"s to ' marks (and so on)