DropDownList的在FormView控件绑定 [英] DropDownList in FormView binding

查看:151
本文介绍了DropDownList的在FormView控件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绑定的dropdownlist到名单,其中,MyIem> ,  在$ C $后面℃。

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

在不使用的ObjectDataSource!

Without using ObjectDataSource !

我怎样才能将其绑定到下拉列表?在什么情况下?

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

还有的SelectedValue ='&LT;%#绑定(的ParentId)%&GT; 应该努力! (我指的是DropDownList的约束力在此之前应该发生!)

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

推荐答案

做出了表率,将设置在DataBound事件下拉。
这里是标记
使用DDL的办法,就是找到它的FindControl()在数据绑定事件。
当你在DataBound事件的控制,你也可以绑定下拉到列表&LT;>
希望这有助于。

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>

下面是code背后:

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

这篇关于DropDownList的在FormView控件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