Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Populating gridview from listbox

Posted on 2008-11-05
5
Medium Priority
?
964 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:AvinashKN
  • 4
5 Comments
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22888821
-> 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

0
 

Author Comment

by:AvinashKN
ID: 22888919
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.
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22889291
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

0
 
LVL 3

Accepted Solution

by:
dhanushkad earned 1500 total points
ID: 22889325
Sorry, I am not sure that 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 property of the dropdown to true, then 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());
    }
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22889340
*  Sorry, I am not sure that i have understood your question properly.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question