Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SelectedIndexChanged Datagrid dropdown

Posted on 2004-08-11
11
564 Views
Last Modified: 2011-09-20
Hi Experts,

I've few TemplateColumns to define the columns of a table within a datagrid. The first templatecolumn has a dropdownlist in edittemplate which I bind it to some datasource for the list. What I need is this.. I've few other dropdown lists and textboxes which needs to be populated based on the selection I make in this dropdownlist. For which I need to capture the SelectedIndexChanged of this dropdown list and based on the value bind the second list.

I would appreciate any help, as I'm quite new to ASP.NET.

TIA & Cheers,
kaykay!
0
Comment
Question by:skaykay
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11771979
Heres an example:

#Region " Tier1 DropDownList "
    Private Sub Tier1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tier1.Load
        If Page.IsPostBack Then Return

        Dim cnn As New OleDb.OleDbConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New OleDb.OleDbCommand

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT Tier1ID, Name FROM Tier1 ORDER BY Name"
        cmd.Connection = cnn

        Tier1.DataTextField = "Name"
        Tier1.DataValueField = "Tier1ID"
        cnn.Open()
        Tier1.DataSource = cmd.ExecuteReader()
        Tier1.DataBind()
        cnn.Close()

        cmd.Dispose()
        cnn.Dispose()

        If Tier1.Items.Count = 0 Then Response.Redirect("ManageTier1.aspx", True)
        Tier1.Items.Insert(0, New ListItem("Select one...", ""))
    End Sub
    Private Sub Tier1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tier1.SelectedIndexChanged
        If Tier1.SelectedValue = "" Then Return

        Dim cnn As New OleDb.OleDbConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New OleDb.OleDbCommand

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT Tier2ID, Name FROM Tier2 WHERE " & _
            "Tier1ID = " & Tier1.SelectedValue & " ORDER BY Name"
        cmd.Connection = cnn

        Tier2.DataTextField = "Name"
        Tier2.DataValueField = "Tier2ID"
        cnn.Open()
        Tier2.DataSource = cmd.ExecuteReader()
        Tier2.DataBind()
        cnn.Close()

        cmd.Dispose()
        cnn.Dispose()
        Tier2.Items.Insert(0, New ListItem("-- None --", "NULL"))

        Tier3.Items.Clear()
        Tier3.Items.Insert(0, New ListItem("-- None --", "NULL"))

        LoadProductData()
    End Sub
#End Region

#Region " Tier2 DropDownList "
    Private Sub Tier2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tier2.SelectedIndexChanged
        If Tier2.SelectedValue = "" Then Return

        Dim cnn As New OleDb.OleDbConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New OleDb.OleDbCommand

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT Tier3ID, Name FROM Tier3 WHERE " & _
            "Tier2ID = " & Tier2.SelectedValue & " ORDER BY Name"
        cmd.Connection = cnn

        Tier3.DataTextField = "Name"
        Tier3.DataValueField = "Tier3ID"
        cnn.Open()
        Tier3.DataSource = cmd.ExecuteReader()
        Tier3.DataBind()
        cnn.Close()

        cmd.Dispose()
        cnn.Dispose()
        Tier3.Items.Insert(0, New ListItem("-- None --", "NULL"))
        LoadProductData()
    End Sub
#End Region

Tier2's dropdownlist is based on the slected value of Tier1's.  This shows you how to hand the selected index changed.  If you have any problems let me know.

Regards,

Aeros
0
 
LVL 4

Author Comment

by:skaykay
ID: 11772497
Well, I've the dropdowns within the datagrid and not in the web form directly. (which I have within a templatecolumn). This is where I get the problem.

How will I use it in this case?
0
 
LVL 4

Expert Comment

by:marko020397
ID: 11772667
I have had exactly the same problem.

The SelectedIndexChange won't get called until you call DataBind for your datagrid. That caused the most problems for me.

The next problem is that I filled DropdownLists in OnDataBind event in template class. In OnDataBind event I couldn't get the value of the first DropDownList which determines what to load in other DropDownLists. Fortunately I had only two different options. I filled two listboxes in the dependant template column in OnDataBind both with Visible flag set to false. Then after datagrid DataBind I showed only the DropDownList which needed to be shown. I didn't find the way to fill DropDownList with the appropriate data according to the first DropDownList.

This is a little confusing. Ask me if you have any problems or I have been not clear enough.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 11772718
is autopostback set to true for the dropdownlist?

Regards,

Aeros
0
 
LVL 4

Expert Comment

by:marko020397
ID: 11772727
Yes. The AutoPostBask is set to true.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11772745
do you have something like
If Page.ispostback Then
rebinddatahere
End If
in your page load event?
0
 
LVL 4

Author Comment

by:skaykay
ID: 11772788
Well, it seems its getting more complicated that I ever thought. My situation here is this. With AutoPostBack set, I'm able to catch the SelectedIndexChange event. But I'm not able to get the instance of the second dropdownlist control to populate. (in the CS file). ALong with the column property too I'm not able to do it..

But well, the workaround that you gave is not viable to my situation here as I've many combinations.

kaykay!
0
 
LVL 4

Author Comment

by:skaykay
ID: 11773083
guys, I guess I've got the solution for this...

http://aspnet.4guysfromrolla.com/articles/051904-1.2.aspx

check this link and also the complete tutorial with live demos and samples in datagrid. I guess this should solve our problem...

hope it helps

cheers,
kaykay!
0
 
LVL 4

Expert Comment

by:marko020397
ID: 11778020
I hope it will help you. I found this article too but it didn't work because I created my columns dinamicaly with ITemplate classes for ItemTemplate, EditItemTemplate,...
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 11811783
Closed, 100 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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