by joe.pesch
22. December 2010 08:13
SimpleWebService.ASMX:
<%@ WebService Language="C#" Class="SimpleWebService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://JMP-Sample.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SimpleWebService : System.Web.Services.WebService {
[WebMethod]
public string WsTestMethod(long TestId, string TestData)
{
return string.Join(" ", "You sent TestId:", TestId.ToString(), "; TestData:", TestData);
}
}
SimpleWebService.ASPX:
<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<script runat="server">
protected string RootUrl
{
get
{
string query = (Request.QueryString == null) ? "" : "?" + Request.QueryString.ToString();
return Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, string.Empty).Replace(query, string.Empty);
}
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
string soap =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<WsTestMethod xmlns=""http://JMP-Sample.org/"">
<TestId>" + this.TestId.Text + @"</TestId>
<TestData>" + this.TestData.Text + @"</TestData>
</WsTestMethod>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
string.Join("", RootUrl, ResolveUrl("~/SimpleWebService.asmx")));
req.Headers.Add("SOAPAction", "\"http://JMP-Sample.org/WsTestMethod\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
Stream str = req.GetRequestStream();
using (StreamWriter wri = new StreamWriter(str)) wri.Write(soap);
WebResponse res = req.GetResponse();
str = res.GetResponseStream();
this.WsResults.Text = StreamToString(str);
}
private string StreamToString(Stream str)
{
using (BinaryReader rdr = new BinaryReader(str))
{
using (MemoryStream mem = new MemoryStream())
{
byte[] buf = new byte[256];
int bt;
int bts = 0;
while ((bt = rdr.Read(buf, 0, 256)) > 0)
{
mem.Write(buf, 0, bt);
bts += bt;
}
mem.Position = 0;
byte[] bytes = new byte[bts];
mem.Read(bytes, 0, bytes.Length);
return Encoding.ASCII.GetString(bytes);
}
}
}
</script>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TestId" Text="12345" runat="server"></asp:TextBox>
<asp:TextBox ID="TestData" Text="Simple Web Service" runat="server"></asp:TextBox>
<asp:Button ID="SubmitButton" OnClick="SubmitButton_Click" Text="Run Web Service"
runat="server" />
<asp:Literal ID="WsResults" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
6e282c92-6d65-43eb-8962-f8c16ac8192f|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags:
ASP.Net | C# | Web Service
by joe.pesch
21. December 2010 11:39
If you attempt to change drop-down list options dynamically on the client side you may have issues with postback as the drop-down options will not match what the server originally sent to the client. One way to overcome this is to have a server control with all the possible values (that is hidden on the UI) and a visible client side only drop-down list that is dynamically changed. As the client side drop-down values are changed keep the server side control synchronized and then use the server side control value on post-back. Simple sample below:
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ServerSelect.DataSource = new string[] { "one", "two", "three", "four", "five" };
this.ServerSelect.DataBind();
}
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
this.ResultLabel.Text = string.Join(" ", "You selected value:", this.ServerSelect.SelectedValue.ToString());
}
</script>
<!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></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.each(["one", "two", "three", "four", "five"], function (index, value) {
$('select#LocalSelect').append($('<option></option>').val(value).html(value));
});
$('select#LocalSelect').change(function () {
$('select#<%=ServerSelect.ClientID%>').val($('select#LocalSelect').val());
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ServerSelect" runat="server" Style="display: none;" />
<select id="LocalSelect">
</select>
<asp:Button ID="SubmitButton" Text="Submit" OnClick="SubmitButton_Click" runat="server" />
<asp:Label ID="ResultLabel" runat="server" />
</div>
</form>
</body>
</html>
cbce0cbd-f973-40e8-b186-5730c863f0b3|1|5.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags:
by joe.pesch
21. December 2010 10:51
Make sure you have the assemblies referenced as follows:
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
<buildProviders>
<add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
</buildProviders>
</compilation>
c41ad7ec-7e99-431b-823c-a4161fe9442b|2|5.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags:
by joe.pesch
21. December 2010 07:39
In the page:
<%=RootUrl%><%=ResolveUrl("~/InmanNewsFeedWs.asmx")%>
Code behind:
protected string RootUrl
{
get
{
string query = (Request.QueryString == null) ? "" : "?" + Request.QueryString.ToString();
return Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, string.Empty).Replace(query, string.Empty);
}
}
c819ab8f-b989-491f-8d7d-e299f2fe1327|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags: