0

我在页面加载中有一个 gridview 我使用 checkfunction 更改了按钮的一些图像但是,当我更改页面的索引时。页面加载不要在程序中调用我尝试了 GridView1_PageIndexChanging, GridView1_SelectedIndexChanged,GridView1_PageIndexChanged 事件

在页面加载的代码中,我在事件中复制了它,但它不起作用

        DBConnection db = new DBConnection();


        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow gvrow = GridView1.Rows[i];
            ImageButton ib = (ImageButton)gvrow.Controls[1].Controls[0];

            string feedUrl = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value;



            bool res = db.CheckAddedFeeds(feedUrl, User_Name);
            if (res)
            {
                ib.ImageUrl = "~/images/delete.png";
            }

        }
4

1 回答 1

0

首先,您需要创建一个用于绑定 gridview 的函数,如下所示

private void BindGridView()
{ 
        // place your bind grid code here..
    DBConnection db = new DBConnection();

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        GridViewRow gvrow = GridView1.Rows[i];
        ImageButton ib = (ImageButton)gvrow.Controls[1].Controls[0];

        string feedUrl = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value;



        bool res = db.CheckAddedFeeds(feedUrl, User_Name);
        if (res)
        {
            ib.ImageUrl = "~/images/delete.png";
        }

    }


}

在页面加载后面的顶点代码中调用此函数

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindGridView();
    }

}

在后面的代码中处理分页事件,如下所示

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGridView();
}

希望对你有帮助...

于 2012-04-09T10:46:56.400 回答