根据if条件删除dropdownlist中已经绑定的值

时间:2019-02-20 12:08:32

标签: javascript c# asp.net

我已经将值绑定到下拉列表,但是当下一个条件触发时,previos绑定值没有消失。它们显示包括前一个值在内的所有值。

  

设计代码

<div class="row">
                <div class="col-md-12">
                    <div class="col-md-3">
                        <div class="form-group input-group">
                            <div class="form-group input-group">
                                <span class="input-group-addon"><i class="fa fa-tag">User Type</i></span>
                                <asp:DropDownList ID="ddlUserType" runat="server" CssClass="form-control" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlUserType_SelectedIndexChanged"></asp:DropDownList>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-9">
                        <div class="form-group input-group">
                            <div class="form-group input-group">
                                <span class="input-group-addon"><i class="fa fa-tag">User List</i></span>
                                <asp:DropDownList ID="ddlUserList"  runat="server" CssClass="form-control" AutoPostBack="true" AppendDataBoundItems="true"  ></asp:DropDownList>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
  

cs文件

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        Session["Firmcode"] = "1";
        Session["ConnStr"] = ConfigurationManager.ConnectionStrings["Cnn"].ToString();
        string query = "SELECT id,usertype_name FROM master_usertype ";
        BindControls.BindDropdownlist(1, 0, ddlUserType, Datalayer.GetDatatable(query, Session["ConnStr"].ToString()));
        ddlUserType.Items.Insert(0, "select");


    }
}



protected void ddlUserType_SelectedIndexChanged(object sender, EventArgs e)
{

    // ddlUserList.Enabled = true;
    int user_type = Convert.ToInt32(ddlUserType.SelectedItem.Value);
    if (user_type == 1)
    {
        string qry = "select e.auto_number,e.employeename from master_employee as e RIGHT JOIN master_user as u on e.employeecode = u.entity_id where u.type =" + user_type;
        BindControls.BindDropdownlist(1, 0, ddlUserList, Datalayer.GetDatatable(qry, Session["ConnStr"].ToString()));
        ddlUserList.Items.Insert(0, "select");

    }
    else if(user_type == 2)
        {
            string qry = "select d.auto_number,d.doctorname from master_doctor as d RIGHT JOIN master_user as u on d.doctorcode = u.entity_id where u.type =" + user_type;
            BindControls.BindDropdownlist(1, 0, ddlUserList, Datalayer.GetDatatable(qry, Session["ConnStr"].ToString()));
            ddlUserList.Items.Insert(0, "select");
        }

}

第一个条件有2个值,第二个条件有2个值。显示了第二条件的所有值。如何仅显示必填字段?我是一个初学者。请原谅我,如果这很基础! 请帮助

附加的屏幕截图。 Second condition

First condition

1 个答案:

答案 0 :(得分:0)

Please try like this
protected void ddlUserType_SelectedIndexChanged(object sender, EventArgs e)
{
     //To clear existing items
     ddlUserList.Items.Clear();
    int user_type = Convert.ToInt32(ddlUserType.SelectedItem.Value);
    if (user_type == 1)
    {

      //your coding here....
    }
}