中继器更新面板中的Dropdownlist在回发后丢失值



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

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

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

我想保持这种价值。

谢谢。

您忘记添加AsyncPostBackTrigger。放在</UpdatePanel>之前

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

最新更新