DropDownList没有回发

时间:2011-06-29 19:29:52

标签: asp.net vb.net drop-down-menu postback

我正在使用Visual Studio 2005和ASP。 NET 2.0与此程序。这是用VB.net编写的

现在,我们已经有一个Gridview嵌入并数据绑定到另一个gridview。第二个Gridview默认隐藏,并由用户扩展。在第二个gridview中有几个DropDownLists。问题是,每当我更改selecteditem时,这些DropDownLists都不会触发OnSelectedIndexChanged功能,也不会触发AutoPostback。

我很好奇它为什么不开火。谢谢。

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

第二个gridview的ASP代码,它位于第一个:

<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>

如果需要更多信息,请告知我们。谢谢。

1 个答案:

答案 0 :(得分:2)

问题在于,如果向模板添加控件,GridView会为该控件创建多个副本,每个副本对应一个数据项。选择项目后,您需要一种方法来确定单击了哪个Dropdownlist以及它属于哪一行。 解决此问题的方法是使用GridView中的事件。

GridView.RowCommand事件用于此目的。不幸的是,最初只要在任何模板中单击任何按钮,就会触发它。此过程将模板中的控件事件转换为包含控件中的事件,称为事件冒泡。

可能是here所述的解决方法可以帮助您。或者可以this文章也可以提供帮助(或者在最底层引用另一篇文章)。