nigelhooper
nigelhooper

Reputation: 39

How to populate an ASP.NET DropDownList from one datasource and set the selected value from another?

I am looking for an example of how to populate a dropdown list from one SQL data source and set the selected field from another, it is basically exactly the same thing that is asked here http://forums.asp.net/p/793752/793955.aspx there is a reply further down (http://forums.asp.net/post/793978.aspx) which is supposed to work but when I copy and paste the code it is giving me lots of errors. I am looking for an ASP.NET 2 example that is coded in C#.

The answer seems to suggest that it is possible to do without writing any code behind, is this correct?

Ultimately I want to use this as part of a much more complicated form where an undefined number of similar dropdowns are created dynamically inside a repeater control.

Upvotes: 0

Views: 3232

Answers (2)

nigelhooper
nigelhooper

Reputation: 39

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>

Upvotes: 0

competent_tech
competent_tech

Reputation: 44921

The problem is that the example had a few typos due to conversion to HTML. Here is a copy of that page that should work correctly:

<%@  page="" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!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:FormView DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" ID="FormView1"
            runat="server">
            <EditItemTemplate>
                CustomerID:
                <asp:Label ID="CustomerIDLabel1" runat="server" Text="<%# Eval("CustomerID") %>"
                    gt="" 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="SqlDataSource1" SelectedValue="<%# Bind("ContactTitle") %>">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" 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>&nbsp;<asp:LinkButton ID="UpdateCancelButton"
                            runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False"></asp:LinkButton>
                </asp:Label></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></ItemTemplate>
            <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] = @original_CustomerID">
            </asp:sqldatasource>
            <div>
            </div>
        </asp:FormView>
    </div>
    </form>
</body>
</html>

Upvotes: 1

Related Questions