hello, I am a beginner in ASP.NET and I really need your help.

I have a simple database, I want to show list of names (customers) in a dropdown list and, according to what I click on the dropdown list, to get the table updated below... with data corresponding to dropdown list selection.

I know there is GridView, and I know everything can be done automatically, but I want to learn coding and I want to do it myself... coz when I do it automatically it doesnt show any code so I can ponder on it and learn.

Please help
thanks

Dani AI

Generated

The behavior described in the thread almost always comes from rebinding the DropDownList on every Page_Load. As seen in ’s example, the list was populated unconditionally — enabling AutoPostBack (as suggested) then caused the selection to be reset on the postback so the SelectedIndexChanged logic never ran. The correct pattern is: bind the list only on the first load, enable AutoPostBack, and handle SelectedIndexChanged to fetch and bind the related rows.

Example markup and code pattern (illustrative):

<asp:DropDownList ID="drp" runat="server" AutoPostBack="True"
    OnSelectedIndexChanged="drp_SelectedIndexChanged" />

<asp:GridView ID="gv" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindCustomers();   // populate the dropdown only once
}

protected void drp_SelectedIndexChanged(object sender, EventArgs e)
{
    int id = int.Parse(drp.SelectedValue);
    using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString))
    using (var cmd = new SqlCommand("SELECT * FROM Orders WHERE CustomerID = @id", conn))
    {
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        conn.Open();
        gv.DataSource = cmd.ExecuteReader();
        gv.DataBind();
    }
}

Troubleshooting notes and best practices:

  • If SelectedIndexChanged does not fire, confirm AutoPostBack="True" and that OnSelectedIndexChanged is wired (markup or code-behind). ViewState must be enabled (default).
  • Avoid rebinding the DropDownList on postbacks — that is the single most common cause of “it didn’t work.”
  • Use parameterized queries (shown) and using blocks to dispose connections. Prefer explicit SqlDbType over AddWithValue for predictable types.
  • For smoother UX consider an UpdatePanel or client-side AJAX later; for learning purposes the server-side SelectedIndexChanged flow is ideal.

This ties back to ’s question about progress: since the dropdown is already populated, focus on the IsPostBack guard and the SelectedIndexChanged handler as the next steps.

Recommended Answers

All 4 Replies

ok, so what exactly is the question? I understand what you're wanting to accomplish, but where are you in the process? What have you tried so far? What are your results compared to what you expect? Are you getting any errors?

thanks for reply.
I already made one dropdown list which is pulling list of customers. It is ok. here is the code of that:

`protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ToString());
        SqlCommand cmd = new SqlCommand("SELECT ID, CustomerName+' '+CustomerSurname as Customer FROM tblCustomers",conn);

        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        drp.DataSource = dr;
        drp.DataTextField = "Customer";
        drp.DataValueField = "ID";
        drp.DataBind();
        conn.Close();

    }

Now, what I want to do is to make a table showing data relative to what I select in dropdown list.
So if I change, if I select something else in the dropdown list, the table also changes the data to show the data related to the customer selected in the dropdown list.

The easiest way to accomplish this is to do it automatically. But I would like to do it by myself...
How do I do it? I need some advices.

Thanks

Several ways you can do this...one is that enable the dropdownlist to autopost back. On a page load which is a post back you can check to see what has been selected and then do something based on that information.

I tried autopostback=true and dropdownlist didnt work at all then

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.