Solved

SelectedIndexChanged Datagrid dropdown

Posted on 2004-08-11
11
525 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
is autopostback set to true for the dropdownlist?

Regards,

Aeros
0
 
LVL 4

Expert Comment

by:marko020397
Comment Utility
Yes. The AutoPostBask is set to true.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

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

Author Comment

by:skaykay
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Closed, 100 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now