【问题标题】:Method Not Firing on GridView Page Change方法在 GridView 页面更改时未触发
【发布时间】:2014-08-04 18:16:42
【问题描述】:

我有一个分配按钮文本的方法,该按钮位于网格视图中的项目模板内。该方法在 Page_PreRender 或 Page_Load 中触发得很好,但是一旦有人通过内置的分页链接更改页面,结果页面就不会将文本分配给按钮。我在 PageIndexChanged 和 PageIndexChanging 事件中都调用了该方法,但我仍然得到空白按钮。是否有一个事件我可以放入它会导致它在 gridview 页面索引更改时运行?

    protected void populateActivationText()
    {
        foreach (GridViewRow row in allItemGrid.Rows)
        {
            System.Web.UI.WebControls.Button activationButtonAll = row.FindControl("activationButtonAll") as System.Web.UI.WebControls.Button;
            System.Web.UI.WebControls.CheckBox activeCheckAll = row.Cells[7].Controls[0] as System.Web.UI.WebControls.CheckBox;
            if (activeCheckAll.Checked)
            {
                activationButtonAll.Text = "Deactivate";
            }
            else
            {
                activationButtonAll.Text = "Activate";
            }
        }
        foreach (GridViewRow row in searchGrid.Rows)
        {
            System.Web.UI.WebControls.Button activationButtonSearch = row.FindControl("activationButtonSearch") as System.Web.UI.WebControls.Button;
            System.Web.UI.WebControls.CheckBox activeCheckSearch = row.Cells[7].Controls[0] as System.Web.UI.WebControls.CheckBox;
            if (activeCheckSearch.Checked)
            {
                activationButtonSearch.Text = "Deactivate";
            }
            else
            {
                activationButtonSearch.Text = "Activate";
            }
        }
    }

【问题讨论】:

  • 你能把PageIndexChangedPageIndexChanging的代码贴出来吗?
  • protected void allItemGrid_PageIndexChanged(object sender, EventArgs e) { populateActivationText(); }
  • 让我们看看您的 Page_PreRender 和 Page_Load 是什么样子的。请注意,Page_PreRender 在 PageIndexChanged 和 PageIndexChanging 之后触发。让我们看看您的 Page_PreRender 代码
  • EVENT (arguments){populateActivatonText();} --- 我通过在 gridview 的 DataBound 事件中调用 populateActivationText() 来完成这项工作,我不知道这是否是正确的方法但是它按预期工作。

标签: c# asp.net gridview


【解决方案1】:

试试这样的。为新页面设置PageIndex 并执行DataBind 并尝试调用设置按钮文本的方法..

 protected void allItemGrid_PageIndexChanged(object sender, EventArgs e)
 { 
      allItemGrid.PageIndex = e.NewPageIndex;
      allItemGrid.DataBind();
      populateActivationText();
 }

【讨论】:

  • 不幸的是,这不起作用,我曾尝试在以前的尝试中重新绑定 gridview,但没有任何结果。 NewPageIndex 是 PageIndexChanging 事件的属性。感谢您的帮助,如果我没有关于如何在 C# 中执行此操作的任何建议,我想我可以用 .js 编写它,看起来这应该不是一个谜。
【解决方案2】:

我通过在 gridview 的 DataBound 事件中调用 populateActivationText 来完成这项工作。我认为这不是最好的方法,但它现在按预期工作。

protected void allItemGrid_DataBound(object sender, EventArgs e)
        {
            populateActivationText();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2016-02-07
    • 1970-01-01
    相关资源
    最近更新 更多