SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to populate a GridView in a pop-up that is inside an UpdatePanel after a button click inside an UpdatePanel

I have a GridView which has an ImageButton. When clicked it runs a query and populate a GridView from the result (or it should:

<asp:UpdatePanel runat="server" ClientIDMode="Static" ID="TasksUpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated" OnRowCommand="yourTasksGV_RowCommand">
            <Columns>
                <asp:HyperLinkField Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ImageUrl="~/theImages/Dependencies.png" CssClass="gvTaskDep" runat="server" ID="btnShowDepend" CommandName="TaskDepend" CommandArgument='<%#Eval("Object") + "," + Eval("FK") %>' ToolTip="Click to view Dependencies" />
                    </ItemTemplate>
                </asp:TemplateField>
              </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

Object and FK are from the original query which populates the above GridView.

Code behind:

protected void yourTasksGV_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "TaskDepend")
    {
        string[] id = e.CommandArgument.ToString().Split(',');
        connString = @"";
        string objectid = id[0];
        string fkobjectid = id[1];
        string queryStringParent =
                           string.Format(@"SELECT
                                 CT.OBJECTID 'Object ID'
                                 ,LTRIM(RTRIM(CT.ATTR2739)) 'Task Name'
                                 ,UA.USERNAME 'Manager'
                                 ,CT.ATTR2941 'Task Start Date'
                                 ,LTRIM(RTRIM(CT.ATTR2812)) 'Status'
                                 ,CT.ATTR2752 'Due Date'
                                 ,CT.ATTR2757 'Completed Date'
                                 ,CT.ATTR2849 'Checked Date'
                                 ,LTRIM(RTRIM(CT.ATTR2847)) 'Checked Status'
                                 ,LTRIM(RTRIM(CT.ATTR2848)) 'Checked User'
                                 ,CT.FK2744 'Dependent Task'
                   FROM HSI.RMOBJECTINSTANCE1224 CT
                                 LEFT JOIN HSI.RMOBJECTINSTANCE1232 S ON S.OBJECTID = CT.FK2940
                                 LEFT JOIN HSI.USERACCOUNT UA ON UA.USERNUM = (S.FK2852 * -1)
                   WHERE CT.ACTIVESTATUS = 0 AND CT.OBJECTID = '{0}'", fkobjectid);
        string queryStringChildren =
           string.Format(@"SELECT
                                 CT.OBJECTID 'Object ID'
                                 ,LTRIM(RTRIM(CT.ATTR2739)) 'Task Name'
                                 ,UA.USERNAME 'Manager'
                                 ,CT.ATTR2941 'Task Start Date'
                                 ,LTRIM(RTRIM(CT.ATTR2812)) 'Status'
                                 ,CT.ATTR2752 'Due Date'
                                 ,CT.ATTR2757 'Completed Date'
                                 ,CT.ATTR2849 'Checked Date'
                                 ,LTRIM(RTRIM(CT.ATTR2847)) 'Checked Status'
                                 ,LTRIM(RTRIM(CT.ATTR2848)) 'Checked User'
                                 ,CT.FK2744 'Dependent Task'
                   FROM HSI.RMOBJECTINSTANCE1224 CT
                                 LEFT JOIN HSI.RMOBJECTINSTANCE1232 S ON S.OBJECTID = CT.FK2940
                                 LEFT JOIN HSI.USERACCOUNT UA ON UA.USERNUM = (S.FK2852 * -1)
                   WHERE CT.ACTIVESTATUS = 0 AND CT.FK2744 = '{0}'", objectid);

        using (SqlConnection connection = new SqlConnection(connString))
        {
            //Get Parent Task
            SqlCommand command = new SqlCommand(queryStringParent, connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();
            gvParentTasks.DataSource = reader; //not working
            gvParentTasks.DataBind(); //not working
            upParentTasks.Update(); //not working
            if (!string.IsNullOrEmpty(fkobjectid) && fkobjectid != "&nbsp;")
            {
                // Call Read before accessing data. 
                while (reader.Read())
                {
                    //ddlCli.Items.Add(new System.Web.UI.WebControls.ListItem(reader[0].ToString().TrimEnd(), reader[0].ToString().TrimEnd()));

                }
            }
            // Call Close when done reading.
            reader.Close();

            //Get Child Tasks
            command = new SqlCommand(queryStringChildren, connection);

            reader = command.ExecuteReader();

            // Call Read before accessing data. 
            while (reader.Read())
            {
                //ddlCli.Items.Add(new System.Web.UI.WebControls.ListItem(reader[0].ToString().TrimEnd(), reader[0].ToString().TrimEnd()));

            }

            // Call Close when done reading.
            reader.Close();
        }
    }
}

I have a popup which has a GridView:

<div id="popupDep">
    <a id="popupDepClose" title="Close Window">x</a>
    <h3>Parent Dependencies</h3>
    <div id="dvFirst" class="parentGridHolder">
        <asp:UpdatePanel ID="upParentTasks" runat="server" UpdateMode="Conditional" ClientIDMode="Static">
            <ContentTemplate>
                <asp:GridView ID="gvParentTasks" runat="server" AllowSorting="false" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" ClientIDMode="Static">
                    <Columns>
                        <asp:BoundField DataField="Task Name" HeaderText="Task Name" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
                        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
                        <asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
                        <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager" ItemStyle-CssClass="taskTableColumn" />
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    <h3>Children Dependencies</h3>
    <div id="dvSecond" class="childrenGridHolder">
        <asp:UpdatePanel ID="upChildrenTasks" runat="server" UpdateMode="Conditional" ClientIDMode="Static">
            <ContentTemplate>
                <asp:GridView ID="gvChildrenTasks" runat="server" AllowSorting="false" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" ClientIDMode="Static">
                    <Columns>
                        <asp:BoundField DataField="Task Name" HeaderText="Task Name" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
                        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
                        <asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
                        <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager" ItemStyle-CssClass="taskTableColumn" />
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</div>

JQuery which displays the pop-up:

$(document).ready(function () {
    $("#btnShowDepend").click(function (e) {
        e.preventDefault();
        centerPopupDep();
        loadPopupDep();
    });
});

When I click on btnShowDepend image, the pop-up is displayed but nothing is displayed in the GridView. Also, because I am using an ID, if there are multiple rows only the first button triggers the pop-up.

How can I modify the code so that the pop-up is displayed with the GridView populated and for each button that represents individual row handles its own click?

Upvotes: 0

Views: 546

Answers (1)

PieterSchool
PieterSchool

Reputation: 509

Use a class for the binding in sted of an id

ASP:

<asp:UpdatePanel runat="server" ClientIDMode="Static" ID="TasksUpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated" OnRowCommand="yourTasksGV_RowCommand">
            <Columns>
                <asp:HyperLinkField Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ImageUrl="~/theImages/Dependencies.png" CssClass="gvTaskDep btnShowDepend" runat="server" ID="btnShowDepend" CommandName="TaskDepend" CommandArgument='<%#Eval("Object") + "," + Eval("FK") %>' ToolTip="Click to view Dependencies" />
                    </ItemTemplate>
                </asp:TemplateField>
              </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

jquery:

$(document).ready(function () {
    $(".btnShowDepend").click(function (e) {
        e.preventDefault();
        centerPopupDep();
        loadPopupDep();
    });
});

Added the class btnShowDepend to the button, this enables the binding to Jquery. If you want to transfer an id you can add it as alt an get it by getting the .attr('alt')

Upvotes: 1

Related Questions