0

I'm using Visual Studio 2005 and ASP. NET 2.0 with this program. This is written in VB.net

Right now, we've got a Gridview that is embedded and databound into another gridview. The 2nd Gridview is hidden by default and expanded by the user. Within the 2nd gridview are several DropDownLists. The problem is that these DropDownLists are not firing the OnSelectedIndexChanged functon nor AutoPostback whenever I change the selecteditem.

I'm curious as to why it is not firing. Thank you.

Gridview1:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    Dim row As GridViewRow = e.Row
    Dim strSort As String = String.Empty
    Dim strCNRNum As String


    ' Make sure we aren't in header/footer rows
    If row.DataItem Is Nothing Then
        Return
    End If

    ' Find Child GridView control
    Dim gv As GridView = New GridView()
    gv = row.FindControl("GridView2")

    If gv.UniqueID = gvUniqueID Then
        gv.EditIndex = gvEditIndex
        ClientScript.RegisterStartupScript(GetType(Page), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" + e.Row.DataItem("CNR_NUM").ToString() & "','one');</script>")
    End If

    ' Prepare the query for Child GridView by passing 
    ' the Customer ID of the parent row
    'Dim dsTemp As SqlDataSource
    'dsTemp = ChildDataSource(e.Row.DataItem("CNR_NUM").ToString, strSort)
    'gv.DataSource = dsTemp
    'gv.DataBind()

    strCNRNum = e.Row.DataItem("CNR_NUM").ToString()

    Dim dt As DataTable = New DataTable()
    Dim con As SqlConnection = New SqlConnection(ConnectionString)

    Try
        con.Open()
        Dim strSQL As String
        strSQL = "select " & _
                    "CS.SID, " & _
                    "CS.CNR_NUM, " & _
                    "CS.STEP_NUM, " & _
                    "CS.SERVER_ID, " & _
                    "S.SERVER_NM, " & _
                    "CS.DATABASE_ID, " & _
                    "D.DATABASE_NM, " & _
                    "CS.CMD_FILE_NM, " & _
                    "CS.EXECUTE_DTTM, " & _
                    "CS.STEP_TYPE_ID, " & _
                    "ST.STEP_TYPE, " & _
                    "ISNULL(CS.LOG_FILE_NM,'') as LOG_FILE_NM " & _
                 "from " & _
                    "dbo.CNR_STEPS CS INNER JOIN dbo.CNR_SERVERS S on CS.SERVER_ID = S.SERVER_ID " & _
                    "INNER JOIN dbo.CNR_DATABASES D ON CS.DATABASE_ID = D.DATABASE_ID " & _
                    "INNER JOIN dbo.CNR_STEP_TYPES ST ON CS.STEP_TYPE_ID = ST.STEP_TYPE_ID " & _
                 "where " & _
                    "CS.CNR_NUM = '" & strCNRNum & "' " & _
                 "order by " & _
                    "CS.STEP_NUM"

        Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)

        da.Fill(dt)

        If dt.Rows.Count > 0 Then
            gv.DataSource = dt
            gv.DataBind()
        Else
            dt.Rows.Add(dt.NewRow())
            dt.Rows(0)("CNR_NUM") = strCNRNum
            dt.Rows(0)("LOG_FILE_NM") = ""
            gv.DataSource = dt
            gv.DataBind()

            Dim colCount As Integer = gv.Columns.Count
            gv.Rows(0).Cells.Clear()
            gv.Rows(0).Cells.Add(New TableCell())
            gv.Rows(0).Cells(0).ColumnSpan = colCount

            gv.Rows(0).Cells(0).HorizontalAlign = HorizontalAlign.Center
            gv.Rows(0).Cells(0).ForeColor = System.Drawing.Color.Red
            gv.Rows(0).Cells(0).Font.Bold = True

            gv.Rows(0).Cells(0).Text = "No Steps Defined"

        End If
    Catch ex As Exception
        lblMessage.ForeColor = Drawing.Color.Red
        lblMessage.Text = "Error:  " & ex.Message.ToString()
        'ClientScript.RegisterStartupScript(GetType(Page), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>")
    End Try

End Sub

ASP code for the 2nd gridview, which is within the first one:

<asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" Width="100%" Font-Size="Small"  AutoGenerateColumns="false" runat="server" DataKeyNames="CNR_NUM" ShowFooter="true" OnRowEditing = "GridView2_RowEditing"
                                    OnRowCommand = "GridView2_RowCommand"
                                    OnRowDeleting = "GridView2_RowDeleting"
                                    OnRowDeleted = "GridView2_RowDeleted"
                                    OnRowUpdating = "GridView2_RowUpdating"
                                    OnRowUpdated = "GridView2_RowUpdated"
                                    OnRowCancelingEdit = "GridView2_CancelingEdit"
                                    OnRowDatabound="GridView2_RowDataBound"

                                    CssClass="mGrid"  
                                    PagerStyle-CssClass="pgr"  
                                    >
                                    <HeaderStyle Font-Bold="True" ForeColor="White" />
                                    <Columns>
                                            <asp:TemplateField HeaderText="Step Type">
                                            <ItemTemplate><%#Eval("STEP_TYPE")%></ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="ddl_StepTypes" 
                                                DataSourceID="ds_StepTypes" 
                                                SelectedValue='<%# Eval("STEP_TYPE_ID")%>'
                                                DataTextField="STEP_TYPE" 
                                                DataValueField="STEP_TYPE_ID"  
                                                runat="server" 
                                                Width="100px" 
                                                Font-Size="X-Small"
                                                AutoPostBack="true"
                                                OnSelectedIndexChanged = "ddl_StepTypes_SelectedIndexChanged">
                                                </asp:DropDownList>
                                                <label id="lblTest"></label>
                                                <!--<asp:TextBox ID="txtStepType" Text='<%# Eval("SERVER_ID")%>' runat="server"></asp:TextBox>-->
                                            </EditItemTemplate>
                                            <FooterTemplate>
                                                <asp:DropDownList DataSourceID="ds_StepTypes"
                                                  DataTextField="STEP_TYPE" DataValueField="STEP_TYPE_ID"  ID="ddl_StepTypes" runat="server" Width="100px" Font-Size="X-Small"></asp:DropDownList>
                                                <!--<asp:TextBox ID="txtStepType" Text='' runat="server"></asp:TextBox>-->
                                            </FooterTemplate>
                                        </asp:TemplateField>

If any more information is needed, please let me know. Thank you.

Joseph
  • 1

1 Answers1

2

The problem is that if you add a control to a template, the GridView creates multiple copies of that control, one for each data item. When the item is chosen, you need a way to determine which Dropdownlist was clicked and which row it belongs to. The way to resolve this problem is to use an event from the GridView.

The GridView.RowCommand event serves this purpose. Unfortunately initially it is supposed to fire whenever any button is clicked in any template. This process, where a control event in a template is turned into an event in the containing control, is called event bubbling.

May be a workaround described here can help you. Alternatively may be this article could also help (or another one referenced at the very bottom).

Kirill
  • 3,028
  • 1
  • 17
  • 18