Solved

SelectedIndexChanged Datagrid dropdown

Posted on 2004-08-11
11
543 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

17 Experts available now in Live!

Get 1:1 Help Now