locked
Placing gridview inside update panel issue RRS feed

  • Question

  • User-809753952 posted

    I have a parent gridview inside an update panel. 

    I have created a child gridview in the item template which is visible or hidden on clicking an image on the parent gridview rows.

    When parent gridview page indexes are changed, the child gridview is not shown, if I place the parent inside the update panel.

    How can I solve this issue?

    Wednesday, October 21, 2020 10:26 AM

Answers

  • User1535942433 posted

    Hi mnmhemaj,

    Accroding to your description,as far as I think,you could use AsyncPostBackTrigger in updatepanel.

    Since you don't post your codes,I have created a demo for you.Just like this:

    <script language="javascript" type="text/javascript">
            function divexpandcollapse(divname) {
                var div = document.getElementById(divname);
                var img = document.getElementById('img' + divname);
                if (div.style.display == "none") {
                    div.style.display = "inline";
                    img.src = "image/download.jpg";
                } else {
                    div.style.display = "none";
                    img.src = "image/download.jpg";
                }
            }
        </script>
     <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:GridView ID="gvParentGrid" runat="server" DataKeyNames="Id" Width="300" PageSize="2" AllowPaging="true" OnPageIndexChanging="gvParentGrid_PageIndexChanging"
                    AutoGenerateColumns="false" OnRowDataBound="gvParentGrid_RowDataBound" GridLines="None" BorderStyle="Solid" BorderWidth="1px" BorderColor="#df5015">
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <RowStyle BackColor="#E1E1E1" />
                    <AlternatingRowStyle BackColor="White" />
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="20px">
                            <ItemTemplate>
                                <a href="JavaScript:divexpandcollapse('div<%# Eval("Id") %>');">
                                    <img id="imgdiv<%# Eval("Id") %>" width="9px" border="0" src="image/download.jpg" />
                                </a>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-HorizontalAlign="Left" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <tr>
                                    <td colspan="100%">
                                        <div id="div<%# Eval("Id") %>" style="display: none; ; left: 15px; overflow: auto">
                                            <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="250px">
                                                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                                <RowStyle BackColor="#E1E1E1" />
                                                <AlternatingRowStyle BackColor="White" />
                                                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                                <Columns>
                                                    <asp:BoundField DataField="Year" HeaderText="Year" HeaderStyle-HorizontalAlign="Left" />
                                                </Columns>
                                            </asp:GridView>
                                        </div>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                    </ContentTemplate>
                    <Triggers>
                    <asp:AsyncPostBackTrigger EventName="PageIndexChanging" ControlID="gvParentGrid" />
                </Triggers>
                </asp:UpdatePanel>
                
            </div>

    Code-Behind:

    Class SurroundingClass
        Protected Sub bind()
            Dim str, strSql As String
            str = System.Configuration.ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
            Dim conn As SqlConnection = New SqlConnection(str)
            strSql = "select * from Sum"
            Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, str)
            Dim ds As DataSet = New DataSet()
            da.Fill(ds, "Sum")
            Me.gvParentGrid.DataSource = ds.Tables(0).DefaultView
            Me.gvParentGrid.DataBind()
            conn.Close()
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                bind()
            End If
        End Sub
    
        Protected Sub gvParentGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim str, strSql As String
                str = System.Configuration.ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
                Dim con As SqlConnection = New SqlConnection(str)
                con.Open()
                Dim gv As GridView = CType(e.Row.FindControl("gvChildGrid"), GridView)
                Dim CountryId As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
                Dim cmd As SqlCommand = New SqlCommand("select Year from Sum where Id=" & CountryId, con)
                Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim ds As DataSet = New DataSet()
                da.Fill(ds)
                con.Close()
                gv.DataSource = ds
                gv.DataBind()
            End If
        End Sub
    
        Protected Sub gvParentGrid_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
            gvParentGrid.PageIndex = e.NewPageIndex
            bind()
        End Sub
    End Class

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 22, 2020 4:11 AM

All replies

  • User-582711651 posted

    @mnmhemaj

    We can provide a solution when you share your codes, think you need to include triggers

    like this;

        <asp:AsyncPostBackTrigger ControlID="Dr_Grid" EventName="SelectedIndexChanged" />

    possible share your code

    Wednesday, October 21, 2020 7:59 PM
  • User1535942433 posted

    Hi mnmhemaj,

    Accroding to your description,as far as I think,you could use AsyncPostBackTrigger in updatepanel.

    Since you don't post your codes,I have created a demo for you.Just like this:

    <script language="javascript" type="text/javascript">
            function divexpandcollapse(divname) {
                var div = document.getElementById(divname);
                var img = document.getElementById('img' + divname);
                if (div.style.display == "none") {
                    div.style.display = "inline";
                    img.src = "image/download.jpg";
                } else {
                    div.style.display = "none";
                    img.src = "image/download.jpg";
                }
            }
        </script>
     <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:GridView ID="gvParentGrid" runat="server" DataKeyNames="Id" Width="300" PageSize="2" AllowPaging="true" OnPageIndexChanging="gvParentGrid_PageIndexChanging"
                    AutoGenerateColumns="false" OnRowDataBound="gvParentGrid_RowDataBound" GridLines="None" BorderStyle="Solid" BorderWidth="1px" BorderColor="#df5015">
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <RowStyle BackColor="#E1E1E1" />
                    <AlternatingRowStyle BackColor="White" />
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="20px">
                            <ItemTemplate>
                                <a href="JavaScript:divexpandcollapse('div<%# Eval("Id") %>');">
                                    <img id="imgdiv<%# Eval("Id") %>" width="9px" border="0" src="image/download.jpg" />
                                </a>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-HorizontalAlign="Left" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <tr>
                                    <td colspan="100%">
                                        <div id="div<%# Eval("Id") %>" style="display: none; ; left: 15px; overflow: auto">
                                            <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="250px">
                                                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                                <RowStyle BackColor="#E1E1E1" />
                                                <AlternatingRowStyle BackColor="White" />
                                                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                                <Columns>
                                                    <asp:BoundField DataField="Year" HeaderText="Year" HeaderStyle-HorizontalAlign="Left" />
                                                </Columns>
                                            </asp:GridView>
                                        </div>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                    </ContentTemplate>
                    <Triggers>
                    <asp:AsyncPostBackTrigger EventName="PageIndexChanging" ControlID="gvParentGrid" />
                </Triggers>
                </asp:UpdatePanel>
                
            </div>

    Code-Behind:

    Class SurroundingClass
        Protected Sub bind()
            Dim str, strSql As String
            str = System.Configuration.ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
            Dim conn As SqlConnection = New SqlConnection(str)
            strSql = "select * from Sum"
            Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, str)
            Dim ds As DataSet = New DataSet()
            da.Fill(ds, "Sum")
            Me.gvParentGrid.DataSource = ds.Tables(0).DefaultView
            Me.gvParentGrid.DataBind()
            conn.Close()
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                bind()
            End If
        End Sub
    
        Protected Sub gvParentGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim str, strSql As String
                str = System.Configuration.ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
                Dim con As SqlConnection = New SqlConnection(str)
                con.Open()
                Dim gv As GridView = CType(e.Row.FindControl("gvChildGrid"), GridView)
                Dim CountryId As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
                Dim cmd As SqlCommand = New SqlCommand("select Year from Sum where Id=" & CountryId, con)
                Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim ds As DataSet = New DataSet()
                da.Fill(ds)
                con.Close()
                gv.DataSource = ds
                gv.DataBind()
            End If
        End Sub
    
        Protected Sub gvParentGrid_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
            gvParentGrid.PageIndex = e.NewPageIndex
            bind()
        End Sub
    End Class

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 22, 2020 4:11 AM