【问题标题】:DropdownList OnSelectedIndexChanged inside UpdatePanelUpdatePanel 内的 DropdownList OnSelectedIndexChanged
【发布时间】:2018-04-18 07:27:51
【问题描述】:

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

                        <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

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

谁能帮我解决这个问题。

【问题讨论】:

  • 你能显示 OnSelectedIndex_dropdownSub1() 事件吗?你的意思是回发后下拉菜单是空的,还是完全消失?
  • 它完全消失了。我只是在 OnSelectedIndex_dropdownSub1() 事件中绑定 gridview。
  • 好的,你能显示 BindDataToGridView() 子吗?在某些时候,您必须将下拉控件的可见属性切换为“false”。
  • 好的,我会添加。此行为仅在 UpdatePanel 中出现。没有更新面板,它不会消失。

标签: asp.net vb.net webforms updatepanel


【解决方案1】:

据我所知,更新面板应该有一些变化。我真的很讨厌更新面板,并且认为您应该始终尝试使用自己的 AJAX 代码。

首先,我认为您可以将Gridview 包装在&lt;ContentTemplate&gt; 标签中,因为这只是正在更新的控件。

那你需要在里面添加一个触发器,比如这样:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-20
    • 2013-09-13
    • 2016-09-05
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多