在学习vs20008过程中发现其ListView控件不可像GridView那样可添加一个绑定了数据库的DropDownList。经过思考,找到也一个可行的方法。但当我重新打开电脑时,该方法就报错:DropDownList未被初始化。此问题到底时怎么回事?请各位大侠看看,代码如下:   html:
             <asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1" 
        DataKeyNames="Id" InsertItemPosition="LastItem" 
        oniteminserting="ListView1_ItemInserting" 
        onitemcreated="ListView1_ItemCreated">
        <ItemTemplate>
            <li style="background-color: #DCDCDC;color: #000000;">
                标题:
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
                <br />
                概述:
                <asp:Label ID="SummaryLabel" runat="server" 
                    Text='<%# Eval("Summary") %>' />
                <br />
                创建时间:
                <asp:Label ID="CreateDateTimeLabel" runat="server" 
                    Text='<%# Eval("UpdateDateTime") %>' />
                <br />
                上传者:
                <asp:Label ID="UploaderNameLabel" runat="server" 
                    Text='<%# Eval("UploaderName") %>' />
                <br />
                分类:
                <asp:Label ID="CategoryIdLabel" runat="server" 
                    Text='<%# Eval("CategoryId") %>' />
                <br />
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
            </li>
        </ItemTemplate>       
        <InsertItemTemplate>
            <li style="">
                标题:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TitleTextBox" runat="server" ErrorMessage="请输入相应的标题."></asp:RequiredFieldValidator>
                <br />
                概述:
                <asp:TextBox ID="SummaryTextBox" TextMode="MultiLine" Width="150" Height="60" runat="server" Text='<%# Bind("Summary") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="SummaryTextBox" runat="server" ErrorMessage="请输入正确的描述"></asp:RequiredFieldValidator>
                <br />
                归类:
                <asp:DropDownList ID="DropDownList1" runat="server">
                </asp:DropDownList>               
                <br />
                <asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="请选择文件后再上传"></asp:CustomValidator>
                <br />
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
            </li>
        </InsertItemTemplate>
        <LayoutTemplate>
<ul >
<li id="itemPlaceholder" runat="server" />
</ul>
        </LayoutTemplate>
    </asp:ListView>
        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="CollegeEnglishDataContext" EnableDelete="True" 
            EnableInsert="True" OrderBy="UpdateDateTime" TableName="ResourcesDetails" 
            oninserting="LinqDataSource1_Inserting">
        </asp:LinqDataSource>
                    <asp:LinqDataSource ID="LinqDataSource2" runat="server" 
                 ContextTypeName="CollegeEnglishDataContext" OrderBy="SortOrder" 
                 Select="new (Id, Name)" TableName="ResourcesCategory">
                </asp:LinqDataSource>
CodeBehind中的代码如下:    using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        if (!FileUpload1.HasFile)
        {
            CustomValidator CustomValidator1 =
                                 (CustomValidator)ListView1.InsertItem.FindControl("CustomValidator1");
            CustomValidator1.IsValid = false;
            e.Cancel = true;
        }
    }
    protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    {
        ResourcesDetails myResources = (ResourcesDetails)e.NewObject;        DropDownList DropDownList1 = (DropDownList)ListView1.InsertItem.FindControl("DropDownList1");
        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        string virtualFolder = "~/DownloadFold/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));        myResources.DownloadPath = Convert.ToString(virtualFolder + fileName + extension);        myResources.CategoryId = Convert.ToInt32(DropDownList1.SelectedValue);
        myResources.UpdateDateTime = DateTime.Now;
    }
    protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        DropDownList DropDownList1 = (DropDownList)ListView1.FindControl("DropDownList1");
        using (CollegeEnglishDataContext myDataContext = new CollegeEnglishDataContext())
        {
            var resourcesCategory = from p in myDataContext.ResourcesCategory
                                    select new
                                    {
                                        p.Id,
                                        p.Name
                                    };            DropDownList1.DataSource = resourcesCategory;
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataBind();
        }
    }
}

解决方案 »

  1.   

    数据库关系表设计如下:http://hi.csdn.net/attachment/201006/9/3558983_1276072091IJLr.png
    linq to sql class 如下:CollegeEnglish.dbmlhttp://hi.csdn.net/attachment/201006/9/3558983_127607209223Sf.png
      

  2.   

    数据库关系表设计如下:linq to sql class 如下:CollegeEnglish.dbml
      

  3.   

    DropDownList未被初始化???  New下试试
      

  4.   

    DropDownList DropDownList1 = (DropDownList)ListView1.FindControl("DropDownList1");已经将其初始化了
      

  5.   

    单步调试看看
    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
         DropDownList ddl= e.Item.FindControl("ddl") as DropDownList;
    }
    检查页面,在listview EdititemTemplate中DropDownList控件
      

  6.   

    http://www.cnblogs.com/redw/archive/2008/01/10/1033985.html这里讲的很详细,希望对你有帮助
      

  7.   

    我花了一个上午时间弄出了和楼主同样的问题,又弄了一个下午把问题解决了,我是菜鸟,我知道楼主的代码哪里出了问题 应该怎么改,但我不知道为什么要这么改,希望有高手来说明下.DropDownList DropDownList1 = (DropDownList)ListView1.FindControl("DropDownList1");改为:DropDownList DropDownList1 = (DropDownList)ListView1.InsertItem.FindControl("DropDownList1");
      

  8.   

    忘了说明下了,ListView直接找不到DropDownList的