UpdatePanel 内的 DropdownList OnSelectedIndexChanged

DropdownList OnSelectedIndexChanged inside UpdatePanel

我的程序的更新面板中有一个 asp 下拉列表。 OnSelectedIndexChanged 事件的功能工作正常,但问题是下拉列表在 post 返回后不可见。以下是我的代码。

                        <asp:UpdatePanel ID="updatePanel1" runat="server">
                         <ContentTemplate>
                         <div class="row cancel-paddings cancel-margins">
                                <div class="field">
                                 <asp:DropDownList ID="ddlDropdownSub1" EnableViewState="true" Visible="true"  runat="server" class="dropdown" OnSelectedIndexChanged="OnSelectedIndex_dropdownSub1" AutoPostBack="true"></asp:DropDownList>
                                </div>
                            </div>
                        <div class="card-content">
                            <div class="listview is-selectable custom-scroll-one" id="task-listview" data-tmpl="task-tmpl" data-dataset="demoTasks" tabindex="-1" role="listbox" aria-label="Tasks">
                                <ul role="presentation">
                                <asp:GridView ID="gvCaseCards" runat="server" AutoGenerateColumns="false">
                                <Columns>
                                    <asp:TemplateField >
                                        <ItemTemplate>
                                            <li  aria-posinset="1" aria-setsize="12" tabindex="0" class="centered-block" aria-selected="true">
                                            <asp:Label CssClass="listview-subheading" ID="lblCaseIdCaseCard" runat="server" Text=''></asp:Label><br />
                                            <asp:Label CssClass="listview-heading wrap-text" ID="lblDescCaseCard" runat="server" Text=''></asp:Label><br />
                                            <asp:Label CssClass="listview-subheading" ID="lblDueCaseCard" runat="server" Text=''></asp:Label><br /><br />
                                            </li>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                                </ul>
                            </div>
                        </div>
                  </ContentTemplate>
            </asp:UpdatePanel>

在我的 OnSelectedIndex_dropdownSub1 函数中,我可以动态获取 gridview 的值。我的页面加载方法如下。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.BindDataToDropDown1() 'fill data to dropdown 
        Me.BindDataToGridview() 'bind data to grid view
    End If
End Sub

以下是OnSelectedIndex_dropdownSub1事件

Protected Sub OnSelectedIndex_dropdownSub1(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDropdownSub1.SelectedIndexChanged
    Me.BindDataToGridview()
End Sub

以下是我的 binddatatodropdown 函数

 Public Function BindDataToDropDown1()
    Dim ds As New DataSet
    Dim alUtil As New ALUtility
    Dim connString As String = AppSettings("conString")


    Using cnn As New SqlConnection(connString)
        cnn.Open()
        Using dad As New SqlDataAdapter("SELECT * FROM product", cnn)
            dad.Fill(ds)
        End Using
        cnn.Close()
    End Using
    ddlDropdownSub1.DataSource = ds
    ddlDropdownSub1.DataTextField = "product_name"
    ddlDropdownSub1.DataValueField = "product_id"
    ddlDropdownSub1.DataBind()

End Function

下面是 BindDataToGrid 函数,

Public Function BindDataToGridview()
 Dim ds As New DataSet
    Dim alUtil As New ALUtility
    Dim connString As String = AppSettings("conString")

    Dim product_id As Integer = Convert.ToInt32(ddlDropdownSub1.SelectedValue)
    Using cnn As New SqlConnection(connString)
        cnn.Open()
        Using dad As New SqlDataAdapter(" Select * from Product Where product_id = " + product_id.ToString(), cnn)
            dad.Fill(ds)
        End Using
        cnn.Close()
    End Using
    gvCaseCards.DataSource = ds
    gvCaseCards.DataBind()

End Function

除 post返回后下拉菜单消失外,所有功能都正常工作。

谁能帮我解决这个问题。

据我所知,更新面板应该有几处更改。一般来说,我真的很讨厌更新面板,并且认为您应该始终尝试使用自己的 AJAX 代码。

首先,我认为您可以将 Gridview 包裹在 <ContentTemplate> 标记中,因为那只是正在更新的控件。

然后你需要添加一个触发器,比如这样:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="ddlDropdownSub1" EventName="SelectedIndexChanged" />
</Triggers>

这超出了 <ContentTemplate> 标签。我认为您还应该在 UpdatePanel 控件上添加 UpdateMode="Conditional"