FormView绑定中的DropDownList

我想在下面的代码List dropdownlist绑定到List

  <asp:DropDownList ID="listCategories" runat="server" Height="20px" CssClass="CategoryDropList" SelectedValue='' AutoPostBack="false" Width="300px"> 

不使用ObjectDataSource!

如何将其绑定到下拉列表? 在什么情况下?

SelectedValue=''应该有效! (我的意思是dropdownlist绑定应该在此之前发生!)

举个例子来设置DataBound事件的下拉列表。
这是标记
使用ddl的方法是在DataBound事件期间使用findcontrol()找到它。
当您在DataBound事件中拥有控件时,您还可以将下拉列表绑定到List <>
希望这可以帮助。

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>    Untitled Page   
One Two Three

这是后面的代码:

 namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { List list = new List(); list.Add("Some"); list.Add("Other"); FormView1.DataSource = list; //just to get the formview going FormView1.DataBind(); } protected void FormView1_DataBound(object sender, EventArgs e) { DropDownList ddl = null; if(FormView1.Row != null) ddl = (DropDownList) FormView1.Row.FindControl("DropDownList1"); ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Two")); } } } 

假设有效值在数据库中,您可以使用另一个DataSource填充DropDownList。 看看这个video:

http://msdn.microsoft.com/en-us/data/cc546554.aspx

它使用EntityDataSource而不是ObjectDataSource,但原则应该仍然有效。

如果您想要null的“(none)”类型选项,请参阅此页面上的“在模板字段中转换空”部分:

http://msdn.microsoft.com/en-us/library/ms366709.aspx

特别:

  (none)  

注意“AppendDataBoundItems”属性和“asp:ListItem”元素。

好吧,我面临着类似的问题。 我注意到你试图将它添加到,而不是哪个可能是你没有看到它的主要原因。

然而,我已经研究了上面提供的两种解决方案,发现这对我有用: –

   Common Mechanical Electronics      

希望这有助于你。