Gridview被调用两次

时间:2013-10-16 05:55:13

标签: c# asp.net gridview

我有一个调用两个子网格的父网格。但由于某种不明原因,他们被解雇了两次。我似乎无法弄清楚这段代码的问题。谁能指导我?

后端代码(C#):

protected void grdGrpPrntRpt_RowDataBound(object sender,
                                      GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox litGrp = (TextBox)e.Row.FindControl("litGrp");
            litGrp.Text =
              DataBinder.Eval(e.Row.DataItem, "GroupName").ToString();
            ViewState["GroupName"] = litGrp.Text;
            DataSet ds = (DataSet)ViewState["ds"];
            DataView dv = ds.Tables[3].DefaultView;
            dv.RowFilter = "GroupName='" + litGrp.Text + "'";
            DataTable dt = dv.ToTable();
            GridView gvinner = (GridView)e.Row.FindControl("grdInnerMain");
            gvinner.DataSource = dt;
            gvinner.DataBind();
        }
    }
    catch (Exception Ex)
    {
        //report error
    }
}


protected void grdInnerMain_RowDataBound(object sender,
                                      GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox litUsr = (TextBox)e.Row.FindControl("litUsr");
            litUsr.Text = 
                DataBinder.Eval(e.Row.DataItem, "FirstName").ToString();
            // string username=
            DataSet ds = (DataSet)ViewState["ds"];
            DataView dv = ds.Tables[3].DefaultView;
            dv.RowFilter = "userName='" +
           DataBinder.Eval(e.Row.DataItem, "UserName").ToString().Replace("'",
               "''") + "' and GroupName='" + ViewState["GroupName"].ToString()
             + "'";
            DataTable dt = dv.ToTable();
            totalPages = 0;
            totalCost = 0;
            GridView gvinner2 = (GridView)e.Row.FindControl("innerGrid");
            gvinner2.DataSource = dt;
            gvinner2.DataBind();
        }
    }

    catch (Exception Ex)
    {
        //report error
    }

}
protected void innerGrid_RowDataBound(object sender,
                              GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataView dv = 
                ((DataSet)ViewState["dspaper"]).Tables[0].DefaultView;
            dv.RowFilter = "paperid=" +
               Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
                                         "paperid").ToString());
            DataTable dt = dv.ToTable();
            e.Row.Cells[0].Text = dt.Rows[0]["PaperName"].ToString();
            //  e.Row.Cells[2].Text = DataBinder.Eval(e.Row.DataItem, 
     "TotalPagesPrinted").ToString() == "0" ? DataBinder.Eval(e.Row.DataItem, 
       "TotalPagesSent").ToString() : DataBinder.Eval(e.Row.DataItem, 
       "TotalPagesPrinted").ToString();
            e.Row.Cells[2].Text = DataBinder.Eval(e.Row.DataItem, 
       "TotalPage").ToString() == "0" ? DataBinder.Eval(e.Row.DataItem, 
       "Pages_Sent").ToString() : DataBinder.Eval(e.Row.DataItem,
               "TotalPage").ToString();
            // e.Row.Cells[3].Text = 
        Convert.ToDouble(e.Row.Cells[3].Text.ToString()).ToString("0.00");
            totalPages += Convert.ToInt32(e.Row.Cells[2].Text.ToString());
            totalCost += Convert.ToDouble(e.Row.Cells[3].Text.ToString());
            e.Row.Cells[1].Text = DataBinder.Eval(e.Row.DataItem,
                   "Color").ToString() == "0" ? "B & W" : "Color";
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = totalPages.ToString();
            e.Row.Cells[3].Text = totalCost.ToString("0.00");
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = 
                  CResourcesManager.GetString("grdHeaderMediaReport");
            e.Row.Cells[1].Text =
                  CResourcesManager.GetString("grdHeaderColorReport");
            e.Row.Cells[2].Text =
                  CResourcesManager.GetString("grdHeaderPagesReport"); ;
            e.Row.Cells[3].Text = 
                CResourcesManager.GetString("grdHeaderTotalCostReport");
        }
    }
    catch
    { }
}

前端代码:

<asp:GridView Width="100%" ID="grdGrpPrntRpt" runat="server" 
               AutoGenerateColumns="False"
                    CellPadding="0" CellSpacing="2" BorderWidth="0px" 
             ShowHeader="false" RowStyle-HorizontalAlign="Left"
                    OnRowDataBound="grdGrpPrntRpt_RowDataBound">

                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <table id="tblInnerHeader" runat="server"
                                 width="100%" cellspacing="0" cellpadding="0">
                                    <tr bgcolor="gray" valign="top">
                                        <td width="20%" height="30px">
                                            &nbsp;
                                            <asp:TextBox ID="litGrp"
                           ForeColor="White" BackColor="Gray" CssClass="LabelText2"
                                                runat="server" BorderStyle="None" 
                                                   BorderWidth="0"></asp:TextBox>
                                        </td>
                                        <td width="2%">
                                        </td>
                                        <td width="78%" align="left">
                                            <asp:ImageButton ID="imgMainShowHide" 
                                    runat="server" ImageUrl="~/Images/plus.jpg"
                                                OnClick="imgMainShowHide_Click" />
                                        </td>
                                    </tr>
                                </table>
                                <table id="tblFirstInnerGrid" runat="server" 
                             visible="false" width="100%" cellspacing="2"
                                    cellpadding="0">
                                    <tr>
                                        <td align="center">
                                            <asp:GridView Width="100%" 
               ID="grdInnerMain" runat="server" AutoGenerateColumns="False"
                                                CellPadding="0" CellSpacing="2" 
              BorderWidth="0px" ShowHeader="false" RowStyle-HorizontalAlign="Left"

                        OnRowDataBound="grdInnerMain_RowDataBound">
                                                <Columns>
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <table id="tblHeader"
                  runat="server" width="100%" cellspacing="0" cellpadding="0">
                                                                <tr bgcolor="Wheat" 
                                                               valign="top">
                                                                    <td width="20%" 
                                                              height="25px">
                                                                        &nbsp;
                                                                        <asp:TextBox 
              ID="litUsr" ForeColor="Blue" BackColor="Wheat" CssClass="LabelText2"

              runat="server" BorderStyle="None" BorderWidth="0"></asp:TextBox>
                                                                    </td>
                                                                    <td width="2%">
                                                                    </td>
                                                                    <td width="78%" 
                                                                      align="left">
                                                                        <asp:ImageButton ID="imgShowHide" runat="server" ImageUrl="~/Images/plus.jpg"
                                               OnClick="imgShowHide_Click" />
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                            <table id="tblInnerGrid" 
                          runat="server" visible="false" width="100%" cellspacing="2"
                                                                cellpadding="0">
                                                                <tr>
                                                                   <td align="center">
                                                                        <asp:GridView 
           ID="innerGrid" runat="server" AutoGenerateColumns="False" CellPadding="0"

       CellSpacing="2" HeaderStyle-CssClass="GridHeaderNoSorting" BorderWidth="0px"

          ShowFooter="true" RowStyle-HorizontalAlign="Left" Width="98%" 
                    OnRowDataBound="innerGrid_RowDataBound">
                                                                            <Columns>
                                                                                <asp:TemplateField>
                                                                                    <ItemTemplate>
                                                                                        <%-- <asp:Label ID="lblMedia" runat="server"></asp:Label>--%>
                                                                                    </ItemTemplate>
                                                                                </asp:TemplateField>
                                                                                <%--<asp:BoundField DataField="Color" HeaderText="Color/ B & W" /> --%>
                                                                                <asp:TemplateField HeaderText="Color/ B & W">
                                                                                    <ItemTemplate>
                                                                                        <%--   <asp:Label ID="lblColor" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Color").ToString()=="0" ? "B & W" :"Color" %>'></asp:Label>--%>
                                                                                    </ItemTemplate>
                                                                                </asp:TemplateField>
                                                                                <%--<asp:BoundField DataField="TotalPagesPrinted" ItemStyle-HorizontalAlign="Center" />--%>
                                                                                <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
                                                                                    FooterStyle-HorizontalAlign="Center">
                                                                                    <ItemTemplate>
                                                                                        <%--<asp:Label ID="lblPagesPrinted" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Color").ToString()=="0" ? "B & W" :"Color" %>'></asp:Label>--%>
                                                                                    </ItemTemplate>
                                                                                </asp:TemplateField>
                                                                                <asp:BoundField DataField="Total_Cost" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
                                                                                    FooterStyle-HorizontalAlign="Center" />
                                                                            </Columns>
                                                                            <HeaderStyle CssClass="GridHeaderNoSorting" />
                                                                            <AlternatingRowStyle CssClass="GridAlternateRowStyle" />
                                                                            <RowStyle CssClass="GridRowStyle" />
                                                                            <FooterStyle CssClass="GridFooter" HorizontalAlign="Left" />
                                                                        </asp:GridView>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                                <AlternatingRowStyle CssClass="GridAlternateRowStyle" />
                                                <RowStyle CssClass="GridRowStyle" />
                                            </asp:GridView>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <AlternatingRowStyle CssClass="GridAlternateRowStyle" />
                    <RowStyle CssClass="GridRowStyle" />
                </asp:GridView>

额外信息

出于某种原因,事件grdGrpPrntRpt_RowDataBound被调用的次数与下一个网格中的数据一样多。它应该在接下来的两个网格中的整个数据被绑定后立即终止,但它再次调用它并再次重复整个过程。

7 个答案:

答案 0 :(得分:2)

有可能由于AutoEventWireUp,在您的代码中可能是这样。 如果是,请将其设为假。感谢

答案 1 :(得分:1)

检查你是否将GridView绑定到其他一些事件上,可能是Page_Load或......如果你在Page_Load上绑定GridView,请按以下方式执行:

if (!Page.IsPostBack)
{

//bind your GridView
}

答案 2 :(得分:1)

问题应该只是意味着你以某种方式调用grdGrpPrntRpt.DataBind()两次,

你的嵌套gridview数据绑定的实现似乎是正确的所以问题

可能是对主网格进行数据绑定的事件。

尝试评论主网格DataBind的每一行并将其放在Page_Load

事件,你可能会遇到另一个问题,但你会更接近解决方案。

答案 3 :(得分:0)

始终检查所有绑定网格或调用gridbinding函数的位置。如: -

你可能先在Page_Load上调用它来绑定第一次,然后你可以在imgMainShowHide_Click事件中调用它来将它绑定到新值。因此绑定网格两次,所以确保仅在Page_Load上的!Ispostback时绑定并启用gridview viewstate以在回发期间保持其状态。

答案 4 :(得分:0)

我认为给定的后端代码和前端代码没有任何问题。您可以查看this link以供参考。

答案 5 :(得分:0)

网格的RowDataBound触发网格中存在的行数的次数。

因此请确保您的父网格是否有多条记录..

答案 6 :(得分:-1)

请确保你的HTML代码gridview

 OnRowCreated="gv_RowCreated"
如果您使用的是vb.net,则会删除

。面对同样的问题。