none
listview中可以绑定数据库中的dropdownlist,但是却不能进行SelectedValue选择 RRS feed

  • 问题

  • 绑定部门:
            protected void bind_Bm(DropDownList ddl_Ssbm)
            {
                SqlDataReader sdr = DBManager.getSdr("select [id],[department] from [department] order by px");
                ddl_Ssbm.DataSource = sdr;
                ddl_Ssbm.DataValueField = "id";
                ddl_Ssbm.DataTextField = "department";
                ddl_Ssbm.DataBind();
                sdr.Close();
                DBManager.closeCon();
            }
    

    ListView的ItemDataBound
           protected void lv_User_ItemDataBound(object sender, ListViewItemEventArgs e)
            {
                DropDownList ddl = (DropDownList)e.Item.FindControl("ddl_Bm");
                bind_Bm(ddl);
                ddl.SelectedValue = DataBinder.Eval(e.Item, ("id")).ToString();
            }
    前台:您只注意DropDownList即可
    <asp:ListView ID="lv_User" runat="server" DataKeyNames="id" OnPagePropertiesChanging="lv_User_PagePropertiesChanging"
            OnItemDataBound="lv_User_ItemDataBound">
            <LayoutTemplate>
                <table cellpadding="1" cellspacing="0" width="80%" style="font-family: 宋体; font-size: 14px;
                    border-color: Black; border-collapse: collapse;" id="table_Fscxgdzcsq" align="center">
                    <tr>
                        <td>
                            姓名
                        </td>
                        <td>
                            部门
                        </td>
                        <td>
                            用户名
                        </td>
                        <td>
                            密码
                        </td>
                        <td>
                            录入
                        </td>
                        <td>
                            删除
                        </td>
                        <td>
                            修改
                        </td>
                        <td>
                            查询
                        </td>
                    </tr>
                    <tr runat="server" id="itemPlaceholder">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <a href='/schy/view.aspx?id=<%#Eval("id") %>' target="_blank">
                            <%#Eval("xm") %></a>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddl_Bm" runat="server">
                        </asp:DropDownList>
                    </td>
                    <td>
                        <%#Eval("username") %>
                    </td>
                    <td>
                        <%#Eval("password") %>
                    </td>
                    <td>
                        <asp:CheckBox ID="cb_Zj" runat="server" Checked='<%#Bind("lr") %>' />
                    </td>
                    <td>
                        <asp:CheckBox ID="cb_Sc" runat="server" Checked='<%#Bind("sc") %>' />
                    </td>
                    <td>
                        <asp:CheckBox ID="cb_Xg" runat="server" Checked='<%#Bind("xg") %>' />
                    </td>
                    <td>
                        <asp:CheckBox ID="cb_Cx" runat="server" Checked='<%#Bind("cx") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
    现象:能够从数据库中读取部门信息  即:lv_User_ItemDataBound中
    DropDownList ddl = (DropDownList)e.Item.FindControl("ddl_Bm");
                bind_Bm(ddl);
    没有问题

    但是ddl.SelectedValue = DataBinder.Eval(e.Item, ("id")).ToString();却不能运行,无法把user表中的部门信息绑定到ddl_Bm上
    2009年5月1日 13:05

