2011-03-24 36 views
2

我有一個下拉列表,我正在從excel中加載數據。 Excel有2列產品和電子郵件。 「產品」列中的數據綁定到DataTextField並將電子郵件列綁定到DataValueField。 對於不同的產品,當電子郵件不同但電子郵件對不同產品具有相同值時,下拉菜單正常工作,然後將所選值更改回發送到同一電子郵件值的第一項。當datavaluefield和datatextfield相同時,dropdownlist無法正常工作

下面是在Excel中的採樣數據,以顯示下方下拉的行爲

實施例1(下拉對於此示例正常工作)

 
Product    Email 
iPad     [email protected] 
iPhone 3G    [email protected] 
iPhone4    [email protected] 

實施例2(在該例子中,不管我選擇上柱(iPad或iPhone 3G或iPhone4的)背面的下拉選擇的值將是ipad公司)

 
Product    Email 
iPad     [email protected] 
iPhone 3G    [email protected] 
iPhone4    [email protected] 

實施例3(在下面的例子中,當我選擇的iPad下拉工作正常,但當我選擇iPhone 3G或iPhone4後回選擇的下拉列表值將是iPhone 3G。基本上,在這裏選擇的iPhone4,上回發這表明iPhone 3G)

 
Product    Email 
iPad     [email protected] 
iPhone 3G    [email protected] 
iPhone4    [email protected] 

下面就是我從Excel loaidng數據的功能到下拉

private void ExtractFromExcelInitial() 
{ 

    // Put user code to initialize the page here 
    // Create connection string variable. Modify the "Data Source" 
    // parameter as appropriate for your environment. 
    string ExcelFilePath = Server.MapPath("~/ProductExcel") + "\\ProductEmail.xls"; 
    String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
    "Data Source=" + ExcelFilePath + ";" + 
    "Extended Properties=Excel 8.0;"; 

    // Create connection object by using the preceding connection string. 
    OleDbConnection objConn = new OleDbConnection(sConnectionString); 

    // Open connection with the database. 
    objConn.Open(); 

    // The code to follow uses a SQL SELECT command to display the data from the worksheet. 

    // Create new OleDbCommand to return data from worksheet. 
    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn); 


    // Create new OleDbDataAdapter that is used to build a DataSet 
    // based on the preceding SQL SELECT statement. 
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); 

    // Pass the Select command to the adapter. 
    objAdapter1.SelectCommand = objCmdSelect; 

    // Create new DataSet to hold information from the worksheet. 
    DataSet objDataset1 = new DataSet(); 

    // Fill the DataSet with the information from the worksheet. 
    objAdapter1.Fill(objDataset1, "XLData"); 


    ddlProduct.DataTextField = "Product"; 
    ddlProduct.DataValueField = "Emailid"; 
    ddlProduct.DataSource = objDataset1.Tables[0]; 

    ddlProduct.DataBind(); 
    ddlProduct.Items.Insert(0, new ListItem("Select Product", "0")); 

    // Bind data to DataGrid control. 
    ////DataGrid1.DataSource = objDataset1.Tables[0].DefaultView; 
    ////DataGrid1.DataBind(); 

    // Clean up objects. 
    objConn.Close(); 

} 

請請幫助我在此爲Iam在過去的3天裏堅持了這一點。

回答

1

我認爲ASP.NET假定下拉列表中的值將是唯一的。通常,值用於存儲類似ID的內容,因此您不必解析更具描述性的Text屬性。

當您執行回發時,所有ASP.NET從您的頁面獲得的是正常的HTML表單發佈數據,以及一些ControlState和ViewState(如果已啓用)。表單發佈數據將包含您的下拉列表的名稱/ ID和當前選定的值。 ControlState/ViewState可能會在您的下拉列表中包含文本/值對的完整列表,以便控件可以在回放時自動重新填充,而無需擔心。

我猜在回發期間ASP.NET只是設置下拉列表的SelectedValue屬性;因爲你有非唯一的值,只是選擇第一個值而已。

基本上,您需要使您的下拉列表值具有唯一性。在進行初始數據綁定時,可以選擇複合值。看看你當前的實現,綁定到一個DataSet,這可能有點痛苦。如果不綁定到DataSet,而是綁定到對象列表,則可能會更容易。因此,也許是這樣的:

internal class Product 
{ 
    public int Id { get; set; } 
    public string Email { get; set; } 
    public string ProductName { get; set; } 
    public string CompositeId 
    { 
     get 
     { 
     return String.Format("{0}|{1}", this.Id, this.Email); 
     } 
    } 
} 

// in the data-binding 
List<Product> products = GetProductsFromDataSet(objDataset1); 
ddlProduct.DataTextField = "ProductName"; 
ddlProduct.DataValueField = "CompositeId"; 
ddlProduct.DataSource = products; 
ddlProduct.DataBind(); 

還是不要用複合ID麻煩,只需使用一個數字ID爲值,當你需要它在以後查找相關的電子郵件地址。

+0

非常感謝克拉克。我使用數字ID作爲值,並根據需要獲取emailId。有效!!! – 2011-03-25 07:13:38

相關問題