The first two functions works well but the third one with some error unclear. Here is all the codes and could you help me how to complete the last function.
code in WebService1.asmx
using System.ComponentModel;
using System.Web.Services;
using System.Text;
using System.IO;
namespace WebApplication1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorldFun1()
{
return "Hello World";
}
[WebMethod]
public string HelloWorldFun2(string str)
{
return "Hello World,"+str;
}
[WebMethod]
public string Write_to_File(string str)
{
StreamWriter _testData = new StreamWriter(Server.MapPath("~/output.txt"), true);
_testData.WriteLine(str); // Write the file.
_testData.Flush();
_testData.Close(); // Close the instance of StreamWriter.
_testData.Dispose(); // Dispose from memory.
return str;
}
}
}
code in Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>WebService</title>
<script type="text/javascript" language="javascript">
function func1()
{
WebApplication1.WebService1.HelloWorldFun1(onSuccess,onFail,'Span1');
}
function func2()
{
var txt=document.getElementById('Text1').value;
WebApplication1.WebService1.HelloWorldFun2(txt,onSuccess,onFail,'Span2');
}
function write()
{
var txt = document.getElementById("Span1").innerHTML;
WebApplication1.WebService1.Write_to_File("kkkkkkkkkk",onSuccess,onFail,'Span3');
}
function onSuccess(value,context)
{
document.getElementById(context).innerHTML=value;
}
function onFail(value)
{
alert(value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<input id="Button1" type="button" value="button" onclick="func1()" /> <span id="Span1"></span>
<hr />
<input id="Text1" type="text" /><input id="Button2" type="button" value="button" onclick="func2()" /><span id="Span2"></span>
</form>
<input id = "WTF" type = "button" value = "write" onclick ="write()"/><span id="Span3"></span>
</body>
</html>
I have know the reason. There should be some defined function named “write()”. I just change the name “write()” to “write_to()” and then it works well. Thanks for your answers, Vitali Kaspler.