答案

  • 你好,我写了个标例你对比修改一下

    DataList_X20090502.aspx
    --------------------------------------------------------------------------------------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200905020016.aspx.cs" Inherits="DataList_X200905020016" %>
    <!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></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListView ID="ListView1" DataKeyNames="EmployeeKey" runat="server" 
                onitemdatabound="ListView1_ItemDataBound">
                <LayoutTemplate>
                    <table cellpadding="1" cellspacing="0" width="80%" style="font-family: 宋体; font-size: 14px;
                        border-color: Black; border-collapse: collapse;" id="table_Fscxgdzcsq" align="center">
                        <tr>
                            <td>员工 ID</td>
                            <td>姓名</td>
                            <td>员工部门 Id</td>
                            <td>员工部门</td>
                        </tr>
                        <tr runat="server" id="itemPlaceholder">
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td><asp:Label ID="fEmployeeKey" runat="server" Text='<%#Eval("EmployeeKey")%>'></asp:Label></td>
                        <td><asp:Label ID="fName" runat="server" Text='<%#Eval("Name")%>'></asp:Label></td>
                        <td><asp:Label ID="fDepartmentKey" runat="server" Text='<%#Eval("DepartmentKey")%>'></asp:Label>
                        <td><asp:DropDownList ID="fDropDownList" runat="server"></asp:DropDownList></td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>
        </div>
        </form>
    </body>
    </html>
    
    
    DataList_X20090502.aspx.cs
    --------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class DataList_X200905020016 : System.Web.UI.Page
    {
        #region " Employee "
    
        private DataTable fEmployee;
        private DataTable Employee
        {
            get 
            {
                if (fEmployee == null)
                {
                    fEmployee = new DataTable();
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(int))); // 员工 Id
                    fEmployee.Columns.Add(new DataColumn("DepartmentKey", typeof(int))); // 员工部门 Id
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(string))); // 员工名称
    
                    Random fRandom = new Random();
                    for (int i = 1; i < 10; i++)
                    {
                        fEmployee.Rows.Add(new object[] { i, fRandom.Next(1, 11), string.Format("姓名 {0}", i) });
                    }
                }
    
                return fEmployee;
            }
        }
    
        #endregion
    
        #region " Department "
    
        private DataTable fDepartment;
        private DataTable Department
        {
            get
            {
                if (fDepartment == null)
                {
                    fDepartment = new DataTable();
                    fDepartment.Columns.Add(new DataColumn("DepartmentKey", typeof(int))); // 部门 Id
                    fDepartment.Columns.Add(new DataColumn("Name", typeof(string))); // 部门名称
    
                    Random fRandom = new Random();
                    for (int i = 1; i < 11; i++)
                    {
                        fDepartment.Rows.Add(new object[] { i, string.Format("部门 {0}", i) });
                    }
                }
    
                return fDepartment;
            }
        }
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ListView1.DataSource = this.Employee;
                this.ListView1.DataBind();
            }
        }
    
        private void BindList(DropDownList fList)
        {
            fList.DataTextField = "Name";
            fList.DataValueField = "DepartmentKey";
            fList.DataSource = this.Department;
            fList.DataBind();
        }
    
        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Label fControl1 = (Label)e.Item.FindControl("fEmployeeKey");
                Label fControl2 = (Label)e.Item.FindControl("fDepartmentKey");
                DropDownList fControl3 = (DropDownList)e.Item.FindControl("fDropDownList");
                this.BindList(fControl3);
    
                if (fControl3.Items.Count > 0 &&
                    fControl3.Items.FindByValue(fControl2.Text) != null)
                {
                    fControl3.SelectedValue = fControl2.Text;
                }
            }
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    • 已标记为答案 杜宏宇 2009年5月2日 1:36
    2009年5月1日 16:45
    版主

