Link to home
Start Free TrialLog in
Avatar of AvinashKN
AvinashKN

asked on

Populating gridview from listbox

Hello all,

I would like to know how to populate a gridview based on user selected option in a listbox.

I have a dropdownlist which is connected to a database and a listbox displaying items when the user selects an option in the dropdownlist. I'm able to populate the dropdownlist and th listbox but how can I populate the gridview based on user selection. I want to display the gridview when the user chooses the option in the listbox and clicks a button called "view".

I'm a beginner, so go easy on me.

I'm using Visual Studio 2005, c# and sql server 2005.  

Here is my code.

populateddl  is the dropdownbox
setzipcode is the listbox


Thanks for the help.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using MITS.BL;
 
public partial class BulkExtractM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (!(IsPostBack))
        {
            PopulateDDL();
        }
 
    }
 
 
    protected void PopulateDDL()
    {
 
        CHHExtractBL_Impl blHHExtract = new CHHExtractBL_Impl();
        DataSet ds = null;
        ds = blHHExtract.GetContractArea("<Root><ContractorID>1</ContractorID></Root>");
 
        DropDownContractorArea.DataSource = ds.Tables[0];
        DropDownContractorArea.DataTextField = "CITYMAP_Description";
        DropDownContractorArea.DataValueField = "CITYMAP_ID";
        DropDownContractorArea.DataBind();
    }
 
    protected void DropDownContractorArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetZipCode(DropDownContractorArea.SelectedValue.Trim());
    }
 
    private void SetZipCode(string strContractArea)
    {
 
        CHHExtractBL_Impl blHHExtract = new CHHExtractBL_Impl();
        DataSet ds = null;
        string inXML = "<Root><ContractArea>" + strContractArea + "</ContractArea>";
        //Change to session
        inXML += "<ContractorID>1</ContractorID></Root>";
            ds = blHHExtract.GetZipCode(inXML);
 
        ds = blHHExtract.GetZipCode(inXML);
        ContractAreaZipCode.DataSource = ds;
 
        ContractAreaZipCode.DataTextField = ds.Tables[0].Columns["ZIP_CODE"].ToString();
        ContractAreaZipCode.DataValueField = ds.Tables[0].Columns["ZIP_CODE"].ToString();
        ContractAreaZipCode.DataBind(); 
 
 
 
    }
 
 
    protected void btnView_Click(object sender, EventArgs e)
    {
 
    }
}

Open in new window

Avatar of dhanushkad
dhanushkad

-> Set the AutoPost back property of the DropDownContractorArea into false
DropDownContractorArea.AutoPostBack = false;


-> delete the  DropDownContractorArea_SelectedIndexChan method.

-> modify code as follows...
 protected void Page_Load(object sender, EventArgs e)
    {
 
        if (!(IsPostBack))
        {
            PopulateDDL();
        }
        else
        {
            SetZipCode(DropDownContractorArea.SelectedValue.Trim());
        }
 
    }
 
 
protected void btnView_Click(object sender, EventArgs e)
{
    SetZipCode(DropDownContractorArea.SelectedValue.Trim());
}

Open in new window

Avatar of AvinashKN

ASKER

I think you misunderstood my question. My dropdownlist and listbox are working fine. My question is when a user clicks on an item in the listbox, I need the gridview to be populated based on the selection.

The listbox contains zipcodes. If the user clicks on one zipcode, I need the gridview to access the database and show the account information of that zipcode.

I hope I'm not confusing everyone. Sorry if I did.
Sorry, I am not i have misunderstood your question properly. I think you need to postback the page and rebind some new data into the grid.

Set the AutoPostBack to true of the dropdown. When you change the seleted item in the dropdown, it will post back the page into the server and reload the page. I think this is what you want? Sorry for interuption..

DropDownContractorArea.AutoPostBack = true;

http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx


 protected void DropDownContractorArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetZipCode(DropDownContractorArea.SelectedValue.Trim());
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dhanushkad
dhanushkad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
*  Sorry, I am not sure that i have understood your question properly.