具有相同来源的不同下拉列表给出相同的值。如何处理?

时间:2017-08-16 05:46:46

标签: asp.net datatable dropdown

我使用相同的oledb数据适配器绑定了三个下拉列表。这个oledbdataadapter填充了三个不同的数据表,我最终用它来填充三个下拉列表。但是当我在提交按钮中访问三个下拉值时,即使我在下拉列表中选择了三个不同的选项,所有三个下拉列表都会给出相同的选定项目。有人可以帮忙吗? 这是我的代码: -

//oda - oledbdataadapter
oda.SelectCommand = cmdExcel;
oda.Fill(dt_Temp);
DataTable dt_costcode = new DataTable();
oda.Fill(dt_costcode);
DataTable dt_desc = new DataTable();
oda.Fill(dt_desc);
DataTable dt_unit = new DataTable();
oda.Fill(dt_unit);
connExcel.Close();
ViewState["TempImport"] = dt_Temp;

List<CostColumns> list_costCode = Columns(dt_costcode);
Bind_ddlCostCode(list_costCode);

List<CostColumns> list_desc = Columns(dt_desc);
Bind_ddlDescription(list_desc);

List<CostColumns> list_unit = Columns(dt_unit);
Bind_ddlUnitofMeasure(list_unit);

此处用于绑定下拉列表的代码: -

public void Bind_ddlCostCode(List<CostColumns> list_costCode)
    {
        foreach (CostColumns costcol in list_costCode.ToList())
        {
            if (costcol.ColumnType == "System.String")
            {
                ddlCostCodeNumber.Items.Add(new ListItem(costcol.Columns, costcol.ColumnType));
            }
        }
        ddlCostCodeNumber.DataBind();
        ddlCostCodeNumber.Items.Insert(0, new ListItem("--Select CostCode--", "0"));
    }
    public void Bind_ddlDescription(List<CostColumns> list_desc)
    {

        foreach (CostColumns costcol in list_desc.ToList())
        {
            if (costcol.ColumnType == "System.String")
            {
                ddlDescription.Items.Add(new ListItem(costcol.Columns, costcol.ColumnType));
            }
        }
        ddlDescription.DataBind();
        ddlDescription.Items.Insert(0, new ListItem("--Select Desc.--", "0"));
    }
//(Same code for Bind_ddlUnitofMeasure(list_unit))

这就是出错的地方: -

 protected void Btn_ImportRecords_Click(object sender, EventArgs e)
    {
        DataTable dt_Temp= (DataTable)ViewState["TempImport"];
        DataTable dt_Import = new DataTable();
        Int32 i = costCodeNo();
        string ccn = ddlCostCodeNumber.SelectedItem.Text;//gives same selected option(CostCodeNumber)
        string desc = ddlDescription.SelectedItem.Text;//gives same selected option
        string unit = ddlUnitofMeasure.SelectedItem.Text;//gives same selected option
        dt_Import.Columns.Add(new DataColumn("CostCodeNo", typeof(Int32)));
        dt_Import.Columns.Add(new DataColumn(ddlCostCodeNumber.SelectedItem.Text, typeof(string)));
        dt_Import.Columns.Add(new DataColumn(ddlDescription.SelectedItem.Text, typeof(string)));
       //throws exception that dt_Import already has same column name
}

Selected different values

0 个答案:

没有答案