我有一個下拉列表,我正在從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天裏堅持了這一點。
非常感謝克拉克。我使用數字ID作爲值,並根據需要獲取emailId。有效!!! – 2011-03-25 07:13:38