Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个函数调用RowDataBound_C#_Asp.net_Gridview - Fatal编程技术网

C# 从另一个函数调用RowDataBound

C# 从另一个函数调用RowDataBound,c#,asp.net,gridview,C#,Asp.net,Gridview,我有两个网格视图。第一个网格有一个按钮,单击该按钮时,它将根据所单击按钮的id使用数据填充第二个网格 然后,我在RowDataBound函数中使用代码来显示基于所选行的网格。但问题是,代码在执行populate函数之前会自动运行RowDataBound。所以第二个网格没有显示 GridView的代码: <asp:GridView style="width:75%" ID="gvCVRT"

我有两个网格视图。第一个网格有一个按钮,单击该按钮时,它将根据所单击按钮的id使用数据填充第二个网格

然后,我在RowDataBound函数中使用代码来显示基于所选行的网格。但问题是,代码在执行populate函数之前会自动运行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 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 ();");
}

只有在调用了
GridView
DataBind
方法时,才会调用
OnRowDataBound
事件

在您的特定情况下,问题在于
页面的
else
分支中的
Page\u Load
。IsPostBack
条件:

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

    }
}
问题的根本原因是您需要回发请求中的数据,并将这些数据放入
ViewState[“CVRT”]
,而不是重新请求数据。在web应用程序中,为新请求再次读取数据是非常常见的。因此,您可能会考虑是否确实需要将数据放入ViewState,或者是否可以在从数据源回发时请求它们


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

我不知道为什么您更喜欢使用
gridviewParent\u SelectedIndexChanged
然后
grdParent\u 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; }
}
public部分类Default2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
List pList=新列表()
{
新父类{Id=5,Name=“V”},
新父类{Id=6,Name=“VI”},
新父类{Id=7,Name=“VII”},
新父类{Id=8,Name=“VIII”},
新父类{Id=9,Name=“IX”},
新父类{Id=10,Name=“X”},
};
grdParent.DataSource=pList;
grdParent.DataBind();
}
}
受保护的void grdParent_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.DataItem==null | | e.Row.RowType!=DataControlRowType.DataRow)
{
返回;
}
ParentClass p=e.Row.DataItem作为父类;
var btn=e.Row.Cells[1]。将[0]控制为链接按钮;
btn.CommandArgument=p.Id.ToString();
}
受保护的void grdParent_row命令(对象发送方,GridViewCommandEventArgs e)
{
int parentId=Convert.ToInt32(e.CommandArgument);
var releventStudents=GetRepositary().FindAll(i=>i.ParentId==ParentId);
grdChild.DataSource=相关学生;
grdChild.DataBind();
}
受保护的void grdChild_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.DataItem==null | | e.Row.RowType!=DataControlRowType.DataRow)
{
返回;
}
//lookupCVRT工作=(lookupCVRT)e.Row.DataItem;
//GridView gv=发送方作为GridView;
//如果(work.ID!=null)
//{
//int index=gv.Columns.HeaderIndex(“选择”);
//如果(索引>-1)
//    {
//e.Row.Cells[index].Attributes.Add(“class”,“gvCVRTRow”);
//e.Row.Cells[index].ToolTip=“单击此处编辑检查表”;
//    }
//}         
}
私有列表GetRepositary()
{
List allChild=新列表();
随机r=新随机();
对于(int i=0;i<50;i++)
{
ChildClass c=新的ChildClass
{
ChildId=i,
ParentId=r.Next(5,10),
Name=“Child Name”+i,
滚动=i
};
添加(c);
}
归还所有儿童;
}
}
公共类父类
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公营儿童班
{
public int ParentId{get;set;}
public int ChildId{get;set;}
公共int Roll{get;set;}
公共字符串名称{get;set;}
}

但是当单击select时,
Gridview
会被绑定,因为我需要从所选行获取ID:
gvCVRT.SelectedDataKey.Value.ToString()
。我无法从页面加载中执行此操作,因为我还没有ID。@user123456789:是的,我理解。我再次阅读了你的问题,我对控件和事件处理程序的名称越来越困惑。澄清一下:您有一个GridView
gvCVRT
,它是具有事件处理程序
gridviewParent\u SelectedIndexChanged
gvCVRT\u RowDataBound
的父级。然后是一个子GridView
gvCVRTDetails
,但在问题中没有提到子GridView的事件处理程序。使用
RowDataBound
是指
gvCVRT\u RowDataBound
还是子GridView的处理程序?@user123456789:但是,
RowDataBound
仅在调用
DataBind
后运行。因此,检查代码中对
DataBind
的所有调用以及它们是否在正确的位置是一个很好的步骤。例如,如果父GridView在
Page\u Load
中是数据绑定的,则其
gvCVRT\u RowDataBound
将在
gridviewParent\u SelectedIndexChanged
之前运行,并且很可能会销毁所选内容。是的,父GridView在
页面中是数据绑定的
// 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>;
        }
    }
}
<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; }
}