全部回复

  • 你好, DataBinder.Eval(e.Item, ("id")).ToString(); 换成User对应的部门 ID 试试
    知识改变命运,奋斗成就人生!
    2009年5月1日 13:11
    版主
  • 不行,我看了一个视频,如果是gridview就可以。它是用
    ddl.SelectedValue=DataBinder.Eval(e.Row.DataItem,"id").ToString();
    而换成listview,我就用
    ddl.SelectedValue=DataBinder.Eval(e.Item, ("id")).ToString(); 就不行
    2009年5月1日 13:20
  • 刚才贴出了前台代码,现在把后台代码也一并贴出,供高人研究:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace schy.manager.rygl
    {
        public partial class query : System.Web.UI.Page
        {
            DB.DB DBManager = new DB.DB();//数据库操作类实例化
            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SCHY"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind_Lv_User();
                }
            }
    
            protected void bind_Lv_User()
            {
                DataSet ds = new DataSet();
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();
                    using (SqlDataAdapter sda = new SqlDataAdapter("select * from [login]", conn))
                    {
                        sda.Fill(ds);
                    }
                }
                lv_User.DataSource = ds;
                lv_User.DataBind();
            }
    
            protected void lv_User_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
            {
    
            }
    
           //绑定部门
            protected void bind_Bm(DropDownList ddl_Ssbm)
            {
                SqlDataReader sdr = DBManager.getSdr("select [id],[department] from [department] order by px");
                ddl_Ssbm.DataSource = sdr;
                ddl_Ssbm.DataValueField = "id";
                ddl_Ssbm.DataTextField = "department";
                ddl_Ssbm.DataBind();
                sdr.Close();
                DBManager.closeCon();
            }
    
          
            protected void lv_User_ItemDataBound(object sender, ListViewItemEventArgs e)
            {
                DropDownList ddl = (DropDownList)e.Item.FindControl("ddl_Bm");
                bind_Bm(ddl);
                ddl.SelectedValue = DataBinder.Eval(e.Item, ("id")).ToString();
            }
      
        }
    }
    2009年5月1日 13:22
  • 你好,DataBinder.Eval(e.Item, ("id")).ToString() 是Login表中的 Id ,并不是Department的Id, 
    你须对 ddl.SelectedValue 设一个有效的 DepartmentId才能达到你的效果


    知识改变命运,奋斗成就人生!
    2009年5月1日 14:16
    版主
  • 那我要怎么做呢?现在大脑十分混乱,什么也想不清了。

    2009年5月1日 15:18
  • 你好,我写了个标例你对比修改一下

    DataList_X20090502.aspx
    --------------------------------------------------------------------------------------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200905020016.aspx.cs" Inherits="DataList_X200905020016" %>
    <!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></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListView ID="ListView1" DataKeyNames="EmployeeKey" runat="server" 
                onitemdatabound="ListView1_ItemDataBound">
                <LayoutTemplate>
                    <table cellpadding="1" cellspacing="0" width="80%" style="font-family: 宋体; font-size: 14px;
                        border-color: Black; border-collapse: collapse;" id="table_Fscxgdzcsq" align="center">
                        <tr>
                            <td>员工 ID</td>
                            <td>姓名</td>
                            <td>员工部门 Id</td>
                            <td>员工部门</td>
                        </tr>
                        <tr runat="server" id="itemPlaceholder">
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td><asp:Label ID="fEmployeeKey" runat="server" Text='<%#Eval("EmployeeKey")%>'></asp:Label></td>
                        <td><asp:Label ID="fName" runat="server" Text='<%#Eval("Name")%>'></asp:Label></td>
                        <td><asp:Label ID="fDepartmentKey" runat="server" Text='<%#Eval("DepartmentKey")%>'></asp:Label>
                        <td><asp:DropDownList ID="fDropDownList" runat="server"></asp:DropDownList></td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>
        </div>
        </form>
    </body>
    </html>
    
    
    DataList_X20090502.aspx.cs
    --------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class DataList_X200905020016 : System.Web.UI.Page
    {
        #region " Employee "
    
        private DataTable fEmployee;
        private DataTable Employee
        {
            get 
            {
                if (fEmployee == null)
                {
                    fEmployee = new DataTable();
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(int))); // 员工 Id
                    fEmployee.Columns.Add(new DataColumn("DepartmentKey", typeof(int))); // 员工部门 Id
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(string))); // 员工名称
    
                    Random fRandom = new Random();
                    for (int i = 1; i < 10; i++)
                    {
                        fEmployee.Rows.Add(new object[] { i, fRandom.Next(1, 11), string.Format("姓名 {0}", i) });
                    }
                }
    
                return fEmployee;
            }
        }
    
        #endregion
    
        #region " Department "
    
        private DataTable fDepartment;
        private DataTable Department
        {
            get
            {
                if (fDepartment == null)
                {
                    fDepartment = new DataTable();
                    fDepartment.Columns.Add(new DataColumn("DepartmentKey", typeof(int))); // 部门 Id
                    fDepartment.Columns.Add(new DataColumn("Name", typeof(string))); // 部门名称
    
                    Random fRandom = new Random();
                    for (int i = 1; i < 11; i++)
                    {
                        fDepartment.Rows.Add(new object[] { i, string.Format("部门 {0}", i) });
                    }
                }
    
                return fDepartment;
            }
        }
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ListView1.DataSource = this.Employee;
                this.ListView1.DataBind();
            }
        }
    
        private void BindList(DropDownList fList)
        {
            fList.DataTextField = "Name";
            fList.DataValueField = "DepartmentKey";
            fList.DataSource = this.Department;
            fList.DataBind();
        }
    
        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Label fControl1 = (Label)e.Item.FindControl("fEmployeeKey");
                Label fControl2 = (Label)e.Item.FindControl("fDepartmentKey");
                DropDownList fControl3 = (DropDownList)e.Item.FindControl("fDropDownList");
                this.BindList(fControl3);
    
                if (fControl3.Items.Count > 0 &&
                    fControl3.Items.FindByValue(fControl2.Text) != null)
                {
                    fControl3.SelectedValue = fControl2.Text;
                }
            }
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    • 已标记为答案 杜宏宇 2009年5月2日 1:36
    2009年5月1日 16:45
    版主
  • 您果真是传说中的高人!
    现在我才明白什么事text,什么是value,原来两者不一样的。dropdownlist进行选择只能是selectedvalue或selectedindex,不能是selectedtitem.text
    哎。。。。
    2009年5月2日 1:38