Call RowDataBound from another function

I have 2 Gridviews. The first grid has a button that, when pressed, will fill the second grid with data based on the identifier of the button pressed.

Then I have the code in the RowDataBound function to show the grid based on the selected row. But the problem is that the code starts RowDataBound automatically before the fill function. Thus, the second grid is not displayed.

Code for 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 selection 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 that when I click the Select button in the grid, it starts RowDataBound first and then gridviewParent_SelectedIndexChanged , but I need to start gridviewParent_SelectedIndexChanged first. Can I call the RowDataBound function from gridviewParent_SelectedIndexChanged ?

Page_Load function:

 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(); } } } 
+7
c # gridview
source share
2 answers

The OnRowDataBound event OnRowDataBound raised only when the DataBind method for the GridView called.

In your specific case, the problem is in Page_Load in the else branch of the Page.IsPostBack condition:

  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. If you do not reset ViewState["ParentID"] somewhere else in your code, then with each postback you bind the GridView gvCVRT . It is for this reason that RowDataBound is RowDataBound . Upon completion of Page_Load page calls additional event handlers in your case gridviewParent_SelectedIndexChanged .

To solve this problem, you need to change the code in the Page_Load handler so that there are no DataBind calls for postback:

 // 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 the postback request and that you put it in the ViewState["CVRT"] instead of requesting the data again. Web applications often read data for a new request. So you can think about whether you really need to put the data in the ViewState or you can request it after the postback from the data source.

Putting data in the ViewState increases the size of the page that is passed to the client (basically you have HTML for the GridView and, in addition, you have data in the ViewState). Therefore, in most cases, requesting them again will be the best way.

+1
source share

I don’t know why you preferred to use gridviewParent_SelectedIndexChanged , then grdParent_RowDataBound ... I created a simple solution for you .. this could help you.

 <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; } } 
0
source share

All Articles