从另一个函数调用RowDataBound

时间:2016-02-16 14:49:08

标签: c# asp.net gridview

我有2个Gridviews。第一个网格有一个按钮,当点击它时,它将填充第二个网格,其中的数据基于点击按钮的ID。

然后我在RowDataBound函数中有代码,根据所选行显示网格。但问题是代码在填充函数之前自动运行RowDataBound。所以第二个网格没有显示。

GridView代码:

<asp:GridView  style="width:75%"  
                        ID="gvCVRT" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRT_RowDataBound"  
                        OnSelectedIndexChanged="gridviewParent_SelectedIndexChanged"                           
                        DataKeyField="ID" 
                        DataKeyNames="ChecklistID"
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID"  ></asp:BoundField> 
                            <asp:CommandField ShowSelectButton="True" HeaderText="Select" />
                            <asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField>
                            <asp:BoundField DataField="User" HeaderText="User" ></asp:BoundField>
                            <asp:BoundField DataField="Note" HeaderText="Note" ></asp:BoundField>

                        </Columns>
                    </asp:GridView> 

代码背后:

protected void gvCVRT_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        lookupCVRT work = (lookupCVRT)e.Row.DataItem;
        GridView gv = sender as GridView;

        if (work.ID != null)
        {
            int index = gv.Columns.HeaderIndex("Select");
            if (index > -1)
            {
                e.Row.Cells[index].Attributes.Add("class", "gvCVRTRow");
                e.Row.Cells[index].ToolTip = "Click here to Edit Checklist";
            }
        }
    }
}

选择按钮的代码:

protected void gridviewParent_SelectedIndexChanged(object sender, EventArgs e)
{
    List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), gvCVRT.SelectedDataKey.Value.ToString());
    gvCVRTDetails.DataSource = workDetails;
    gvCVRTDetails.DataBind();
    FireJavascriptCallback("setArgAndPostBack ();");
}

问题是,当我点击网格中的“选择”按钮时,它先运行RowDataBound然后运行gridviewParent_SelectedIndexChanged,但我需要先运行gridviewParent_SelectedIndexChanged。我可以从RowDataBound调用gridviewParent_SelectedIndexChanged函数吗?

Page_Load功能:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetChecklistID = "";
            if (ParentID.HasValue)
            {
                ViewState["ParentID"] = ParentID;

                List<lookupCVRT> work = lookupCVRT.GetCVRTItems(Company.Current.CompanyID, ParentID.ToString());
                ViewState["CVRT"] = work;
                gvCVRT.DataSource = work;
                gvCVRT.DataBind();

            }
        }
        else
        {
            if (ViewState["ParentID"] != null)
            {
                ParentID = (int?)ViewState["ParentID"];
                List<lookupCVRT> work = ViewState["CVRT"] as List<lookupCVRT>;
                gvCVRT.DataSource = work;
                gvCVRT.DataBind();

            }
        }
    }

2 个答案:

答案 0 :(得分:1)

仅在调用OnRowDataBound的{​​{1}}方法时调用DataBind事件。

在您的具体情况下,问题出现在GridView条件的Page_Load分支的else中:

Page.IsPostBack

此代码针对每个回发运行。除非您在代码中的其他位置重置 else { if (ViewState["ParentID"] != null) { ParentID = (int?)ViewState["ParentID"]; List<lookupCVRT> work = ViewState["CVRT"] as List<lookupCVRT>; gvCVRT.DataSource = work; gvCVRT.DataBind(); } } ,否则在每次回发时都会再次绑定GridView ViewState["ParentID"]。这就是调用gvCVRT的原因。完成RowDataBound后,该页面会调用其他事件处理程序,在您的案例Page_Load中。

为了解决这个问题,您需要更改gridviewParent_SelectedIndexChanged处理程序中的代码,以便对回发没有调用Page_Load

DataBind

