Asp.net datavaluefield和datatextfield相同时dropdownlist行为不正常
我有一个dropdownlist,我正在从excel加载数据。Excel有两列:产品和电子邮件。产品列中的数据绑定到DataTextField,电子邮件列绑定到DataValueField。 当不同产品的电子邮件不同,但不同产品的电子邮件具有相同的值时,下拉列表可以正常工作,然后无论我选择什么,在回发时,所选值将更改为相同电子邮件值的第一项 下面是Excel中显示下拉菜单行为的示例数据 例1。(对于本例,下拉菜单适用)Asp.net datavaluefield和datatextfield相同时dropdownlist行为不正常,asp.net,drop-down-menu,Asp.net,Drop Down Menu,我有一个dropdownlist,我正在从excel加载数据。Excel有两列:产品和电子邮件。产品列中的数据绑定到DataTextField,电子邮件列绑定到DataValueField。 当不同产品的电子邮件不同,但不同产品的电子邮件具有相同的值时,下拉列表可以正常工作,然后无论我选择什么,在回发时,所选值将更改为相同电子邮件值的第一项 下面是Excel中显示下拉菜单行为的示例数据 例1。(对于本例,下拉菜单适用) 请在这方面帮助我,因为我在过去的3天里一直在做这件事。我认为ASP.NET假
请在这方面帮助我,因为我在过去的3天里一直在做这件事。我认为ASP.NET假定下拉列表中的值是唯一的。通常,值用于存储ID之类的内容,因此不必解析更具描述性的文本属性 当您进行回发时,ASP.NET从您的页面获得的只是正常的HTML表单post数据,加上一些ControlState和ViewState(如果已启用)。表单post数据将包含下拉列表的名称/id以及当前选定的值。ControlState/ViewState可能会在下拉列表中包含文本/值对的完整列表,以便在回发时自动重新填充控件,而无需担心 我猜在回发过程中,ASP.NET只是设置下拉列表的SelectedValue属性;由于您有非唯一的值,所以默认情况下只选择第一个值 基本上,您需要使下拉列表值唯一。在执行初始数据绑定时,可以选择复合值。看看您当前的实现,绑定到数据集,这可能有点麻烦。如果不绑定到数据集,而是绑定到对象列表,则可能会更容易。所以可能是这样的:
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();
内部类产品
{
公共int Id{get;set;}
公共字符串电子邮件{get;set;}
公共字符串ProductName{get;set;}
公共字符串复合ID
{
得到
{
返回String.Format(“{0}{1}”、this.Id、this.Email);
}
}
}
//在数据绑定中
列表产品=GetProductsFromDataSet(objDataset1);
ddlProduct.DataTextField=“ProductName”;
ddlProduct.DataValueField=“CompositeId”;
ddlProduct.DataSource=产品;
ddlProduct.DataBind();
或者不必麻烦使用复合ID,只需使用数字ID作为值,并在以后需要时查找相关的电子邮件地址。非常感谢。我使用一个数字ID作为值,并在需要时根据它获取emailId。成功了!!! Product Email iPad prashanth364@gmail.com iPhone 3G prashanth364@gmail.com iPhone4 prashanth364@gmail.com Product Email iPad prashanth364@yahoo.co.in iPhone 3G prashanth364@gmail.com iPhone4 prashanth364@gmail.com
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();
}
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();