HighTechTalks DotNet Forums  

Web methods in user controls?

ASP.net Web Services microsoft.public.dotnet.framework.aspnet.webservices


Discuss Web methods in user controls? in the ASP.net Web Services forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
MarkShoe
 
Posts: n/a

Default Web methods in user controls? - 12-13-2007 , 10:58 AM






Is it possible to use Webmethods in user controls (ascx)?



Reply With Quote
  #2  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default RE: Web methods in user controls? - 12-13-2007 , 09:48 PM






Hi MarkShoe,

As for the question you mentioned, do you mean you have an existing ASP.NET
webservice, and you're wantting to call this webservice in a certain method
of an ASP.NET ascx usercontrol?

If this is the case, I think it is certainly supported. A usercontrol will
be embeded in an ASP.NET web page. As long as web page postback events can
perform webservice method call, usercontrol methods can also do the same
thing.

Please feel free to let me know if there is anything I misunderstand.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Quote:
From: "MarkShoe" <mark.shoe (AT) community (DOT) nospam
Subject: Web methods in user controls?
Date: Thu, 13 Dec 2007 11:58:07 -0500

Is it possible to use Webmethods in user controls (ascx)?





Reply With Quote
  #3  
Old   
MarkShoe
 
Posts: n/a

Default Re: Web methods in user controls? - 12-14-2007 , 08:48 AM



Hi Steven, thanks for your reply. Perhaps my question was too general.
Allow me to be a bit more specific.

I have a simple web user control with a button and a div on it. On the
containing page, I have a ScriptManager with EnablePageMethods=true.
When the user clicks the button, I would like it to log the time into the
'timelog' div. What actually happens though, is that when I click the
button, I get a javascript error claiming that PageMethods is not defined.
My question is whether using a webmethod within a user control is supported,
and if not, if there is another way to do this?

Here's the .ascx:

<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="MyUserControl.ascx.cs" Inherits="MyUserControl" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>

<script type="text/javascript">
function RetrieveTime(controlId){
PageMethods.GetTime(controlId,ReceiveTime);
}
function ReceiveTime(time){
document.getElementById('timeLog').innerHTML += '<br />' + time;
}
</script>

<asp:ScriptManagerProxy ID="ScriptManagerProxy1"
runat="server"></asp:ScriptManagerProxy>
<input id="Button1" type="button" value="button"
onclick="RetrieveTime(this.id);"/>
<div id="timeLog"></div>


========== And here's the code-behind ============


public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

[WebMethod]
public static String GetTime(String controlId)
{
return System.DateTime.Now.ToString();
}
}




"Steven Cheng[MSFT]" <stcheng (AT) online (DOT) microsoft.com> wrote

Quote:
Hi MarkShoe,

As for the question you mentioned, do you mean you have an existing
ASP.NET
webservice, and you're wantting to call this webservice in a certain
method
of an ASP.NET ascx usercontrol?

If this is the case, I think it is certainly supported. A usercontrol will
be embeded in an ASP.NET web page. As long as web page postback events can
perform webservice method call, usercontrol methods can also do the same
thing.

Please feel free to let me know if there is anything I misunderstand.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "MarkShoe" <mark.shoe (AT) community (DOT) nospam
Subject: Web methods in user controls?
Date: Thu, 13 Dec 2007 11:58:07 -0500

Is it possible to use Webmethods in user controls (ascx)?







Reply With Quote
  #4  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: Web methods in user controls? - 12-18-2007 , 01:58 AM



Hi Mark,

Thanks for your reply and the further clarity.

Now I've got your actual concern, that is try to make an codebehind method
in ascx usercontrol be called by AJAX client service call.

Based on my research, I'm afraid codebehind method in ascx control can not
be directly exposed as AJAX webservice methods(called via Page methods
collection). We have to modify the page's codebehind so as to support some
certain ajax function calls.

BTW, I've found a forum thread in which someone mentioned a workaround for
his particular scenario. You can have a look to see whether it fits your
case also:

