简体   繁体   English

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

[英]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. 我正在寻找一个示例,该示例如何从一个SQL数据源填充一个下拉列表,并从另一个SQL数据源设置所选字段,这基本上是在这里问的完全相同的东西http://forums.asp.net/p/793752 /793955.aspx后面有一个答复( http://forums.asp.net/post/793978.aspx ),该答复应该起作用,但是当我复制并粘贴代码时,这给了我很多错误。 I am looking for an ASP.NET 2 example that is coded in C#. 我正在寻找用C#编码的ASP.NET 2示例。

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. 最终,我想将其用作更为复杂的表单的一部分,在该表单中,中继器控件内部会动态创建未定义数量的类似下拉列表。

The problem is that the example had a few typos due to conversion to HTML. 问题在于该示例由于转换为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>

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. 当您只想读取值时使用Eval,如果需要读取和更新它,请使用bind。
  • 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. 如果通过Bind或Eval设置对象的text属性,则外部引号和内部引号必须采用不同的样式,其中一种必须是单引号,而另一种必须是双引号,但这似乎并不重要。
  • If you are copying and pasting the example you will need to change the Inherits directive in the first line. 如果要复制和粘贴示例,则需要在第一行中更改Inherits指令。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM