如何填充ASP.. NET下拉列表从一个数据源,并设置从另一个数据源的选定值
本文关键字:数据源 设置 另一个 填充 何填充 ASP NET 下拉列表 一个 | 更新日期: 2023-09-27 18:17:17
我正在寻找如何从一个SQL数据源填充下拉列表,并设置从另一个选定的字段的一个例子,它基本上是完全相同的事情,在这里问http://forums.asp.net/p/793752/793955.aspx有一个答复进一步向下(http://forums.asp.net/post/793978.aspx)应该工作,但当我复制和粘贴代码它给了我很多错误。我在找ASP。用c#编写的。NET 2示例。
答案似乎表明,不编写任何代码是可能的,这是正确的吗?
最终我想使用它作为一个更复杂的形式的一部分,其中一个未定义的数量类似的下拉菜单是在一个重复器控件内动态创建的。
问题是该示例由于转换为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> <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>
我已经设法纠正了这个例子,使它能够编译,并且它确实演示了如何从一个数据源填充并从另一个数据源设置所选值。下面是工作代码,但首先这里有一些我发现的东西,可能会帮助其他初学者:
- Eval是在你只想读取值时使用的,如果你需要读取和更新它,使用bind。
- 如果你是通过Bind或Eval设置一个对象的文本属性,外部和内部的引号需要是不同的样式,一个必须是单引号,一个必须是双引号,但这似乎并不重要。
- 如果你复制粘贴了这个例子,你需要修改第一行的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>