下拉列表中有两次相同的数据

时间:2014-12-11 04:54:56

标签: c#

我有3个下拉列表:

<tr>
<td align="left" class="style2">
Emp Code</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel6" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddcode" runat="server" AutoPostBack="True" 
onselectedindexchanged="ddcode_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddcode" ForeColor="Red">* 
</asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left" class="style2">
Company</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddcompany" runat="server" AutoPostBack="True"> 
 </asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddcompany" ForeColor="Red">* 
</asp:RequiredFieldValidator>
 </ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left" class="style2">
 Branch</td>
 <td align="left">
 <asp:UpdatePanel ID="UpdatePanel2" runat="server">
 <ContentTemplate>
 <div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
 <asp:DropDownList ID="ddbranch" runat="server" 
 onselectedindexchanged="ddbranch_SelectedIndexChanged">
  </asp:DropDownList>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
  ErrorMessage="RequiredFieldValidator" ControlToValidate="ddbranch" ForeColor="Red">*
 </asp:RequiredFieldValidator>
 </ContentTemplate> </asp:UpdatePanel>
 </td>
 </tr>

我还有一个下拉课程:

 public class Dropdown
{
    Connection con = new Connection();
    public void dropdwnlist(string qry, DropDownList ddl)
    {
       con.gettable(qry);
       if (con.dt.Rows.Count > 0)
       {
           if (con.dt.Columns.Count == 2)
           {
               string str1 = con.dt.Columns[0].ColumnName.ToString();
               string str2 = con.dt.Columns[1].ColumnName.ToString();
               ddl.DataValueField = str1;
               ddl.DataTextField = str2;
               ddl.DataSource = con.dt;
               ddl.DataBind();
               con.dt.Columns.Remove(str1);
               con.dt.Columns.Remove(str2);
           }
           else
           {
               string str = con.dt.Columns[0].ColumnName.ToString();
               ddl.DataValueField = str;
               ddl.DataTextField = str;
               ddl.DataSource = con.dt;
               ddl.DataBind();
               con.dt.Columns.Remove(str);
               //con.dt.Columns.Remove(str2);
           }

       }
           ddl.Items.Insert(0, ("--Select--"));



    }

我的按钮点击是:

Dropdown dwn = new Dropdown();
    string str = "";
    SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillempcode();
            //fillcompany();
            //filldepartment();
        }
    }
    protected void fillempcode()
    {

        string str = "select MachID,EmpCode from EmpDetails where StatusID='0'";
        dwn.dropdwnlist(str, ddcode);

    }
    protected void fillcompany()
    {
        //Where EmpCode='" + ddcode.SelectedItem.Text + "'
        str = "select CompanyID,CompanyName from Company ";
        dwn.dropdwnlist(str, ddcompany);
        str = "select CompanyID from View_Company where EmpCode='" + ddcode.SelectedItem.Text + "'";
        dr=conn.query(str);
        if (dr.Read())
        {
            string id = dr[0].ToString();
            ddcompany.SelectedItem.Text = id;
        }
    }
    protected void fillbranch()
    {
        //where EmpCode='" + ddcode.SelectedItem.Text + "'
        string str = "select BranchID,BranchName from View_Branch";
        dwn.dropdwnlist(str, ddbranch);
    }

我的问题是,当我在EmpCode下拉列表中选择时,我想启用所有dropdownllist。现在它工作正常,但公司和分支下拉列表显示相同的值twise。我可以知道原因吗?

1 个答案:

答案 0 :(得分:0)

尝试重写像这样的dropdwnlist方法,

    public void dropdwnlist(string qry, DropDownList ddl)
    {
       ddl.Items.Clear();

       con.gettable(qry);
       if (con.dt.Rows.Count > 0)
       {
           if (con.dt.Columns.Count == 2)
           {
               string str1 = con.dt.Columns[0].ColumnName.ToString();
               string str2 = con.dt.Columns[1].ColumnName.ToString();
               ddl.DataValueField = str1;
               ddl.DataTextField = str2;
               ddl.DataSource = con.dt;
               ddl.DataBind();
               con.dt.Columns.Remove(str1);
               con.dt.Columns.Remove(str2);
           }
           else
           {
               string str = con.dt.Columns[0].ColumnName.ToString();
               ddl.DataValueField = str;
               ddl.DataTextField = str;
               ddl.DataSource = con.dt;
               ddl.DataBind();
               con.dt.Columns.Remove(str);
               //con.dt.Columns.Remove(str2);
           }    
       }
       ddl.Items.Insert(0, ("--Select--"));
       ddl.SelectedIndex = 0;
    }

希望这会有所帮助......