【发布时间】:2014-10-14 17:34:34
【问题描述】:
我有一个表单,我在其中放置一个转发器控件并将其绑定到 pageLoad,它按预期绑定。
我有一个dropDownList,当我从中选择一个值时,我想清除中继器 dataSource 并在代码隐藏的dropDown_SelectedIndexChanged 事件处理程序中重新绑定它。
我将转发器数据源设置为空,然后用新数据源重新绑定它,它不会反映更改,它会显示第一个绑定值。
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
//Populate the drop down
PopulateLocationDropdown();
if (Request.QueryString["locationID"] != null)
{
int locationID=Request.QueryString["locationID"];
//Clear repeater
rpt_Displaytheater.DataSource = null;
rpt_Displaytheater.DataBind();
//Rebind repeater
rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
rpt_Displaytheater.DataBind();
}
}
当下拉selectionChanged:
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
int locationID=ddlLocation.SelectedValue.ToInt();
//Clear repeater
rpt_Displaytheater.DataSource = null;
rpt_Displaytheater.DataBind();
//Rebind repeater
rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
rpt_Displaytheater.DataBind();
}
编辑
这是我的下拉菜单:
<asp:DropDownList ID="ddlLocation" DataTextField="LocationName" DataValueField="Record_Id" runat="server" CssClass="textbox_230"
OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
这是我的中继器:
<asp:Repeater ID="rpt_Displaytheater" runat="server"
OnItemCommand="rpt_Displaytheater_ItemCommand">
<ItemTemplate>
<div class="TheaterListing" style="background: #FFF;">
<div class="TheaterName">
<div class="Theaterhead">
<asp:Label ID="lblTheaterID" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "Record_Id") %>'></asp:Label>
<asp:LinkButton ID="lnkbtnTheaterName" runat="server" Style="color: #000; font-weight: bold" Text='<%# DataBinder.Eval(Container.DataItem, "Theatre_Name") %>'></asp:LinkButton>
</div>
</div>
<div class="showlist">
<%-- show time repeater-Movies Tab --%>
<asp:Repeater ID="rpt_showtime" runat="server">
<ItemTemplate>
<ul style="padding-left: 10px; margin: -18px 0 0 163px;">
<li class="fl" style="font-weight: bold; padding-left: 8px;">
<asp:LinkButton ID="lnk_showtime" CommandName="Showtime" runat="server" CssClass="txtstyle1"
Text='<%# DataBinder.Eval(Container.DataItem, "Time_From") %>'></asp:LinkButton>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
<%-- show time repeater-Movies Tab --%>
<%--<asp:LinkButton ID="LinkButton1" runat="server" style="color: #000;font-weight:bold" Text="10.00 AM"></asp:LinkButton>--%>
</div>
</div>
<br clear="all" />
</ItemTemplate>
</asp:Repeater>
我的问题是,当我在下拉选择更改时将转发器控件的数据源重置为新数据源,但更改未反映在转发器上,它只显示页面加载时的第一个有界值我的代码有什么问题
【问题讨论】:
-
当所选索引改变时,您是否调试并测试代码实际上是呼叫您的方法?并且您需要为您的 ddlLocation 控件设置
AutoPostBack="true" -
没有什么比在 ddlLocation_SelectedIndexChanged 处理程序中设置断点更简单的了。你会看到 locationID 的值是否在变化。据我所知,您的代码没有任何问题。您不必将数据源设置为空。确保打开下拉列表的自动回发。
-
这是您的实际代码吗?
int locationID=Request.QueryString["locationID"];不会编译,ToInt()是什么? -
@Damith 实际上我的代码有点长,这就是为什么要抽象它
-
您在 aspx 标记中使用更新面板吗?
标签: c# asp.net asprepeater