C# FormView绑定中的DropDownList

C# FormView绑定中的DropDownList,c#,asp.net,.net,drop-down-menu,formview,C#,Asp.net,.net,Drop Down Menu,Formview,我想将dropdownlist绑定到列表, 在代码隐藏中 <asp:DropDownList ID="listCategories" runat="server" Height="20px" CssClass="CategoryDropList" SelectedValue='<%# Bind("ParentId") %>' AutoPostBack="false" Width="300px"> 不使用ObjectDataSource 如何将其绑

我想将dropdownlist绑定到
列表
, 在代码隐藏中

 <asp:DropDownList ID="listCategories"  runat="server" Height="20px"   CssClass="CategoryDropList" SelectedValue='<%# Bind("ParentId") %>' AutoPostBack="false" Width="300px">      

不使用ObjectDataSource

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


而且
SelectedValue=''
应该可以工作!(我的意思是dropdownlist绑定应该在此之前发生!)

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

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

        </div>
        <asp:FormView ID="FormView1" runat="server" ondatabound="FormView1_DataBound">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>One</asp:ListItem>
                    <asp:ListItem>Two</asp:ListItem>
                    <asp:ListItem>Three</asp:ListItem>
                </asp:DropDownList>

            </ItemTemplate>
        </asp:FormView>
        </form>
    </body>
    </html>

无标题页
一个
两个
三
下面是隐藏的代码:

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            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"));
        }
    }
}
命名空间WebApplication1
{
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
列表=新列表();
列表。添加(“部分”);
列表。添加(“其他”);
FormView1.DataSource=list;//只是为了让formview运行
FormView1.DataBind();
}
受保护的void FormView1_数据绑定(对象发送方、事件参数e)
{
dropdownlistddl=null;
if(FormView1.Row!=null)
ddl=(DropDownList)FormView1.Row.FindControl(“DropDownList1”);
ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.FindByValue(“两”));
}
}
}

假设数据库中存在有效值,则可以使用另一个数据源填充DropDownList。查看此视频:

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

如果要为null设置“(无)”类型选项,请参阅本页的“在模板字段中转换null”部分:

具体而言:

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
    DataTextField="Name" DataValueField="EmployeeID"
    SelectedValue='<%# Bind("ReportsTo") %>' AppendDataBoundItems="True">
        <asp:ListItem Selected="True" Value="">(none)</asp:ListItem>
</asp:DropDownList>

(无)

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

我也遇到了类似的问题。我注意到你试图将它添加到而不是,这可能是你没有看到它的一个主要原因

然而,我已经对上述两种解决方案进行了研究,发现这对我来说都是可行的:-

<EditItemTemplate>
<asp:DropDownList ID="ddlStream" runat="server" SelectedValue='<%# Bind("Stream") %>'>
                    <asp:ListItem>Common</asp:ListItem>
                    <asp:ListItem>Mechanical</asp:ListItem>
                    <asp:ListItem>Electronics</asp:ListItem>
                    </asp:DropDownList>
</EditItemTemplate>

<ItemTemplate>
<asp:Label runat="server" id="something" text='<%# Eval("Stream")%>'/>
</ItemTemplate>

普通的
机械的
数码产品

希望这对您有所帮助。

但我还需要在代码隐藏中填充DropDownList,而不是像一个人不知道这是否重要,但DropDownList在FormView的EditTemplate中。您可以在DataBound事件中填充DropDownList。获得ddl引用后:将数据获取为列表,将ddl.DataSource设置为列表,然后执行ddl.DataBind()。若您在EditTemplate中,请确保在编辑项模板中具有唯一的ddl ID。希望您能让它工作。我无法在DataBound事件中填充它,因为SelectedValue=''工作将很晚。我明白您的意思。一个建议是可以在DataBound中填充,并在DataBound事件中以代码隐藏而不是表单中设置SelectedValue。1。你能澄清一下你正在使用的ASP.NET版本吗?2.我得到了它的工作,但可以肯定的是,我需要看看你的代码,因为你的问题提供的细节太少了。第二次回答要好得多。。。请把它标为答案