【问题标题】:Dropdownlist in repeater update panel losing value after postback转发器更新面板中的下拉列表在回发后失去价值
【发布时间】:2020-08-12 11:52:25
【问题描述】:

亲爱的,我有以下下拉列表,它位于中继器内的更新面板内。

<asp:Repeater OnItemDataBound="rprProperties_ItemDataBound"  ID="rprProperties" runat="server">
    <ItemTemplate>
        <div class="mb-2">
            <asp:Label style="width : 100px;float:left;" ID="Label1" runat="server" Text='<%# Container.DataItem("name") %>'></asp:Label>
            <asp:Label  style="width : 100px;float:left;" ID="propID" runat="server" Text='<%# Container.DataItem("id") %>' Visible="false"></asp:Label>

            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ClientIDMode="AutoID" AutoPostBack="true" OnSelectedIndexChanged="ddlProperty_SelectedIndexChanged" style="width:100px" CssClass="filter-dropdown bg-light"  DataValueField="id" DataTextField="name"   ID='ddlProperty' runat="server"></asp:DropDownList>
                </ContentTemplate>
            </asp:UpdatePanel>
                                                                
        </div>
    </ItemTemplate>
</asp:Repeater>

我正在转发器的 ItemDataBound 事件中使用此代码填充 ddl。

        Dim propID As Label = TryCast(e.Item.FindControl("propID"), Label)

    Dim ddl As DropDownList = TryCast(e.Item.FindControl("ddlProperty"), DropDownList)


        Dim varDbconn As New SqlConnection(ConfigurationManager.ConnectionStrings("shopCS").ToString)
        Dim varDbcomm As SqlCommand
        Dim varDbRead As SqlDataReader

        varDbconn.Open()

        varDbcomm = New SqlCommand("exec spShowItemPropValues @property,@id,@lang ", varDbconn)

        varDbcomm.Parameters.AddWithValue("@property", SqlDbType.Int).Value = propID.Text
        varDbcomm.Parameters.AddWithValue("@id", SqlDbType.Int).Value = Request.QueryString("id")
        varDbcomm.Parameters.AddWithValue("@lang", SqlDbType.NVarChar).Value = Session("lang")

        varDbRead = varDbcomm.ExecuteReader()

        Dim varDt As New DataTable

        varDt.Load(varDbRead)


        ddl.DataSource = varDt
        ddl.DataBind()

        varDbcomm.Dispose()
        varDbRead.Close()

        varDbconn.Close()

当我选择一个值时,下拉列表会重置为下拉列表中的第一项,而不是保留所选值。

我想保留那个价值。

谢谢。

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    您忘记添加 AsyncPostBackTrigger。放在&lt;/UpdatePanel&gt;之前

    <asp:Repeater OnItemDataBound="rprProperties_ItemDataBound"  ID="rprProperties" runat="server">
         <ItemTemplate>
    
              <div class="mb-2">
              <asp:Label style="width : 100px;float:left;" ID="Label1" runat="server" Text='<%# Container.DataItem("name") %>'></asp:Label>
                <asp:Label  style="width : 100px;float:left;" ID="propID" runat="server" Text='<%# Container.DataItem("id") %>' Visible="false"></asp:Label>
    
                 <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                  <ContentTemplate>
                   <asp:DropDownList ClientIDMode="AutoID" AutoPostBack="true" OnSelectedIndexChanged="ddlProperty_SelectedIndexChanged" style="width:100px" CssClass="filter-dropdown bg-light"  DataValueField="id" DataTextField="name"   ID='ddlProperty' runat="server">
                                                                        </asp:DropDownList>
                  </ContentTemplate>
                <Triggers>
                     <asp:AsyncPostBackTrigger ControlID="ddlProperty" EventName="SelectedIndexChanged" />
                 </Triggers>
             </asp:UpdatePanel>
                                                                
            </div>
    
        </ItemTemplate>
    </asp:Repeater>
    

    【讨论】:

    • 我收到一个错误,找不到名为 ddlProperty 的控件
    • @ahmadnoori,ddlProperty 是您的下拉列表的 ID。并确保您已生成 SelectedIndexChanged 事件后端。
    • 我明白。但它找不到它,因为它在中继器内
    • @ahmadnoori 尝试将更新面板放在中继器之外。所以流程就像 UpdatePanel 然后是 Repeater。
    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多