Click to See Complete Forum and Search --> : Calling Web Service from embedded user control
yeap7
February 9th, 2009, 08:21 AM
Hi
I have a user control embedded in aspx page (using object tag) .
Now I need to communicate with this user control : to invoke its methods and to invoke asp methods from the user control .
Is it possible to use a web service? can I initiate this web service from within the user control and invoke its methods?
Any other suggestions will be highly appreciated ! thx
sr_jay
February 11th, 2009, 10:50 PM
Hi,
It is very much possible to call the methods of a user control from the page on which you have placed the user control. You just need to declare the methods in the user control code as public. I am giving the code for an application below. That will show what I mean.
Here is the html code for a user control WebUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Label ID="Label1" runat="server" Visible="false" Style="z-index: 100; left: 130px; position: absolute;
top: 29px" Text="This is what you typed." Width="93px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Style="z-index: 102; left: 258px; position: absolute;
top: 26px" Width="489px"></asp:TextBox>
and here is the code behind for WebUserControl.ascx.cs
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void initproc(string val, bool pb)
{
Label1.Visible = pb;
TextBox1.Visible = pb;
TextBox1.Text = val;
}
}
As you can see there is a public procedure called initproc in this user control.
And here is the html code for default.aspx containing the user control WebUserControl with id WebUserControl1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="mybutton" Text="Submit" runat="server" style="z-index: 100; left: 411px; position: absolute; top: 184px" OnClick="mybutton_Click" />
<asp:TextBox ID="mytextbox" Text="Type something in here and you will see the user control" runat="server" style="z-index: 102; left: 359px; position: absolute; top: 133px"></asp:TextBox>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
Here is the code behind for default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void mybutton_Click(object sender, EventArgs e)
{
WebUserControl1.initproc(mytextbox.Text, IsPostBack);
}
}
As you can see the procedure initproc can be invoked from Default.aspx.cs which is the containing page of the user control. In this particular application initially the user control is invisible as all its elements are set to visibility = false. On running the application you will find a textbox and a button. If you type in something in the text box and click on the submit button the string in the text box and the IsPostback value is passed to the public initproc procedure of the user control in the click event procedure in default .aspx.cs. This makes the elements of the user control visible and sets the text box value in the user control to what you typed in initially. When the page reappears the user control will be visible and the value you typed in will be there in the user control's text box. It is also possible to invoke web services inside the user control. itself and you can receive data from the web service in a procedure inside the user control itself.
Hope this helps.
Regards,
sr_jay
As you
yeap7
February 12th, 2009, 03:17 AM
Hi
Thank u for the reply .
I think we are doing different things..
my user control is compiled in a class library , and embeded in the web page using object tag.
That way , it functions as an activex (required since i need to access client's computer)
I cannot use the user control for calling its public functions from the web page code behind..this control even does not appear in the aspx file , except in the object tag..
Anyway .. I need to know whether such an activeX can call the functions of a web service..
cause till now i failed to communicate with it via the hosting web page
any help will be highly appreciated
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.