locked
Putting 2 DropDown Lists in a templated Gridview cell, and make the 2nd rebind when the first changes. RRS feed

  • Question

  • User1279376247 posted

    I have a Gridview and one of the templated cells contains 2 dropdown lists, DDL1 and DDL2.

    They are each bound to SQL queries, SQL1 and SQL2.

    I want the DDL2 to update when the user makes a change to the DDL1.  That is, I have a Parameter for the SQL2, which is based

    on DDL1.SelectedValue.

    I haven't been able to get it to work.   I have done it lots of times, but never inside the rows of a GridView.

    I'm not sure if I can use a sql ControlParameter and if so, how to specify the Control, when it's inside a GridViewRow.

    Or, if I  use a regular sql parameter, and set it's defaultvalue in the VB code, when the DDL1 changes. 

    But when I do this, I get:  Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    What's the easiest way to do this?

    Tuesday, May 10, 2016 7:40 PM

Answers

  • User-1547578656 posted

    Ok I have never tried to get a dropdownlist in a GV to autofire and update another dropdownlist.  I was curious if your could put an updatepanel inside of a templatefield and fire off a script using selectedindexchanged and I was surprised this worked on my first go.  You'll need to play around with capturing row indexes to make sure you're updating the correct dropdownlist depending on which one was selected but this totally worked for me:

    <%@ page language="VB" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <script runat="server">
        
        Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            
        End Sub
        
        Sub IndexChanged()
            For Each r As GridViewRow In GVTest.Rows
                Dim vDDL1 As DropDownList = r.Cells(1).FindControl("DDL1")
                Dim vDDL2 As DropDownList = r.Cells(1).FindControl("DDL2")
                vDDL2.SelectedValue = vDDL1.SelectedValue
            Next
        End Sub
        
        
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Test</title>
    </head>
    <body>
        <form runat="server" id="form1">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ASPNetConnection %>" 
            SelectCommand="Select 'Testing' as TestCol"></asp:SqlDataSource>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:GridView runat="server" ID="GVTest" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField HeaderText="TestingUpdate">
                    <ItemTemplate>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="DDL1" EventName="SelectedIndexChanged" />
                            </Triggers>
                            <ContentTemplate>
                                <asp:DropDownList runat="server" ID="DDL1" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true">
                                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                                </asp:DropDownList>
                                <asp:DropDownList runat="server" ID="DDL2">
                                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                                </asp:DropDownList>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2016 8:41 PM
  • User36583972 posted

    Hi DaveBF,

    From your description, I have made a sample on my side. The following code in my project for your reference.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" >
                    <Columns>
                        <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
                            <ItemTemplate>
                                <%--        <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />--%>
                                <asp:Label ID="lablename" runat="server">  <%# Eval("Name") %></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="DropDownList1" ItemStyle-Width="150">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack ="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="DropDownList2" ItemStyle-Width="150">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack ="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    ASPX.VB:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not Me.IsPostBack Then
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
                dt.Rows.Add(1, "John Hammond", "United States")
                dt.Rows.Add(2, "Mudassar Khan", "India")
                dt.Rows.Add(3, "Suzanne Mathews", "France")
                dt.Rows.Add(4, "Robert Schidner", "Russia")
                Session("tableserch") = dt
                GridView1.DataSource = dt
                GridView1.DataBind()
    
    
    
            End If
    
        End Sub
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                Dim DropDownList1 As DropDownList = TryCast(e.Row.FindControl("DropDownList1"), DropDownList)
                If (Session("tableserch") IsNot Nothing) Then
                    Dim tb As DataTable = TryCast(Session("tableserch"), DataTable)
                    DropDownList1.DataSource = tb
                    DropDownList1.DataTextField = "Name"
                    DropDownList1.DataValueField = "Id"
                    DropDownList1.DataBind()
                End If
            End If
        End Sub
    
        Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
    
            Dim DropDownList1 As DropDownList = TryCast(sender, DropDownList)
            Dim row As GridViewRow = TryCast(DropDownList1.NamingContainer, GridViewRow)
            Dim DropDownList2 As DropDownList = TryCast(row.FindControl("DropDownList2"), DropDownList)
            Dim selectvalue = DropDownList1.SelectedValue.ToString()
    
            If (Session("tableserch") IsNot Nothing) Then
                Dim tb As DataTable = TryCast(Session("tableserch"), DataTable)
                Dim tbtmp As DataTable = tb.Select("Id='" + selectvalue + "'").CopyToDataTable()
                DropDownList2.DataSource = tbtmp
                DropDownList2.DataTextField = "Name"
                DropDownList2.DataValueField = "Id"
                DropDownList2.DataBind()
            End If
    
        End Sub
    
        Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim DropDownList2 As DropDownList = TryCast(sender, DropDownList)
            Response.Write(DropDownList2.SelectedValue.ToString())
        End Sub
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 6:08 AM

