Pass (Send) multiple DropDownList value and filter ASP.Net GridView in another Page using C# and VB.Net

Last Reply one month ago By arunkurmi

Posted one month ago

Hi,

on page A, i have 2 dropdownlist. The dropdown list need to send the value to the page B,

so it can extract the data from database and show it in a gridview inside page B.

Below are the example of my code.

Page A:

<div>
    <asp:DropDownList ID="ddltahun" AutoPostBack="true" runat="server" class="dd">
        <asp:ListItem Value=""></asp:ListItem>
        <asp:ListItem Value="1">2017</asp:ListItem>
        <asp:ListItem Value="2">2018</asp:ListItem>
        <asp:ListItem Value="2019">2019</asp:ListItem>
    </asp:DropDownList>
    <br />
    <asp:DropDownList ID="ddlsesi" AutoPostBack="true" runat="server" class="dd">
        <asp:ListItem Value=""></asp:ListItem>
        <asp:ListItem Value="1">Sesi 1</asp:ListItem>
        <asp:ListItem Value="2.1">Sesi 2</asp:ListItem>
        <asp:ListItem Value="3.1">Sesi 3</asp:ListItem>
    </asp:DropDownList>
    <br />
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Submit_OnClick" />
    </div>
</div>

 

protected void Submit_OnClick(object sender, EventArgs e)
{
    string tahun = ddltahun.SelectedItem.Text.Trim();
    string sesi = ddlsesi.SelectedItem.Text.Trim();
    Session["tahun"] = tahun;
    Session["sesi"] = sesi;

    if (tahun == "2019" && sesi == "Sesi 1")
    {
        Response.Redirect("~/dn_soalan.aspx?tahun=" + ddltahun.SelectedValue + "sesi=" + ddlsesi.SelectedValue);
    }
}

Page B:

<td class="auto-style1" style="border-right-style: solid; border-right-width: 1px;
    border-color: #ede8e8">
    <asp:GridView ID="GridView2" runat="server" BorderColor="White" BorderWidth="0px"
        AutoGenerateColumns="false" GridLines="None">
        <Columns>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <%--<%# Container.DataItemIndex + 1 %>--%></ItemTemplate>
                <ItemStyle HorizontalAlign="Center" Width="50px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <h3 class="h3">
                        <asp:HyperLink ID="label_soalan" runat="server" Text='<%# Eval("tajuk")%>' NavigateUrl='<%# Eval("id","~/parliment/dewan/dn/year/2017_sesi1(J).aspx?id={0}")%>'></asp:HyperLink></h3>
                    <asp:Label class="detail" ID="label_ap" runat="server" Text='<%# Eval("as_name")%>'></asp:Label><br />
                    <asp:Label class="detail" ID="label_tarikh1" runat="server" Text='TARIKH :'></asp:Label><asp:Label
                        ID="lblDate" runat="server" Text='<%# Eval("tarikh")%>'></asp:Label><br />
                    <asp:Label class="detail" ID="label_no1" runat="server" Text='NO. SOALAN :'></asp:Label><asp:Label
                        ID="label_no" runat="server" Text='<%# Eval("no_sln")%>'></asp:Label>
                    <hr />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</td>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}
private void BindData()
{
    string v = Request.QueryString["tahun"];
    string v1 = Request.QueryString["sesi"];
    string strQuery = "select post_DN.postDN, post_DN.tajuk, post_DN.tarikh, post_DN.no_sln, ahli_senat.id, ahli_senat.as_name from post_DN INNER JOIN ahli_senat ON post_DN.postDN = ahli_senat.id " +
        "where tahun='" + v + "' and sesi='" + v1 + "' and jenis='Lisan' ";
    SqlCommand cmd = new SqlCommand(strQuery);
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();

}
private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();

    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    //lblDate.Text = DateTime.Parse(dt.Rows[0][0].ToString()).ToString("dd-MM-yyyy");
    return dt;


}

Please, if you can help me. Really appriciate it. thank you :)

Posted one month ago

Hi edaadura,

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

PageA

Country:
<asp:DropDownList ID="ddlCountry" runat="server">
    <asp:ListItem Value="1">--Seletc--</asp:ListItem>
    <asp:ListItem Value="2">UK</asp:ListItem>
    <asp:ListItem Value="3">USA</asp:ListItem>
</asp:DropDownList>
<br />
City:
<asp:DropDownList ID="ddlCity" runat="server">
    <asp:ListItem Value="1">--Seletc--</asp:ListItem>
    <asp:ListItem Value="2">London</asp:ListItem>
    <asp:ListItem Value="3">Seattle</asp:ListItem>
    <asp:ListItem Value="4">Tacoma</asp:ListItem>
    <asp:ListItem Value="5">Redmond</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit_OnClick" />

PageB

<asp:GridView ID="gvEmployee" runat="server" BorderColor="White" BorderWidth="0px"
    AutoGenerateColumns="false" GridLines="None" EmptyDataText="No record found">
    <Columns>
        <asp:TemplateField HeaderText="">
            <ItemStyle HorizontalAlign="Center" Width="50px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:HyperLink ID="hplEmployeeID" runat="server" Text='<%# Eval("EmployeeID")%>'
                    NavigateUrl='<%# Eval("EmployeeID","~/parliment/dewan/dn/year/2017_sesi1(J).aspx?id={0}")%>'></asp:HyperLink></h3>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label><br />
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>'></asp:Label>
                <asp:Label ID="lblcity" runat="server" Text='<%# Eval("City")%>'></asp:Label><br />
                <hr />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

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

VB.Net

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

Code

C#

PageA

protected void Submit_OnClick(object sender, EventArgs e)
{
    string country = ddlCountry.SelectedItem.Text.Trim();
    string city = ddlCity.SelectedItem.Text.Trim();
    Response.Redirect("~/PageB.aspx?Country=" + country + "&City=" + city);
}

PageB

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["Country"]) && !string.IsNullOrEmpty(Request.QueryString["City"]))
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName+' '+FirstName Name,Country,City FROM Employees WHERE Country=@country AND City=@city ", con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@country", Request.QueryString["Country"]);
                    cmd.Parameters.AddWithValue("@city", Request.QueryString["City"]);
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        this.gvEmployee.DataSource = dt;
                        this.gvEmployee.DataBind();
                    }
                }
            }
        }
    }
}

VB.Net

PageA

Protected Sub Submit_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim country As String = ddlCountry.SelectedItem.Text.Trim()
    Dim city As String = ddlCity.SelectedItem.Text.Trim()
    Response.Redirect("~/PageB.aspx?Country=" & country & "&City=" & city)
End Sub

PageB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        If Not String.IsNullOrEmpty(Request.QueryString("Country")) AndAlso Not String.IsNullOrEmpty(Request.QueryString("City")) Then
            Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
                Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,LastName+' '+FirstName Name,Country,City FROM Employees WHERE Country=@country AND City=@city ", con)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@country", Request.QueryString("Country"))
                    cmd.Parameters.AddWithValue("@city", Request.QueryString("City"))
                    Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        Me.gvEmployee.DataSource = dt
                        Me.gvEmployee.DataBind()
                    End Using
                End Using
            End Using
        End If
    End If
End Sub

Screenshot