http://forums.asp.net/p/1145083/1857883.aspx#1857883

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Quote:
From: "MarkShoe" <mark.shoe (AT) community (DOT) nospam
References: <e6jgYlaPIHA.3532 (AT) TK2MSFTNGP04 (DOT) phx.gbl
mHn2sQgPIHA.3644 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl
Subject: Re: Web methods in user controls?
Date: Fri, 14 Dec 2007 09:48:50 -0500

Hi Steven, thanks for your reply. Perhaps my question was too general.
Allow me to be a bit more specific.

I have a simple web user control with a button and a div on it. On
the
containing page, I have a ScriptManager with EnablePageMethods=true.
When the user clicks the button, I would like it to log the time into the
'timelog' div. What actually happens though, is that when I click the
button, I get a javascript error claiming that PageMethods is not defined.
My question is whether using a webmethod within a user control is
supported,
and if not, if there is another way to do this?

Here's the .ascx:

%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="MyUserControl.ascx.cs" Inherits="MyUserControl" %
%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %

script type="text/javascript"
function RetrieveTime(controlId){
PageMethods.GetTime(controlId,ReceiveTime);
}
function ReceiveTime(time){
document.getElementById('timeLog').innerHTML += '<br />' + time;
}
/script

asp:ScriptManagerProxy ID="ScriptManagerProxy1"
runat="server"></asp:ScriptManagerProxy
input id="Button1" type="button" value="button"
onclick="RetrieveTime(this.id);"/
div id="timeLog"></div


========== And here's the code-behind ============


public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

[WebMethod]
public static String GetTime(String controlId)
{
return System.DateTime.Now.ToString();
}
}




Reply With Quote
  #5  
Old   
MarkShoe
 
Posts: n/a

Default Re: Web methods in user controls? - 12-19-2007 , 10:23 AM



Thanks Steven, you have confirmed my suspicions and answered my question.

I will consider using one of these 3 alternatives:

1. A web service which can be accessed from the web user control
(Although I don't really want to expose these methods publically with a
web service)

2. Using client script callbacks
(Not as flexible as the WebMethod -> PageMethod approach; can only pass
one string parameter)

3. The workaround you suggested.
(Requires that the WebMethod be in the page rather than the control;
making the web user control less independent)

Thanks very much for your help.

Mark

"Steven Cheng[MSFT]" <stcheng (AT) online (DOT) microsoft.com> wrote

Quote:
Hi Mark,

Thanks for your reply and the further clarity.

Now I've got your actual concern, that is try to make an codebehind method
in ascx usercontrol be called by AJAX client service call.

Based on my research, I'm afraid codebehind method in ascx control can not
be directly exposed as AJAX webservice methods(called via Page methods
collection). We have to modify the page's codebehind so as to support some
certain ajax function calls.

BTW, I've found a forum thread in which someone mentioned a workaround for
his particular scenario. You can have a look to see whether it fits your
case also:

http://forums.asp.net/p/1145083/1857883.aspx#1857883

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "MarkShoe" <mark.shoe (AT) community (DOT) nospam
References: <e6jgYlaPIHA.3532 (AT) TK2MSFTNGP04 (DOT) phx.gbl
mHn2sQgPIHA.3644 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl
Subject: Re: Web methods in user controls?
Date: Fri, 14 Dec 2007 09:48:50 -0500

Hi Steven, thanks for your reply. Perhaps my question was too general.
Allow me to be a bit more specific.

I have a simple web user control with a button and a div on it. On
the
containing page, I have a ScriptManager with EnablePageMethods=true.
When the user clicks the button, I would like it to log the time into the
'timelog' div. What actually happens though, is that when I click the
button, I get a javascript error claiming that PageMethods is not defined.
My question is whether using a webmethod within a user control is
supported,
and if not, if there is another way to do this?

Here's the .ascx:

%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="MyUserControl.ascx.cs" Inherits="MyUserControl" %
%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %

script type="text/javascript"
function RetrieveTime(controlId){
PageMethods.GetTime(controlId,ReceiveTime);
}
function ReceiveTime(time){
document.getElementById('timeLog').innerHTML += '<br />' + time;
}
/script

