12

List<MyIem>コードビハインドでドロップダウンリストを にバインドしたい。

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

ObjectDataSource を使用せずに!

ドロップダウンリストにバインドするにはどうすればよいですか? どんなイベントで?

また、SelectedValue='<%# Bind("ParentId") %>'動作するはずです!(つまり、ドロップダウンリストのバインディングはこの前に発生する必要があります!)

4

3 に答える 3

5

DataBoundイベントのドロップダウンを設定する例を作成しました。
マークアップは次の
とおりです。ddlを使用する方法は、DataBoundイベント中にfindcontrol()を使用してddlを検索することです。
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"));
        }
    }
}
于 2009-05-24T16:19:25.347 に答える
4

有効な値がデータベースにあると仮定すると、DropDownList に別の DataSource を設定できます。このビデオをチェックしてください:

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

ObjectDataSource の代わりに EntityDataSource を使用していますが、原則は引き続き機能するはずです。

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」要素に注意してください。

于 2010-08-23T09:15:18.573 に答える
1

私は同様の問題に直面していました。あなたがそれを追加しようとしていることに気づきましたが、それが表示されなかった主な原因である可能性があります。

ただし、上記の両方のソリューションに取り組んでおり、これがうまくいくことがわかりました:-

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

これがお役に立てば幸いです。

于 2013-11-22T05:33:21.467 に答える