MrProgram
MrProgram

Reputation: 5242

Refresh an updatepanel on buttonclick from code-behind

I'm using a Gridview that is using datasource & databinding. When i reload the page the gridview is updated but I want it to be on the buttonclick, but it's not working for me.

The gridview inside the updatepanel:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
        <ContentTemplate>
            <asp:GridView ID="gvWallPosts" runat="server" AutoGenerateColumns = "false" 
             CaptionAlign="NotSet" CellPadding="5">
            <Columns>
                <asp:TemplateField HeaderText="Avsändare">
                    <ItemTemplate>
                        <%# GetSender((int)Eval("WallSender"))%>
                        <br />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Inlägg">
                    <ItemTemplate>
                        <%# Eval("Post")%>
                        <br />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:TextBox ID="txtWall" runat="server" Height="105px" TextMode="MultiLine" Width="227px"></asp:TextBox>
    <br />
    <asp:Button ID="btnWall" runat="server" Text="Posta" onclick="btnWall_Click" />

Code-behind:

protected void btnWall_Click(object sender, EventArgs e)
    {
        con.SendWallPost(con.GetId(Membership.GetUser().UserName), Convert.ToInt32(Request.QueryString["ID"]), txtWall.Text); //This method is sending the post
        upWall.Update();
    }

So, I want the updatepanel to be updated on the ButtonClick, I don't want to reload the whole page to see the result

Upvotes: 2

Views: 33308

Answers (3)

abidmix
abidmix

Reputation: 1748

By default, partial-page updates are enabled in an update panel because the default value of the EnablePartialRendering property of the ScriptManager control is true.Putting the button in the update panel is suffice to give you what you need since the button acts as an asynchronus postback control inside the panel.Then just add this line( gvWallospts.Databind()) after your update.Let me know how it goes.

protected void btnWall_Click(object sender, EventArgs e)
{
    con.SendWallPost(con.GetId(Membership.GetUser().UserName), Convert.ToInt32(Request.QueryString["ID"]), txtWall.Text); //This method is sending the post
    //upWall.Update();
    gvWallPosts.DataBind();
}

Try setting up you markup like this

  <asp:ScriptManager ID="ScriptManager1" runat="server">
   </asp:ScriptManager>
      <asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
    <ContentTemplate>
        <asp:GridView ID="gvWallPosts" runat="server" AutoGenerateColumns = "false" 
         CaptionAlign="NotSet" CellPadding="5">
        <Columns>
    <asp:Templatefield>
   <asp:Button ID="btnWall" runat="server" Text="Posta" command="Edit" />
     </asp:TemplateField>
            <asp:TemplateField HeaderText="Avsändare">
                <ItemTemplate>
                    <%# GetSender((int)Eval("WallSender"))%>
                    <br />
                </ItemTemplate>

                <EditItemTemplate>

                      <asp:TextBox Text='<%# Bind("WallSender")%>' />
                </EditItemTemplate>

            </asp:TemplateField>
            <asp:TemplateField HeaderText="Inlägg">
                <ItemTemplate>
                    <%# Eval("Post")%>
                    <br />
                </ItemTemplate>
                   <EditItemTemplate>

                     <asp:TextBox  Text='<%# Bind("Post")%>'/>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

In your grid Row updating event

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


    con.SendWallPost(con.GetId(Membership.GetUser().UserName),    Convert.ToInt32(Request.QueryString["ID"]), txtWall.Text); 
     gvWallPosts.DataBind();


}

Make sure that also you Binding code in page load is sandwiched by this

 If(!IsPostBack)
     {
                   }

Upvotes: 1

Murtuza Kabul
Murtuza Kabul

Reputation: 6514

You should either put the button inside the update panel or define an explicit trigger to update the update panel on button click event as suggested by Tim Schmelter.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460038

Since i don't see the button btnWall, i assume that it's outside of the UpdatePanel. But you need to define an explicit trigger if you want to allow a control outside of an UpdatePanel to trigger a postback.

Therefore you can use an AsyncPostBackTrigger:

<asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
     <ContentTemplate>
         ....
     </ContentTemplate>
     <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnWall" EventName="Click" />
     </Triggers>
 </asp:UpdatePanel>

Upvotes: 9

Related Questions