Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

Gridview Select one dropdownlist to filter the 2nd dropdownlist

I have a gridview with two dropdownlists in it.
When a user selects the first dropdown I would like the 2nd dropdown list to only display associated records.
I am using a separate linqdatasource for each dropdown.
How do I get the selectedvalue from the first dropdown and apply to the 2nd?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think, informaniac is almost complete.

Enable 'AutoPostBack' property of the DropDownList 'd1' to True.

Then in SelectedIndexChanged event write code to identity the two dropdownlist controls. Then as usual bind the second dropdownlist based on the SelectedItem.Value of the first dropdownlist control

Raj
Im sure there's more than one way, but I put together a quick sample of one way to go about it. Just real simple for demo purposes:

1) Setup gridview with 2 dropdownlists
  a) 1st dropdown affects second using autopostback and onselectedindexchanged event
2) In onselectedindexchanged event get the 2 dropdowns, change dropdown items of ddl2 based on choice from ddl1.

I realize you have datasources to bind to etc, but the premise is the same just have to flush it out to work with your details. The code runs stand alone if you want to check it out. I kept it really simple for readablility.
****ASPX PAGE****
    <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="false">
		<Columns>
			<asp:TemplateField>
				<ItemTemplate>
					<span><%#Eval("text")%></span>
					<asp:DropDownList ID="Ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Ddl1_SelectedIndexChanged">
						<asp:ListItem Text="Option1" Value="0" />
						<asp:ListItem Text="Option2" Value="1" />
					</asp:dropdownlist>
					<asp:DropDownList ID="Ddl2" runat="server" Enabled="false">
						<asp:ListItem Text="AutoUpdated" Value="0" />
					</asp:DropDownList>
				</ItemTemplate>
			</asp:TemplateField>	
		</Columns>
    </asp:GridView>

****"ASPX.VB PAGE****
Partial Class GridDynamicDDLs
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim _GridList As New ListItemCollection

        If Not IsPostBack Then
            'Just need something to bind to
            Dim _BindingList As New ListItemCollection
            _BindingList.Add(New ListItem("row1", "0"))
            _BindingList.Add(New ListItem("row2", "1"))
            _BindingList.Add(New ListItem("row3", "2"))
            Gv1.DataSource = _BindingList
            Gv1.DataBind()
        End If

    End Sub

    Protected Sub Ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddl1 As DropDownList = sender
        Dim ddl2 As DropDownList = sender.parent.findcontrol("ddl2")
        ddl2.Items.Clear()
        ddl2.Enabled = True

        'You would assign your datasources here.
        Select Case ddl1.SelectedValue
            Case 0
                ddl2.Items.Add(New ListItem("X1", "0"))
                ddl2.Items.Add(New ListItem("X2", "1"))
                ddl2.Items.Add(New ListItem("X3", "2"))
            Case 1
                ddl2.Items.Add(New ListItem("Y1", "0"))
                ddl2.Items.Add(New ListItem("Y2", "1"))
                ddl2.Items.Add(New ListItem("Y3", "2"))
        End Select
    End Sub
End Class

Open in new window

Ah dude got there in front of me :)
Avatar of Sheritlw

ASKER

I agree.  I need to also find the 2nd dropdownlist.
I have added the code provided by informaniac, but it does not show how to get the 2nd dropdown.

I provided an example below.

Thanks
Protected Sub ddProdType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'To get the row
        Dim gvRow As GridViewRow = DirectCast(ddl.Parent.Parent, GridViewRow)
        If ddl.SelectedItem.Value > -1 Then
            Dim i As Integer = ddl.SelectedValue
            ''I need the other control to change it's source
        End If
    End Sub

Open in new window

His is very similar to mine. To find the second dropdown you would simple use findcontrol on the gvRow you found.

dim ddl2 as dropdownlist = gvRow.findcontrol("idof2nddropdown")

Or if this doesn't work then you can change it a little:
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'To get the row
        Dim ddlParent As DataControlFieldCell = DirectCast(ddl.Parent, DataControlFieldCell)
        If ddl.SelectedItem.Value > -1 Then
            Dim i As Integer = ddl.SelectedValue
            ''I need the other control to change it's source
            Dim ddl2 As DropDownList = ddlParent.FindControl("IDOfDDL2")
        End If
BTW I believe both work.
I have attached what I now have.  
I am getting an error when trying to bind my linq select to my linqdatasource.
The error is weird
Could not find a property or field called 'SELECT [t0].[LUProdSellUseID], [t0].[ProductName], [t0].[UserID], [t0].[ProductTypeID], [t0].[ProdSell]
FROM [dbo].[vProductNameForSale] AS [t0]
WHERE ((CONVERT(NVarChar(MAX),[t0].[UserID])) = @p0) AND ([t0].[ProductTypeID] = @p1)
' on the data context type 'SalonDataClassesDataContext' of LinqDataSource 'LnqProductNames'.


How do I re-bind my linq select to the linqdatasource?

Thanks


Protected Sub ddProdType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'To get the row
        Dim gvRow As GridViewRow = DirectCast(ddl.Parent.Parent, GridViewRow)
        Dim ddlParent As DataControlFieldCell = DirectCast(ddl.Parent, DataControlFieldCell)
        If ddl.SelectedItem.Value > -1 Then
            Dim i As Integer = ddl.SelectedValue
            ''I need the other control to change it's source
            Dim ddl2 As DropDownList = ddlParent.FindControl("ddProductSale")
            If Not ddl2 Is Nothing Then
                Dim dc As New SalonDataClassesDataContext
                Dim u As New SalonUtilities
                Dim sUser As String = u.CheckSessionAndReturnUser
                Dim q = (From a In dc.vProductNameForSales Where a.UserID.ToString = sUser _
                         And a.ProductTypeID = i Select a)
                If Not q Is Nothing Then
                    'Assign q to my linqdatasource
??????
                    Me.LnqProductNames.
                End If
            End If
        End If

    End Sub

Open in new window

I got nuthin...

I thought u wanted to add/change the datasource for the 2nd ddl?

ddl2.datasource = somedatabindableobject (maybe your linq src)
ddl2.databind

I haven't played with Linq in a long while, never was very good with it. I know it does some "unconventional" stuff but the nature of your error makes me think the issue is in your line 14 and the way you are concatenating sUser but I'm just punting!
Hi,

Use linqDAtasource and use selectedvalue of Dropdown as paramerter.
I use a linqdatasource in the aspx page, but when the column gets filtered, I need to know how to rebind the linqdatasource.

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried to find a programming selection for  Linq, but with no luck.  It is supposed to be in the same area as asp.net and vb.net.
Trying to use filtering within the gridview is take to much time to figure out.
I am going to take that out and try to do it later.
Thank you for your help.