如何从一个数据源填充ASP.NET DropDownList并从另一个数据源设置选定的值?

时间:2011-12-08 15:14:16

标签: c# asp.net drop-down-menu asp.net-2.0 sqldatasource

我正在寻找一个如何从一个SQL数据源填充下拉列表并从另一个SQL数据源设置所选字段的示例,它基本上与此处要求的完全相同http://forums.asp.net/p/793752/793955.aspx还有一个答复down(http://forums.asp.net/post/793978.aspx)应该可以工作,但是当我复制并粘贴代码时,它给了我很多错误。我正在寻找一个用C#编码的ASP.NET 2示例。

答案似乎表明,可以不编写任何代码,这是正确的吗?

最终我想将此作为更复杂形式的一部分,其中在转发器控件内动态创建未定义数量的类似下拉列表。

2 个答案:

答案 0 :(得分:1)

问题是该示例由于转换为HTML而存在一些拼写错误。以下是该页面应该正常工作的副本:

<%@  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>

答案 1 :(得分:0)

我已经设法纠正了这个示例,以便编译它确实演示了如何从一个数据源填充并从另一个数据源设置所选值。工作代码如下所示,但首先是一些让我感到高兴的东西可以帮助其他初学者:

  • 当您只想读取值时使用Eval,如果需要读取和更新它,请使用bind。
  • 如果您通过Bind或Eval设置对象的text属性,则外部引号和内部引号必须是不同的样式,必须是单引号,而且必须是双引号,但似乎并不重要。< / LI>
  • 如果您要复制并粘贴示例,则需要在第一行中更改Inherits指令。

以下是工作代码:

<%@  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>