C# FormView 绑定中的 DropDownList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/903869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:26:45  来源:igfitidea点击:

DropDownList in FormView binding

c#asp.net.netdrop-down-menuformview

提问by markiz

I want to bind dropdownlist to List<MyIem>, in code behind.

我想将下拉列表绑定到List<MyIem>, 在后面的代码中。

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

Without using ObjectDataSource !

不使用 ObjectDataSource !

How can I Bind it to the dropdown list? In what event?

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

Also SelectedValue='<%# Bind("ParentId") %>'should work! (I mean the dropdownlist binding should occur before this!)

SelectedValue='<%# Bind("ParentId") %>'应该工作!(我的意思是下拉列表绑定应该在此之前发生!)

回答by Kb.

Made an example which will set the dropdown in the DataBound event.
Here is the markup
The way to use the ddl, is to find it with findcontrol() during DataBound event.
When you have the control in the DataBound event, you can also bind the dropdown to your List<>
Hope this helps.

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

<%@ 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>

Here is the code behind:

这是后面的代码:

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"));
        }
    }
}

回答by Pixel

You can populate the DropDownList with another DataSource, assuming the valid values are in the database. Check out this video:

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

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

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

It's using an EntityDataSource instead of an ObjectDataSource, but the principle should still work.

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

If you want a "(none)" type option for null, see section "Converting Null in Template Fields" on this page:

如果您想要一个“(无)”类型的空值选项,请参阅本页上的“在模板字段中转换空值”部分:

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

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

Specifically:

具体来说:

<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>

Notice the the "AppendDataBoundItems" attribute and the "asp:ListItem" element.

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

回答by haxpak

well i was facing a similar problem. i noticed that you were trying to add it to and not which can be a major cause that you did not see it.

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

i have however worked on both the solutions provided above and found this worked for me :-

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

<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>

hope this helpes you.

希望这对你有帮助。