在 ASP.net 表单上,我有一个按钮和一个下拉列表。Dropdownlist 通过 LinqDatasource 填充。
我试图在单击按钮时获取选定的下拉列表值。
下拉列表从 Northwind db 的员工表中获取姓氏列表。
后面的代码是这样的,它将 ddl 值设为 null
页面
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Demo._Default" %>
<form id="form1" runat="server">
<asp:DropDownList ID="Ddl_allGoldsets" runat="server"
DataSourceID="LinqDataSource1" DataTextField="LastName"
DataValueField="LastName">
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Demo.linqDataContext" EntityTypeName="" GroupBy="LastName"
OrderGroupsBy="key" Select="new (key as LastName, it as Employees)"
TableName="Employees">
</asp:LinqDataSource>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
代码隐藏
namespace Demo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
linqDataContext dc = new linqDataContext();
DropDownList ddl = (DropDownList)Page.FindControl("Ddl_allGoldsets");
string val = ddl.SelectedValue;
/* code that writes val into db*/
}
}
}
上面的代码返回空值到ddl!!