Solved

Gridview Select one dropdownlist to filter the 2nd dropdownlist

Posted on 2010-11-16
13
1,092 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Sheritlw
13 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 250 total points
ID: 34152054
Try this code
<asp:TemplateField>

                <ItemTemplate>

                    <asp:DropDownList ID="d1" runat=server 

        onselectedindexchanged="d1_SelectedIndexChanged"></asp:DropDownList>

                </ItemTemplate>

            </asp:TemplateField>



In code behind

protected void d1_SelectedIndexChanged(object sender, EventArgs e)

        {

            DropDownList ddl = ((DropDownList)sender);

            //To get the row

            GridViewRow gvRow = ((GridViewRow)ddl.Parent.Parent);

        }

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34152172
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
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 34152197
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

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 34152198
Ah dude got there in front of me :)
0
 

Author Comment

by:Sheritlw
ID: 34152207
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

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 34152266
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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 20

Expert Comment

by:ddayx10
ID: 34152297
BTW I believe both work.
0
 

Author Comment

by:Sheritlw
ID: 34152398
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

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 34152651
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!
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34153606
Hi,

Use linqDAtasource and use selectedvalue of Dropdown as paramerter.
0
 

Author Comment

by:Sheritlw
ID: 34160740
I use a linqdatasource in the aspx page, but when the column gets filtered, I need to know how to rebind the linqdatasource.

Thanks
0
 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 250 total points
ID: 34161165
If the ddl has a datasource, then you are changing a property (filtering) of that datasource, you should re-bind the ddl by just calling ddl.databind()... Whatever changes you made to the datasource will be reflected.

The issue seems to be more about how to filter/utilize the linq filtring properly. It is really hard to know how to proceed or even setup a sample when you are working with custom objects that we cannot see.

I think this issue is beginning to suffer from a bit of scope creep. You might get better play by re-packaging the issue as a linq question at this stage of the game. People who have insight are going to see the original issue and never look at the current problem because of this.
0
 

Author Comment

by:Sheritlw
ID: 34161468
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.






0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

23 Experts available now in Live!

Get 1:1 Help Now