简体   繁体   中英

Populating /Refreshing Drop down list based on user input

I am working on an asp.net application. I have a drop down list of customer account no. When i enter customer id card number in text box and click search button, the drop down should be populated with all the account numbers provided against that id card number previously. It works fine but when i add a new id card number and click search the drop down still retains the previous customer's account numbers. I want the drop down to empty and reppulate everytime i click Search and the item at value 1 ie Other should stay constant rest of the list changes dynamically.

 protected void Search_Click(object sender, EventArgs e) { ddl_accno.Items.Clear(); ddl_accno.Items.Add(new ListItem("Other", "0")); string cnic = txt_cnic.Text; BindControls.ControlBinder.BindDropDown(ddl_accno, UL.GetAccountNo(cnic),"ACCOUNT_NO","ACCOUNT_NO"); } 
 <td style="width:275px"> <label for="textfield"> Account no.</label> <asp:DropDownList Font-Size="Small" ID="ddl_accno" runat="server" AutoPostBack = "True" Width="319px" AppendDataBoundItems="true" onselectedindexchanged="ddl_accno_SelectedIndexChanged"> <asp:ListItem Value="0" Selected="True" Text="Select Account No"></asp:ListItem> <asp:ListItem Value="1" Text="Other"></asp:ListItem> </asp:DropDownList><br /> 

I am using UL.GetAccountNO() function to populate ddl with query. If the user selects other a text box will be visible where user can enter an account number apart from the ones in the drop down.

Pasting a sample code:

Design:

<asp:DropDownList ID="AssignedToDropDownList" runat="server" DataSourceID="UserLinqDataSource"
        DataTextField="UserName" DataValueField="UserId" AppendDataBoundItems="true"
        SelectedValue='<%# Bind("AssignedTo") %>'>
    </asp:DropDownList>

Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        DDLDataBind();
    }

    private void DDLDataBind()
    {
        AssignedToDropDownList.Items.Clear();
        AssignedToDropDownList.Items.Add(new ListItem("--did not assign--", "0"));
        AssignedToDropDownList.DataBind();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM