【问题标题】:Nesting Repeater in Gridview: Hiding gridview row and repeater header if data is not there in repeater在 Gridview 中嵌套中继器:如果中继器中没有数据,则隐藏 gridview 行和中继器标题
【发布时间】:2011-02-10 11:59:15
【问题描述】:

我在 Gridview 中嵌套了一个中继器控件。现在它显示每个案例的网格视图行和转发器标题(转发器控件中的特定网格视图行是否存在数据)。当特定 gridview 行没有数据时,我想隐藏 gridview 行和转发器控件标题。

谢谢, 我通过过滤结果数据表在代码级别处理了这种情况。

现在我面临的另一个问题是: 我已允许在 gridview 上进行分页,即 pagesize 3。 当页面加载时它工作正常,但是当我转到第 2 页时,它会产生以下错误: 指数超出范围。必须是非负数且小于集合的大小。参数名称:索引

下面是在网格的rowdatabound事件上填充网格、分页和填充repeater的代码。

私有 void FillGrid() { clsCustomFunctions objShort = new clsCustomFunctions(); grd1.DataSource = objShort.GetAll();
}

protected void grd1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    try
    {
        FillGrid();
        grd1.PageIndex = e.NewPageIndex;
        grd1.DataBind();
    }
    catch (Exception ex)
    {
        lblMsg.Text = ex.Message;
    }
}

protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    clsCustomFunctions objShort = new clsCustomFunctions();        
    if (e.Row.RowType == DataControlRowType.DataRow)
    {            
        ((HtmlTable)e.Row.FindControl("gridTable")).BgColor = "#006699";
        Repeater rpt = (Repeater)e.Row.FindControl("rpt1");
        rpt.DataSource = objShort.GetResult(Convert.ToInt32(grd1.DataKeys[e.Row.DataItemIndex].Value));
        rpt.DataBind();
    }
}

grd1.DataKeys[e.Row.DataItemIndex].Value 行抛出错误。如何处理此问题以仅传递第 2 页的值。

【问题讨论】:

    标签: c# asp.net gridview repeater


    【解决方案1】:

    尝试处理网格的 OnRowDataBound 事件。这会给你一个 GridViewRowEventArgs 对象(比如 e)。

    然后您可以查看 e.Row.DataItem 以获取它所绑定的数据,以检查您是否需要隐藏标题。

    您可以使用 e.Row.FindControl("RepeaterName") 让中继器随心所欲地进行操作。

    【讨论】:

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