asp.net - 当 datavaluefield 和 datatextfield 相同时,下拉列表无法正常运行

标签 asp.net drop-down-menu

我有一个下拉列表,我正在为其加载来自 excel 的数据。 Excel 有 2 列产品和电子邮件。 Product 列中的数据绑定(bind)到 DataTextField,Email 列绑定(bind)到 DataValueField。 当不同产品的电子邮件不同时,下拉列表工作正常,但当电子邮件对不同产品具有相同值时,无论我选择什么,在回发时,所选值都会更改为相同电子邮件值的第一项。

下面是 Excel 中的示例数据,用于显示下拉列表的行为

示例 1。(对于此示例,下拉效果很好)

 
Product               Email 
iPad                  prashanth364@gmail.com
iPhone 3G             prashanth364@yahoo.co.in
iPhone4               prashanth364@in.com

Example 2. (In the example below, whatever I select (iPad or iPhone 3G or iPhone4) on post back the dropdown selected value will be iPad)

 
Product               Email 
iPad                  prashanth364@gmail.com
iPhone 3G             prashanth364@gmail.com
iPhone4               prashanth364@gmail.com

Example 3. (In the example below, when I select iPad the dropdown works fine but when I select iPhone 3G or iPhone4 on post back the dropdown selected value will be iPhone 3G. Basically, on selecting iPhone4 here, on post back it shows iPhone 3G)

 
Product               Email 
iPad                  prashanth364@yahoo.co.in
iPhone 3G             prashanth364@gmail.com
iPhone4               prashanth364@gmail.com

Below is the function where I am loaidng the data from excel to dropdown

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();

}

请帮我解决这个问题,因为过去 3 天我一直坚持这个问题。

最佳答案

我认为 ASP.NET 假定下拉列表中的值将是唯一的。通常 Values 用于存储 ID 之类的内容,因此您不必解析更具描述性的 Text 属性。

当您执行回发时,ASP.NET 从您的页面获取的只是普通的 HTML 表单发布数据,加上一些 ControlState 和 ViewState(如果已启用)。表单发布数据将包含您的下拉列表的名称/ID,以及当前选择的值。 ControlState/ViewState 可能会在您的下拉列表中包含完整的文本/值对列表,因此该控件可以在回发时自动重新填充,您无需担心。

我想在回发期间 ASP.NET 只是设置下拉列表的 SelectedValue 属性;因为您有非唯一值,所以它只是默认选择第一个。

基本上,您需要使下拉列表值唯一。在进行初始数据绑定(bind)时,您可以选择复合值。查看您当前的实现,绑定(bind)到数据集,这可能有点麻烦。如果不是绑定(bind)到 DataSet 而不是绑定(bind)到对象列表,则可能会更容易。所以也许是这样的:

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 作为值,并在以后需要时查找关联的电子邮件地址。

关于asp.net - 当 datavaluefield 和 datatextfield 相同时,下拉列表无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5416232/

相关文章:

asp.net - ASP MVC 授权除少数以外的所有操作

javascript下拉填充

asp.net - 防止 FormsAuthenticationModule 拦截 ASP.NET Web API 响应

php - 如何在下拉更改事件中调用 AJAX 请求?

css - Bootstrap 中带有子菜单的可滚动下拉菜单

jquery - 选择框在 mozilla 中无法正常工作

c# - 循环访问 UserControl 的控件

asp.net - 非 ASCII 字符需要 web.config 吗?

html - 带有 <select> 的 CSS 下拉菜单在 IE 中不可用