当数据值字段和数据文本字段相同时,下拉列表行为不正确
我有一个下拉列表,我正在从 Excel 加载数据。 Excel 有 2 列“产品”和“电子邮件”。产品列中的数据绑定到 DataTextField,电子邮件列绑定到 DataValueField。 当不同产品的电子邮件不同时,下拉菜单工作正常,但当不同产品的电子邮件具有相同值时,无论我选择什么,回发时所选值都会更改为相同电子邮件值的第一项。
下面是 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 加载到下拉列表的功能,
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 天里一直坚持这个问题。
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.
Below are the sample data in Excel to show the behavior of dropdown
Example 1. (Drop down works fine for this example)
Product Email iPad [email protected] iPhone 3G [email protected] iPhone4 [email protected]
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 [email protected] iPhone 3G [email protected] iPhone4 [email protected]
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 [email protected] iPhone 3G [email protected] iPhone4 [email protected]
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();
}
Please please help me on this as Iam stuck on this for past 3 days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 ASP.NET 假设下拉列表中的值是唯一的。通常,值用于存储 ID 之类的内容,因此您不必解析更具描述性的 Text 属性。
当您进行回发时,ASP.NET 从您的页面获取的只是普通的 HTML 表单发布数据,以及一些 ControlState 和 ViewState(如果已启用)。表单发布数据将包含下拉列表的名称/ID 以及当前选择的值。 ControlState/ViewState 可能会在下拉列表中包含文本/值对的完整列表,以便控件可以在回发时自动重新填充,而无需担心。
我想在回发期间 ASP.NET 只是设置下拉列表的 SelectedValue 属性;由于您有非唯一值,因此默认选择第一个值。
基本上,您需要使下拉列表值唯一。在进行初始数据绑定时,您可以选择复合值。看看您当前的实现,绑定到数据集,这可能有点痛苦。如果您不绑定到数据集,而是绑定到对象列表,则可能会更容易。所以也许是这样的:
或者不用费心使用复合 ID,只需使用数字 ID 作为值,并在稍后需要时查找关联的电子邮件地址。
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.
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.
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.
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:
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.