asp:ScriptManagerProxy ID="ScriptManagerProxy1"
runat="server"></asp:ScriptManagerProxy
input id="Button1" type="button" value="button"
onclick="RetrieveTime(this.id);"/
div id="timeLog"></div


========== And here's the code-behind ============


public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

[WebMethod]
public static String GetTime(String controlId)
{
return System.DateTime.Now.ToString();
}
}




Reply With Quote
  #6  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: Web methods in user controls? - 12-19-2007 , 11:44 PM



Thanks for your reply and sharing the further ideas with us.

If you meet other problems later, also welcome to post here.

BTW, you're also recommended to post your comments or any suggestion to the
product feedback center:

http://connect.microsoft.com/feedbac...spx?SiteID=210

this will be very helpful for our future improvement.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Quote:
From: "MarkShoe" <mark.shoe (AT) community (DOT) nospam
References: <e6jgYlaPIHA.3532 (AT) TK2MSFTNGP04 (DOT) phx.gbl
mHn2sQgPIHA.3644 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl
<eMIBzBmPIHA.3676 (AT) TK2MSFTNGP06 (DOT) phx.gbl>
<GrNoJvUQIHA.1584 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl>
Quote:
Subject: Re: Web methods in user controls?
Date: Wed, 19 Dec 2007 11:23:12 -0500

Thanks Steven, you have confirmed my suspicions and answered my question.

I will consider using one of these 3 alternatives:

1. A web service which can be accessed from the web user control
(Although I don't really want to expose these methods publically with
a
web service)

2. Using client script callbacks
(Not as flexible as the WebMethod -> PageMethod approach; can only
pass
one string parameter)

3. The workaround you suggested.
(Requires that the WebMethod be in the page rather than the control;
making the web user control less independent)

Thanks very much for your help.

Mark

"Steven Cheng[MSFT]" <stcheng (AT) online (DOT) microsoft.com> wrote in message
news:GrNoJvUQIHA.1584 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl...
Hi Mark,

Thanks for your reply and the further clarity.

Now I've got your actual concern, that is try to make an codebehind
method
in ascx usercontrol be called by AJAX client service call.

Based on my research, I'm afraid codebehind method in ascx control can
not
be directly exposed as AJAX webservice methods(called via Page methods
collection). We have to modify the page's codebehind so as to support
some
certain ajax function calls.

BTW, I've found a forum thread in which someone mentioned a workaround
for
his particular scenario. You can have a look to see whether it fits your
case also:

http://forums.asp.net/p/1145083/1857883.aspx#1857883

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "MarkShoe" <mark.shoe (AT) community (DOT) nospam
References: <e6jgYlaPIHA.3532 (AT) TK2MSFTNGP04 (DOT) phx.gbl
mHn2sQgPIHA.3644 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl
Subject: Re: Web methods in user controls?
Date: Fri, 14 Dec 2007 09:48:50 -0500

Hi Steven, thanks for your reply. Perhaps my question was too general.
Allow me to be a bit more specific.

I have a simple web user control with a button and a div on it. On
the
containing page, I have a ScriptManager with EnablePageMethods=true.
When the user clicks the button, I would like it to log the time into the
'timelog' div. What actually happens though, is that when I click the
button, I get a javascript error claiming that PageMethods is not
defined.
My question is whether using a webmethod within a user control is
supported,
and if not, if there is another way to do this?

Here's the .ascx:

%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="MyUserControl.ascx.cs" Inherits="MyUserControl" %
%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %

script type="text/javascript"
function RetrieveTime(controlId){
PageMethods.GetTime(controlId,ReceiveTime);
}
function ReceiveTime(time){
document.getElementById('timeLog').innerHTML += '<br />' + time;
}
/script

asp:ScriptManagerProxy ID="ScriptManagerProxy1"
runat="server"></asp:ScriptManagerProxy
input id="Button1" type="button" value="button"
onclick="RetrieveTime(this.id);"/
div id="timeLog"></div


========== And here's the code-behind ============


public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

[WebMethod]
public static String GetTime(String controlId)
{
return System.DateTime.Now.ToString();
}
}






Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.