Asp.net ItemCommand asynchronous postback in an UpdatePanel

In Asp.net Simply adding an UpdatePanel to a ListView with an ItemCommand won’t do the trick of asynchronous postback. The Command should be registered to the script manager Dynamically.

To achieve an asynchronous postback you have to do the following trick

Asp.net front end Code Example

<asp:UpdatePanel runat=”server” ID=”upContent”>
<ContentTemplate>
<asp:ListView runat=”server” ID=”lvContent”
OnItemCommand=”lvContent_ItemCommand”
onitemdatabound=”lvContent_ItemDataBound” >
<ItemTemplate>
<div class=”trd-block”>
<asp:LinkButton Text=”Delete” runat=”server” ID=”lnkDelete” CommandArgument='<%#Eval(“ID”) %>’  />
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>

The solution comes in the Code Behind C# on the ItemDataBound

protected void lvContent_ItemDataBound(object sender, ListViewItemEventArgs e)
{
LinkButton delete = (LinkButton)e.Item.FindControl(“lnkDelete”);
ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(delete);
}

 

YouTube API with Asp.net C#

aspnet youtube

aspnet youtubeYouTube is a major video hosting website. IF you want to have videos on your website and you don’t want to host them, you can always use YouTube as a CDN. 1st method is to upload the video on YouTube and embed the code on your website, 2nd method is to make your website directly upload the videos to YouTube and embed them. YouTube provides a .Net Api to do this from your website directly or from your application. Continue reading “YouTube API with Asp.net C#”

RegisterStartupScript with asp:updatepanel

Using Asp.net if you want to add a script to the page from the code behind. The normal Page.RegisterStartupScript will not work if it is called from a button (or any other event) inside an updatePanel.

This is the normal code you would use ( this will work normally if you don’t have an updatePanel)

Page.RegisterStartupScript(“popup”, “<script type=’text/javascript’>alert(‘hello world’);</script>”);

It should be replaced with the following line of code

ScriptManager.RegisterStartupScript(PanelID, PanelID.GetType(), “script key”, “alert(‘hello world’);”, true);

or use it like this same result

ScriptManager.RegisterStartupScript(PanelID, PanelID.GetType(), “script key”, “<script type=’text/javascript’>alert(‘hello world’);</script>”, false);

Hope this was helpful and solved your problem