【问题标题】:OnSelectedIndexChanged event occurs on one page and doesn't on another oneOnSelectedIndexChanged 事件发生在一个页面上,而不发生在另一个页面上
【发布时间】:2013-05-20 06:11:44
【问题描述】:

我想这个问题很难。我有两个功能相同的页面。第一个是针对人的。第二个是作为副本创建的,然后针对产品进行了一些修改。

我在两个页面上都有 DropDownLists,两个 DDL 都有 OnSelectedIndexChanged 事件和 AutoPostBack 选项启用。两个页面都有代码隐藏过程 RefreshPage,它使用 id 参数重新加载页面。这个 id 参数是下拉列表中实际选择的值。

所以,按照设计:

1) 我更改了 DDL 的选定项目,它执行 AutoPostBack 并触发 OnSelectedIndexChanged 事件。

2) 此事件运行RefreshPage 过程,该过程将DDL 的当前选定值添加到QueryString 并用它重新加载页面。

3) 然后这个 ID 取自 QueryString 并设置为 DDL。查看代码。

RefreshPagePeople.aspx 页面上成功运行。但是 Products.aspx 上的类似程序根本不会运行。我将断点添加到两个过程的同一位置 - Page.Response.Redirect 方法的行。在第一页它停止并显示参数 paramID 的正确值,但第二页永远不会到达断点。 OnSelectedIndexChanged 事件被简单地跳过。

两个页面都已EnableViewState 禁用。我不使用 javascript 或 AJAX。我不使用 PreRender 方法。

这是我的代码。

People.aspx:

<asp:DropDownList ID="ddJGroup" runat="server" OnSelectedIndexChanged="RefreshPage" AutoPostBack="True" CssClass="DropDown" Font-Bold="True"/>

People.aspx.cs:

protected void RefreshPage(object s, EventArgs e)
{
    paramID = ddJGroup.SelectedValue;
    Page.Response.Redirect("People.aspx?id=" + paramID);
}

产品.aspx:

<asp:DropDownList ID="ddCType" runat="server" OnSelectedIndexChanged="RefreshPage" AutoPostBack="True" CssClass="class_DropDown" Font-Bold="True"/>

在这两个页面中,我从 QueryString 中获取 id 参数,并在 Page_Load 过程中使用它设置 DropDownLists 的选定值。见下文。

Products.aspx.cs:

...
if ((Request.QueryString["id"] != null) && (Request.QueryString["id"] != ""))
    paramID = Convert.ToInt32(Request.QueryString["id"]);
...
ddCType.DataSource = sqlReader;
ddCType.DataValueField = "c_type_id";
ddCType.DataTextField = "type_name";
ddCType.DataBind();
ddCType.SelectedIndex = ddCType.Items.IndexOf(ddCType.Items.FindByValue(Convert.ToString(paramID)));

此代码运行良好。但是在 Products.aspx 上,参数 paramID 永远不会改变。它应该在 RefreshPage 过程中更改,但该过程永远不会运行,也永远不会更改 QueryString["id"] 参数。我的意思是,这个页面永远不会从 RefreshPage 重新加载,它总是做简单的 PostBack。

在 RefreshPage 过程中:

protected void RefreshPage(object o, EventArgs e)
{
    paramID = ddCType.SelectedValue;
    Page.Response.Redirect("Products.aspx?id=" + paramID);
}

如果此过程运行,页面将使用新的 id 参数重新加载,并且下拉列表将获取此新索引。

【问题讨论】:

  • 没有足够的信息来说明为什么没有触发事件。请同时上传 Products.aspx 和 Products.aspx.cs。
  • 在选择 DropDownList 后,是否所有事件都在 Products.aspx 中相应触发?换句话说, Page_Load 应该首先触发;然后是 DropDownList 等等。在包括 Page_Load 在内的所有事件中设置断点并进行调试。
  • 还要确保AutoEventWireup="true"CodeBehind="Products.aspx.cs"Inherits="PROJECTNAMESPACE.Products"Products.aspx
  • 能否启用视图状态(通过删除EnableViewState="false")并再次测试?
  • 我已经更新了主题,提供了更多细节。

标签: asp.net postback procedure autopostback selectedindexchanged


【解决方案1】:

我会说 70-90% 的 DropDownList_SelectedIndexChanged 事件没有被触发,因为您页面中某些数据绑定到控件的错误,

检查以查看页面中的绑定并检查您是否在每次页面加载时都没有绑定它检查:

if(!IsPostBack)
{
mydropdownlist.DataSource=yourDataSource;
mydropdownlist.DataBind();


}

所以它不会在每次回发事件发生时都绑定,因此 IndexChanged 不会被触发

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多