Solved

SelectedIndexChanged Datagrid dropdown

Posted on 2004-08-11
11
554 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now