Refresh child page on DropDownList change in UserControl using C# and VB.Net in ASP.Net

Last Reply 3 days ago By dharmendr

Posted 4 days ago

How to refresh child page after change in combo box located in user control

Here is my situation.

I have got #1 a master page, this master page contains #2 a child page where a #3 grid is located and a #4 user control. This user control contains a drop down box #5.

My idea is that when a user changes the item in the drop down box to refresh the grid located in the child page applying the value from the drop down as filter value.

So far, I used an autopostback on the drop down which stores the selected value in a session value, when the refresh occurs the whole page is reloaded but a SqlDataSource appears to be refreshed before the drop down updates the session value therefore, I am not showing the correct data.

Is there a better way to refresh de child page from a drop down located in a user control? Should I add a button? 

Posted 3 days ago Modified on 3 days ago

Hi Corobori,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

UserControl

Country:
<asp:DropDownList runat="server" ID="ddlCountries" AutoPostBack="true" OnSelectedIndexChanged="OnCountryChanged">
    <asp:ListItem Value="0" Text="Select" />
    <asp:ListItem Value="Germany" Text="Germany" />
    <asp:ListItem Value="Mexico" Text="Mexico" />
    <asp:ListItem Value="UK" Text="UK" />
    <asp:ListItem Value="USA" Text="USA" />
    <asp:ListItem Value="Canada" Text="Canada" />
</asp:DropDownList>

Default

<%@ Register Src="~/UC_Country.ascx" TagName="Country" TagPrefix="uc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <uc:Country ID="ucCountry" runat="server"></uc:Country>
    <hr />
    <asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="CustomerID" HeaderText="Id" />
            <asp:BoundField DataField="ContactName" HeaderText="Name" />
            <asp:BoundField DataField="City" HeaderText="City" />
        </Columns>
    </asp:GridView>
</asp:Content>

Namepsaces

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Code

UserControl

C#

public event EventHandler OnChangeValue;
protected void OnCountryChanged(object sender, EventArgs e)
{
    if (OnChangeValue != null)
    {
        OnChangeValue(sender, e);
    }
}

VB.Net

Public Event OnChangeValue As EventHandler
Protected Sub OnCountryChanged(ByVal sender As Object, ByVal e As EventArgs)
    RaiseEvent OnChangeValue(sender, e)
End Sub

Default

C#

protected void Page_Load(object sender, EventArgs e)
{
    ucCountry.OnChangeValue += new EventHandler(ValueChanged);
}

protected void ValueChanged(object sender, EventArgs e)
{
    DropDownList ddlCountry = (DropDownList)sender;
    string country = ddlCountry.SelectedValue.Trim();
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT CustomerID,ContactName,City FROM Customers WHERE Country = @Country";
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@Country", country);
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                gvCustomers.DataSource = dt;
                gvCustomers.DataBind();
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    ucCountry.OnChangeValue += New EventHandler(AddressOf ValueChanged)
End Sub

Protected Sub ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim ddlCountry As DropDownList = CType(sender, DropDownList)
    Dim country As String = ddlCountry.SelectedValue.Trim()
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT CustomerID,ContactName,City FROM Customers WHERE Country = @Country"
    Using con As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand = New SqlCommand(query)
        cmd.Parameters.AddWithValue("@Country", country)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd

            Using dt As DataTable = New DataTable()
                sda.Fill(dt)
                gvCustomers.DataSource = dt
                gvCustomers.DataBind()
            End Using
        End Using
    End Using
End Sub

Screenshot