问题的根本原因是您需要回发请求中的数据,并将这些数据放入// field moved to class level so that you can access this variable instead of a DataRow in gvCVRT private List<lookupCVRT> work; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetChecklistID = ""; if (ParentID.HasValue) { ViewState["ParentID"] = ParentID; work = lookupCVRT.GetCVRTItems(Company.Current.CompanyID, ParentID.ToString()); ViewState["CVRT"] = work; gvCVRT.DataSource = work; gvCVRT.DataBind(); } } else { if (ViewState["ParentID"] != null) { ParentID = (int?)ViewState["ParentID"]; work = ViewState["CVRT"] as List<lookupCVRT>; } } } 而不是重新请求数据。在Web应用程序中,为新请求再次读取数据是很常见的。因此,您可能会考虑是否确实需要将数据放入ViewState,或者是否可以在从数据源回发时请求它们。

将数据放入ViewState会增加传输到客户端的页面大小(基本上你有GridView的HTML,另外你有ViewState中的数据)。因此,在大多数情况下,重新请求它们会更好。

答案 1 :(得分:0)

我不知道为什么你更喜欢使用gridviewParent_SelectedIndexChanged然后使用grdParent_RowDataBound ...我为你创建了一个简单的解决方案......它可以帮助你......

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
        <label>Parent Grid</label>
        <asp:GridView ID="grdParent" runat="server" AutoGenerateColumns="false" 
            DataKeyField="Id" OnRowDataBound="grdParent_RowDataBound" OnRowCommand="grdParent_RowCommand">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />                
                <asp:ButtonField CommandName="Details"  HeaderText="Select" Text="Hello" ButtonType="Link" />
            </Columns>
        </asp:GridView>
    </div>
    <div>
         <label>child Grid</label>
        <asp:GridView ID="grdChild" runat="server" AutoGenerateColumns="false"
            DataKeyNames="ChildId" OnRowDataBound="grdChild_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Name" />
                <asp:BoundField DataField="Roll" />                
                <asp:ImageField HeaderText="Image" />
            </Columns>
        </asp:GridView>
    </div>
    </div>
    </form>
</body>
</html>

<强>代码隐藏

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<ParentClass> pList = new List<ParentClass>()
            {
                new ParentClass{Id=5, Name="V"},
                new ParentClass{Id=6,Name="VI"},
                new ParentClass{Id=7,Name="VII"},
                new ParentClass{Id=8,Name="VIII"},
                new ParentClass{Id=9,Name="IX"},
                new ParentClass{Id=10,Name="X"},
            };

            grdParent.DataSource = pList;
            grdParent.DataBind();
        }
    }

    protected void grdParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem == null || e.Row.RowType != DataControlRowType.DataRow)
        {
            return;
        }

        ParentClass p = e.Row.DataItem as ParentClass;

        var btn = e.Row.Cells[1].Controls[0] as LinkButton;
        btn.CommandArgument = p.Id.ToString();
    }

    protected void grdParent_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int parentId = Convert.ToInt32(e.CommandArgument);

        var releventStudents = GetRepositary().FindAll(i => i.ParentId == parentId);

        grdChild.DataSource = releventStudents;
        grdChild.DataBind();

    }

    protected void grdChild_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem == null || e.Row.RowType != DataControlRowType.DataRow)
        {
            return;
        }

        //lookupCVRT work = (lookupCVRT)e.Row.DataItem;
        //GridView gv = sender as GridView;

        //if (work.ID != null)
        //{
        //    int index = gv.Columns.HeaderIndex("Select");
        //    if (index > -1)
        //    {
        //        e.Row.Cells[index].Attributes.Add("class", "gvCVRTRow");
        //        e.Row.Cells[index].ToolTip = "Click here to Edit Checklist";
        //    }
        //}         
    }

    private List<ChildClass> GetRepositary()
    {
        List<ChildClass> allChild = new List<ChildClass>();
        Random r = new Random();

        for (int i = 0; i < 50; i++)
        {
            ChildClass c = new ChildClass
            {
                ChildId = i,
                ParentId = r.Next(5, 10),
                Name = "Child Name " + i,
                Roll = i
            };

            allChild.Add(c);
        }

        return allChild;
    }
}

public class ParentClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ChildClass
{
    public int ParentId { get; set; }
    public int ChildId { get; set; }
    public int Roll { get; set; }
    public string Name { get; set; }
}