reset (reload or refresh) DropDownList with protected void Button1_Click(object sender, EventArgs e)

Cyber Learning 1 Reputation point
2022-12-26T18:17:25.423+00:00

Hello Forum Members

I am trying to reset (reload or refresh) the values of the dropdownlist.

Here is the code in asp.net:

<asp:DropDownList

ID="ddlUsage1" runat="server"

AutoPostBack="true"

DataSourceID="DropDownDataSourceUsage1"

DataTextField="Usage1"

DataValueField="Usage1"

AppendDataBoundItems="true" Width="100px"

OnSelectedIndexChanged="ddlUsage1_SelectedIndexChanged">

<asp:ListItem Text="All Usage" Value=""></asp:ListItem>

</asp:DropDownList>

code behind:

protected void Button1_Click(object sender, EventArgs e)

{

ddlUsage1.Items.Clear(); --- this is the problem.

}

Instead of clear (), how can I reset (reload or refresh) the values of the dropdownlist?

I sincerely appreciate your help.

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2022-12-27T02:48:04.34+00:00

    Hi @Cyber Learning ,

    When there is any update on your data source, you can call DataBind() again to reload the data source. Here is simple example:

       protected void Button1_Click(object sender, EventArgs e)  
               {  
                   string constr = ConfigurationManager.ConnectionStrings["conStr"].ToString();  
         
                   using (SqlConnection conn = new SqlConnection(constr))  
                   {  
                       string cmdString = "INSERT INTO dbo.DDLItems VALUES (5,'Item5','Value5')";  
                       conn.Open();  
                       using (SqlCommand comm = new SqlCommand(cmdString, conn))  
                       {  
                           comm.ExecuteNonQuery();  
                       }  
                   }  
         
                   ddlUsage1.DataBind();  
               }  
    

    Result:
    274008-result.gif

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Cyber Learning 1 Reputation point
    2023-01-16T19:23:27.4033333+00:00

    Hello Xudong Peng

    I apologize for not getting back to you sooner. I got pretty sick and now am recovering.

    I would like to resume this question.

    I have the clear button as follows:

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="CLEAR" />
    

    I have the code behind as follows:

    protected void Button1_Click(object sender, EventArgs e)
            {
                string constr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    string cmdString = "INSERT INTO dbo.DDLItems VALUES (5,'Item5','Value5')";
                    conn.Open();
                    using (SqlCommand comm = new SqlCommand(cmdString, conn))
                    {
                        comm.ExecuteNonQuery();
                    }
                }
                ddlUsage1.DataBind();
            }
    

    But, the button control still doesn't work.

    Do you have other suggestions on what else I need to check?

    Thank you.


  3. Cyber Learning 1 Reputation point
    2023-01-19T02:00:56.8933333+00:00

    Hello Xudong Peng

    I ended up using javascript, and it is providing the same effect.

    function ResetDropDownList()

    Thank you again for your help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.