Vote count:
0
I have a listview which is populated from database and is manually binded in codebehind with BindGrid(). This works fine and gets all the right data.
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable dt = new DataTable();
da.Fill(dt);
listView.DataSource = dt;
listView.DataBind();
I have two dropdowns. One contains sorting through, ALL, Country, Price Low - High, Price High To Low.
<asp:DropDownList ID="DDLSorting" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDLSorting_SelectedIndexChanged" >
<asp:ListItem Text="All" Value="All"></asp:ListItem>
<asp:ListItem Text="Country" Value="Country"></asp:ListItem>
<asp:ListItem Text="Price - Lowest to Highest" Value="PriceL"></asp:ListItem>
<asp:ListItem Text="Price - Highest to Lowest" Value="PriceH"></asp:ListItem>
</asp:DropDownList>
When Country is selected it should populate the second dropdown called ddlCountries with SELECT Distinct Countries otherwise it is disabled.
<asp:DropDownList ID="DDLCountries" runat="server" AutoPostBack="True" Enabled = "false" >
<asp:ListItem Text = "--Select Country--" Value = ""></asp:ListItem>
</asp:DropDownList>
Problems I am having:
- When I select anything from dropdown the listview doesn't rebind/refresh with the new sorted data? I have tried putting
BindGrid(); in each of the if statement but that didn't work! - Not sure of how to bind the countries when Country is selected to the second dropdown?
`
protected void ddlSorting_SelectedIndexChanged(object sender, EventArgs e)
{
string con = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataReader reader = null;
SqlConnection Con = new SqlConnection(con);
string query = string.Empty;
if (DDLSorting.SelectedValue == "All")
{
query = "SELECT * FROM Wines";
}
else if (DDLSorting.SelectedValue == "Country")
{
query = "SELECT DISTINCT Country FROM Wines";
DDLCountries.Enabled = true;
}
else if
{
query = "SELECT * FROM Wines ORDER BY price {0}";
if (DDLSorting.SelectedValue == "PriceL")
{
query = string.Format(query, "ASC");
}
else if(DDLSorting.SelectedValue == "PriceH")
{
query = string.Format(query, "DESC");
}
}
SqlCommand cmd = new SqlCommand(query, Con);
Con.Open();
BindGrid();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
asked 58 secs ago
Sorting listview with values from dropdown asp.net
Aucun commentaire:
Enregistrer un commentaire