Solved

SelectedIndexChanged Datagrid dropdown

Posted on 2004-08-11
11
570 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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