Monday, August 11, 2008

How to add service reference dynamically from user control? Microsoft Ajax

First, Create a web page default.aspx and put the html code as following..

---------------------------------------------------------------------------
<asp:ScriptManager ID="MainScriptManager" runat="server">
</asp:ScriptManager>
--------------------------------------------------------------------------

Now, Create a usercontrol my first user control

Create a function called AddScriptReference as following...

'--------------------------------------------------------------------------
private void AddScriptReference()
{
     ScriptManager scriptManger = ScriptManager.GetCurrent(this.Page);
     if (scriptManger != null)
     {
         ServiceReference serviceReference = new ServiceReference();
         serviceReference.Path="~/MyWebService.asmx";
         serviceReference.InlineScript = false; 
         scriptManger.Services.Add(serviceReference);   
     }
     else
     {
         throw new Exception("Script Manager Not Found");
     }
}
'--------------------------------------------------------------------------
Then override controls init event as following...

'--------------------------------------------------------------------------
protected override void OnInit(EventArgs e)
{
     AddScriptReference();
     base.OnInit(e);
}
'--------------------------------------------------------------------------

Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews