I have two gridviews. The user can select a row from the first GridView and display a list based on the selected GridView ID.
First grid: 
Second grid: 
Code for the first GridView:
<asp:GridView style="width:75%" ID="gvCVRT" ShowHeaderWhenEmpty="true" CssClass="tblResults" runat="server" OnSelectedIndexChanged="gridviewParent_SelectedIndexChanged" OnRowDataBound="gvCVRT_RowDataBound" 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> <asp:TemplateField HeaderText="Delete" ItemStyle-CssClass="tblRowDelete"> <ItemTemplate> <asp:LinkButton ID="btnDelete" runat="server" OnClientClick="event.stopPropagation()" OnClick="btnDeleteCVRT_Click"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </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; string checklistid = work.ChecklistID.ToString(); e.Row.Attributes.Add("ID", "gvCVRT_" + work.ID); LinkButton btnDelete = (LinkButton)e.Row.FindControl("btnDelete"); btnDelete.CommandArgument = checklistid; 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"; e.Row.Cells[index].Attributes.Add("style", "color:blue;cursor:pointer;cursor:hand"); } } } }
Code for gridviewParent_SelectedIndexChanged :
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 ();"); }
JavaScript:
$(".gvCVRTRow").off(); $(".gvCVRTRow").click(function (e) { ShowAddEditCVRT(this, "Edit"); }); function ShowAddEditCVRT(sender, AddEdit) { $("#divCVRTDetails").fadeIn(300); }
Sorry, a lot of code, but I wanted to show what exactly is happening. When I set the code to int index = gv.Columns.HeaderIndex("Select"); , it first goes into the javascript function ShowAddEditCVRT , which tries to display the second grid divCVRTDetails . But first, I need to run the gridviewParent_SelectedIndexChanged method, because it associates the second grid with an identifier selected from the first grid.
The only way I can first link the second grid is to change the code to this: int index = gv.Columns.HeaderIndex("Checklist Date"); . Thus, the user must click on the selection first (snap the grid), and then click on the date cell to display the grid.
so my question is: is there anyway to run the gridviewParent_SelectedIndexChanged method before calling the javascript function?