简体   繁体   English

从另一个函数调用RowDataBound

[英]call RowDataBound from another function

I have a 2 Gridviews. 我有2个Gridviews。 The first grid has a button that when clicked it will populate a second grid with the data based on the id of the button clicked. 第一个网格有一个按钮,当单击它时,它将填充第二个网格,其中的数据基于单击按钮的ID。

I then have code in the RowDataBound function to show the grid based on the row selected. 然后,我在RowDataBound函数中有代码,以根据所选行显示网格。 But the problem is the code is automatically running the RowDataBound before the populate function. 但问题是代码在填充函数之前自动运行RowDataBound。 So the second grid isn't displaying. 所以第二个网格没有显示。

Code for GridView: 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> 

Code behind: 代码背后:

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

Code for select button: 选择按钮的代码:

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

So the problem is when I click on the Select button in the grid it runs the RowDataBound first then the gridviewParent_SelectedIndexChanged but I need to run gridviewParent_SelectedIndexChanged first. 所以问题是当我点击网格中的Select按钮时它先运行RowDataBound然后是gridviewParent_SelectedIndexChanged但我需要先运行gridviewParent_SelectedIndexChanged Can I call the RowDataBound function from gridviewParent_SelectedIndexChanged ? 我可以从gridviewParent_SelectedIndexChanged调用RowDataBound函数吗?

Page_Load function: 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();

            }
        }
    }

The OnRowDataBound event is only called if the DataBind method for the GridView has been called. 仅在调用GridViewDataBind方法时才调用OnRowDataBound事件。

In your specific case, the problem is in Page_Load in the else branch of the Page.IsPostBack condition: 在您的特定情况下,问题出现在Page.IsPostBack条件的else分支中的Page_Load中:

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

    }
}

This code is run for each postback. 此代码针对每个回发运行。 Unless you reset ViewState["ParentID"] somewhere else in your code, on every postback you bind the GridView gvCVRT again. 除非您在代码中的其他位置重置ViewState["ParentID"] ,否则在每次回发时gvCVRT再次绑定GridView gvCVRT This is the reason that RowDataBound is called. 这就是调用RowDataBound的原因。 After finishing Page_Load , the page calls the additional event handlers, in your case gridviewParent_SelectedIndexChanged . 完成Page_Load ,页面将调用其他事件处理程序,在您的情况下为gridviewParent_SelectedIndexChanged

In order to solve this problem, you need to change the code in your Page_Load handler so that there are no calls to DataBind for a postback: 为了解决这个问题,您需要更改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>;
        }
    }
}

The root cause of your problem is that you need the data in a postback request and that you put these into ViewState["CVRT"] instead of requesting the data anew. 问题的根本原因是您需要回发请求中的数据,并将这些数据放入ViewState["CVRT"]而不是重新请求数据。 In web applications it is pretty common the read the data again for a new request. 在Web应用程序中,为新请求再次读取数据是很常见的。 So you might think about whether you really need to put the data into ViewState or whether you can request them upon a postback from the data source. 因此,您可能会考虑是否确实需要将数据放入ViewState,或者是否可以在从数据源回发时请求它们。

Putting the data into ViewState increases the size of the page that is transferred to the client (basically you have the HTML for the GridView and in addition you have the data in ViewState). 将数据放入ViewState会增加传输到客户端的页面大小(基本上你有GridView的HTML,另外你有ViewState中的数据)。 So requesting them anew would be the better way in most cases. 因此,在大多数情况下,重新请求它们将是更好的方法。

I don't know why you preferred to use gridviewParent_SelectedIndexChanged then grdParent_RowDataBound ... i have created a simple solution for you .. it could help you .. 我不知道为什么你更喜欢使用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>

Codebehind 代码隐藏

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM