locked
Update panel - partial postback RRS feed

  • Question

  • User-193078671 posted

    Hi 

    I have a certain scenario in which I have 2 gridviews. There is a linkbutton "select" in the first gridview and on clicking it, the second gridview is populated.

    Now I dont want a full page postback when I click the "select" button of the first gridview.

    Please suggest a way to apply Update panel here.

    Monday, July 7, 2014 6:59 PM

Answers

  • User-417640953 posted

    Hi Jaydeep,

    Based on your description, I see you want set two gridviews into a UpdatePanel. And the second gridview's data

    based on the first gridview. When click the linkbutton in first gridview, then bind data to second gridview with updatepanel partial render.

    For this issue, you can try to set the first gridview OnRowCommand event as the AsyncPostBackTrigger of UpdatePanel. I made a demo below,

    Please refer to below.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                     <%#Eval("Country") %>  
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("Country") %>' CommandName="ShowCity">Select</asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate><%#Eval("City") %></ItemTemplate>
                            </asp:TemplateField> 
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
                </Triggers>
            </asp:UpdatePanel>

    Code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) 
                { 
                   //testdata
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Country");
                    dt.Rows.Add("US");
                    dt.Rows.Add("UK");
                    dt.Rows.Add("China");
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                
                }
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "ShowCity") 
                {
                    var country = e.CommandArgument.ToString();
                    //testdata
                    DataTable dt = new DataTable();
                    dt.Columns.Add("City");
                    dt.Rows.Add(country+"City1");
                    dt.Rows.Add(country + "City2");
                    dt.Rows.Add(country + "City3");
                    this.GridView2.DataSource = dt;
                    this.GridView2.DataBind();
                }
            }

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2014 3:49 AM

All replies

  • User-1569093213 posted

    thanks for posting the forum

    as per your question u want to populate the second grid view depending on select button of first grid  without full page post back....

    if i understood correctly then the solution is to put second  grid view in update panel.by using the update panel u can have partial postback means u can have postback inside the update panel only rather than full page post back.

    hope it will help you

    Tuesday, July 8, 2014 12:14 AM
  • User-417640953 posted

    Hi Jaydeep,

    Based on your description, I see you want set two gridviews into a UpdatePanel. And the second gridview's data

    based on the first gridview. When click the linkbutton in first gridview, then bind data to second gridview with updatepanel partial render.

    For this issue, you can try to set the first gridview OnRowCommand event as the AsyncPostBackTrigger of UpdatePanel. I made a demo below,

    Please refer to below.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                     <%#Eval("Country") %>  
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("Country") %>' CommandName="ShowCity">Select</asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate><%#Eval("City") %></ItemTemplate>
                            </asp:TemplateField> 
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
                </Triggers>
            </asp:UpdatePanel>

    Code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) 
                { 
                   //testdata
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Country");
                    dt.Rows.Add("US");
                    dt.Rows.Add("UK");
                    dt.Rows.Add("China");
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                
                }
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "ShowCity") 
                {
                    var country = e.CommandArgument.ToString();
                    //testdata
                    DataTable dt = new DataTable();
                    dt.Columns.Add("City");
                    dt.Rows.Add(country+"City1");
                    dt.Rows.Add(country + "City2");
                    dt.Rows.Add(country + "City3");
                    this.GridView2.DataSource = dt;
                    this.GridView2.DataBind();
                }
            }

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2014 3:49 AM