All replies

  • User-1547578656 posted

    Ok I have never tried to get a dropdownlist in a GV to autofire and update another dropdownlist.  I was curious if your could put an updatepanel inside of a templatefield and fire off a script using selectedindexchanged and I was surprised this worked on my first go.  You'll need to play around with capturing row indexes to make sure you're updating the correct dropdownlist depending on which one was selected but this totally worked for me:

    <%@ page language="VB" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <script runat="server">
        
        Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            
        End Sub
        
        Sub IndexChanged()
            For Each r As GridViewRow In GVTest.Rows
                Dim vDDL1 As DropDownList = r.Cells(1).FindControl("DDL1")
                Dim vDDL2 As DropDownList = r.Cells(1).FindControl("DDL2")
                vDDL2.SelectedValue = vDDL1.SelectedValue
            Next
        End Sub
        
        
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Test</title>
    </head>
    <body>
        <form runat="server" id="form1">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ASPNetConnection %>" 
            SelectCommand="Select 'Testing' as TestCol"></asp:SqlDataSource>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:GridView runat="server" ID="GVTest" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField HeaderText="TestingUpdate">
                    <ItemTemplate>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="DDL1" EventName="SelectedIndexChanged" />
                            </Triggers>
                            <ContentTemplate>
                                <asp:DropDownList runat="server" ID="DDL1" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true">
                                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                                </asp:DropDownList>
                                <asp:DropDownList runat="server" ID="DDL2">
                                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                                </asp:DropDownList>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2016 8:41 PM
  • User-1716253493 posted
        Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) 
        Handles DropDownList1.SelectedIndexChanged
            Dim DDL1 As DropDownList = CType(sender, DropDownList) 'sender is the dropdown
            Dim row As GridViewRow = CType(DDL1.NamingContainer, GridViewRow) 'use DDL1 namingcontainer to get current row
            Dim DDL2 As DropDownList = CType(row.FindControl("DropDownList2"), DropDownList) 'get DDL2 from current row
        End Sub

    Wednesday, May 11, 2016 3:05 AM
  • User36583972 posted

    Hi DaveBF,

    From your description, I have made a sample on my side. The following code in my project for your reference.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" >
                    <Columns>
                        <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
                            <ItemTemplate>
                                <%--        <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />--%>
                                <asp:Label ID="lablename" runat="server">  <%# Eval("Name") %></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="DropDownList1" ItemStyle-Width="150">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack ="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="DropDownList2" ItemStyle-Width="150">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack ="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    ASPX.VB:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not Me.IsPostBack Then
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
                dt.Rows.Add(1, "John Hammond", "United States")
                dt.Rows.Add(2, "Mudassar Khan", "India")
                dt.Rows.Add(3, "Suzanne Mathews", "France")
                dt.Rows.Add(4, "Robert Schidner", "Russia")
                Session("tableserch") = dt
                GridView1.DataSource = dt
                GridView1.DataBind()
    
    
    
            End If
    
        End Sub
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                Dim DropDownList1 As DropDownList = TryCast(e.Row.FindControl("DropDownList1"), DropDownList)
                If (Session("tableserch") IsNot Nothing) Then
                    Dim tb As DataTable = TryCast(Session("tableserch"), DataTable)
                    DropDownList1.DataSource = tb
                    DropDownList1.DataTextField = "Name"
                    DropDownList1.DataValueField = "Id"
                    DropDownList1.DataBind()
                End If
            End If
        End Sub
    
        Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
    
            Dim DropDownList1 As DropDownList = TryCast(sender, DropDownList)
            Dim row As GridViewRow = TryCast(DropDownList1.NamingContainer, GridViewRow)
            Dim DropDownList2 As DropDownList = TryCast(row.FindControl("DropDownList2"), DropDownList)
            Dim selectvalue = DropDownList1.SelectedValue.ToString()
    
            If (Session("tableserch") IsNot Nothing) Then
                Dim tb As DataTable = TryCast(Session("tableserch"), DataTable)
                Dim tbtmp As DataTable = tb.Select("Id='" + selectvalue + "'").CopyToDataTable()
                DropDownList2.DataSource = tbtmp
                DropDownList2.DataTextField = "Name"
                DropDownList2.DataValueField = "Id"
                DropDownList2.DataBind()
            End If
    
        End Sub
    
        Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim DropDownList2 As DropDownList = TryCast(sender, DropDownList)
            Response.Write(DropDownList2.SelectedValue.ToString())
        End Sub
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 6:08 AM