Quantcast
Channel: dynamic Filter ( Using multiple drop down list)
Viewing all articles
Browse latest Browse all 20

Re: dynamic Filter ( Using multiple drop down list)

$
0
0

This should give you the idea for using a listbox instead of a dropdownlist

Markup based on yours

<table id="tblCampaignList" border="0" cellpadding="3" class="auto-style2"><tr><td style="background-color: #5D7B9D; font-size: medium; font-weight: bold; color: white; width: 100px">
        Campaigns:</td><td><asp:DropDownList ID="ddlCampaigns" runat="server" TabIndex="6"
        AutoPostBack="True" onselectedindexchanged="ddlCampaigns_SelectedIndexChanged"></asp:DropDownList></td><td style="background-color: #5D7B9D; font-size: medium; font-weight: bold; color: white; width: 100px">
        Segment: </td><td><asp:ListBox ID="lbSegments" runat="server" SelectionMode="Multiple"></asp:ListBox></td></tr></table><asp:Button ID="btnRetrieve" runat="server" Text="Retrieve" OnClick="btnRetrieve_Click" /><asp:Label ID="lblParameters" runat="server" Text="--"></asp:Label>

Code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindCampaigns();
        ddlCampaigns.SelectedIndex = 0;
        BindSegments();
    }
}

protected void ddlCampaigns_SelectedIndexChanged(object sender, EventArgs e)
{
    BindSegments();
}

/// <summary>
/// populate campaign drowdonw
/// </summary>
private void BindCampaigns()
{
    ddlCampaigns.DataSource = getCampaings();
    ddlCampaigns.DataValueField = "ID";
    ddlCampaigns.DataTextField = "Campaign";
    ddlCampaigns.DataBind();
}

/// <summary>
/// populate segments listbox
/// </summary>
private void BindSegments()
{
    int campaignid = Convert.ToInt32(ddlCampaigns.Items[ddlCampaigns.SelectedIndex].Value);
    lbSegments.DataSource = getSegments(campaignid);
    lbSegments.DataTextField = "Segment";
    lbSegments.DataValueField = "ID";
    lbSegments.DataBind();
}

/// <summary>
/// get campaigns from DB
/// </summary>
/// <returns></returns>
private DataTable getCampaings()
{
    DataTable tbl = new DataTable();
    tbl.Columns.Add("ID", typeof(int));
    tbl.Columns.Add("Campaign", typeof(string));

    tbl.Rows.Add(new object[] { 1, "First campaign" });
    tbl.Rows.Add(new object[] { 2, "Second campaign" });

    return tbl;
}

/// <summary>
/// get segments from DB for given campaign (foreign key relationhip)
/// </summary>
/// <param name="iCampaignID"></param>
/// <returns></returns>
private DataTable getSegments(int iCampaignID)
{
    DataTable tbl = new DataTable();
    tbl.Columns.Add("ID", typeof(int));
    tbl.Columns.Add("CampaignID", typeof(int));
    tbl.Columns.Add("Segment", typeof(string));

    switch (iCampaignID)
    {
        case 1:
            tbl.Rows.Add(new object[] { 1, 1, "Segment A" });
            break;
        case 2:
            tbl.Rows.Add(new object[] { 2, 2, "Segment B" });
            tbl.Rows.Add(new object[] { 3, 2, "Segment C" });
            tbl.Rows.Add(new object[] { 6, 2, "Segment F" });
            break;
        case 3:
            tbl.Rows.Add(new object[] { 2, 3, "Segment B" });
            tbl.Rows.Add(new object[] { 4, 3, "Segment D" });
            tbl.Rows.Add(new object[] { 5, 3, "Segment E" });
            break;
    }

    return tbl;
}

/// <summary>
/// retrieve filtered campaign info records from DB
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnRetrieve_Click(object sender, EventArgs e)
{
    string c = "@campaign = ";
    string s = "@segment = ";

    // add selected campaign
    c += ddlCampaigns.Items[ddlCampaigns.SelectedIndex].Value;

    // get selected segments
    int[] selectedsegments = lbSegments.GetSelectedIndices();
    // add selected segments to s
    for (int cnt = 0; cnt < selectedsegments.Length; cnt++)
    {
        s += lbSegments.Items[selectedsegments[cnt]].Text + ",";
    }

    // display
    lblParameters.Text = c + " | " + s.Trim(',');

}

The important part for you is what happens in the btnRetrieve event handler; you cam ignore the rest, but it provides a complete demo.

Good luck.


Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>