FormView 绑定中的 DropDownList

发布于 2024-07-21 23:40:32 字数 464 浏览 10 评论 0原文

我想将 dropdownlist 绑定到 List, 在后面的代码中。

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

不使用 ObjectDataSource !

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

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

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

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

Without using 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!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界和平 2024-07-28 23:40:33

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

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

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

希望这对您有帮助。

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.

楠木可依 2024-07-28 23:40:33

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

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

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

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

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

具体来说:

<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”元素。

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

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

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

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

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.

怀中猫帐中妖 2024-07-28 23:40:32

制作了一个在 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>

这是背后的代码:

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

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.

<%@ 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"));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文