DropDownList的不正常行为时datavaluefield和datatextfield相同 [英] dropdownlist not behaving properly when datavaluefield and datatextfield are same

查看:137
本文介绍了DropDownList的不正常行为时datavaluefield和datatextfield相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对我从Excel的数据加载一个DropDownList。 Excel中有2列的产品和电子邮件。在产品列中的数据绑定到DataTextField和Email列DataValueField。
下拉工作正常时,电子邮件是不同的产品不同,但是当电子邮件有不同的产品相同的值,那么无论我选择,在回发所选择的值更改为相同的电子邮件值的第一个项目。

I have a dropdownlist for which I am loading data from excel. Excel has 2 columns Product and Email. Data in Product column is binding to DataTextField and Email column to DataValueField. The dropdown works fine when email is different for different product but when email has same value for different product then whatever I select, on postback the selected value changes to the first item of the same email value.

下面是Excel中的样本数据显示下拉的行为

Below are the sample data in Excel to show the behavior of dropdown

例1(下拉工作正常,在这个例子中)

Example 1. (Drop down works fine for this example)

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

例2(在下面的例子中,无论我选择回来后的下拉菜单中选择值(i​​Pad或iPhone 3G或iPhone4的)将是新iPad)

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

例3(在下面的例子中,当我选择iPad的下拉工作正常,但当我选择岗位iPhone 3G或iPhone4的背面的下拉列表中选择值将是iPhone 3G的。基本上,在这里选择的iPhone4,上回发这表明iPhone 3G)

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

下面是我在哪里从Excel loaidng的数据下拉列表中选择功能

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天。

Please please help me on this as Iam stuck on this for past 3 days.

推荐答案

我想ASP.NET做一个假设,在下拉列表中的值将是独一无二的。通常值是用来存储像一个ID,所以你不必解析更具描述性的Text属性。

I think ASP.NET makes an assumption that the values in a drop-down list are going to be unique. Usually Values are used to store something like an ID, so you don't have to parse the more descriptive Text property.

当你做回传,所有的ASP.NET从您的页面得到的是普通的HTML表单提交的数据,再加上一些了ControlState-和视图状态(如果它启用)。表单POST数据将包含您的下拉列表中的名称/ ID,和当前选择的值。该了ControlState- / ViewState中可能会包含文本/值对的完整列表,在下拉列表中,这样的控制,可自动回发上重新填写你无须担心。

When you do a postback, all that ASP.NET is getting from your page is the normal HTML form post data, plus some ControlState and ViewState (if it's enabled). The form post data will contain the name/id of your drop-down list, and the currently selected value. The ControlState/ViewState will potentially contain the full list of text/value pairs in your drop-down list, so that the control can be automatically re-populated on postback without you needing to worry.

我回发ASP.NET是刚刚设置的下拉列表的SelectedValue属性中猜;如您有非唯一值,它只是默认为选择第一个。

I guess during a postback ASP.NET is just setting the SelectedValue property of the drop-down list; as you have non-unique values it's just defaulting to selecting the first one.

基本上,你需要让你的下拉列表中值是唯一的。当进行初始数据绑定,你可以选择一个综合值。纵观当前的实现,绑定到DataSet,这可能是一个有点痛。如果,代替结合你绑定到对象的列表数据集,这可能是更容易。因此,也许是这样的:

Basically, you need to make your drop-down list values unique. When doing the initial data-bind, you could opt for a composite value. Looking at your current implementation, binding to a DataSet, that might be a bit of a pain. If, instead of binding to a DataSet you were to bind to a list of objects, it might be easier. So maybe something like this:

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为值,当你需要它在以后查找了相关的电子邮件地址。

Or don't bother with a composite ID, just use a numerical ID for the Value, and look-up the associated email address when you need it at a later point.

这篇关于DropDownList的不正常行为时datavaluefield和datatextfield相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