方法不在GridView页面更改上触发

时间:2014-08-04 18:16:43

标签: c# asp.net gridview

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

    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";
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。设置新页面的PageIndex并执行DataBind并尝试调用设置按钮文字的方法。

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

答案 1 :(得分:0)

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

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