Access MasterPage DropDownList in ContentPage and filter ASP.Net GridView using C#

Last Reply one year ago By dharmendr

Posted one year ago

you are right and i appreciate with this demo but i want to call my child page gridview in this method.

protected void ChangeCountry(object sender, EventArgs e)
{
    (((this.Master) as MasterPage).FindControl("ddlCountries") as DropDownList).ClearSelection();
    ((this.Master) as MasterPage).Country_Changed(null, null);
    -- i want to call here child page gridview with dropdown selected value id.
}

 

Posted one year ago

Hi varun.p,

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

MasterPage.master

<form id="form1" runat="server">
<div>
    Country:&nbsp;<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true">
    </asp:DropDownList>
    <hr />
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
</div>
</form>

CS.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Button ID="Button1" Text="Search" runat="server" OnClick="Search" />
    <br />
    <asp:GridView runat="server" AutoGenerateColumns="false" ID="gvCustomers">
        <Columns>
            <asp:BoundField DataField="CustomerId" HeaderText="Id" />
            <asp:BoundField DataField="ContactName" HeaderText="Name" />
            <asp:BoundField DataField="Country" HeaderText="Country" />
        </Columns>
    </asp:GridView>
</asp:Content>

Namespaces

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

MasterPage.master.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string query = "SELECT DISTINCT Country FROM Customers WHERE Country <> ''";
        BindDropDownList(ddlCountries, query, "Country", "Country", "Select Country");
    }
}

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            con.Open();
            ddl.DataSource = cmd.ExecuteReader();
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
            con.Close();
        }
    }
    ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}

CS.aspx.cs

protected void Search(object sender, EventArgs e)
{
    string country = (((this.Master) as MasterPage).FindControl("ddlCountries") as DropDownList).SelectedItem.Text;
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT * FROM Customers WHERE Country = @Country";
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            cmd.Parameters.AddWithValue("@Country", country);
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds, "Customers");
                gvCustomers.DataSource = ds;
                gvCustomers.DataBind();
            }
        }
    }
}

Screenshot