using System;using System.IO;using System.Web.Services;using System.Xml;using taExternalTestTableAdapters;namespace nsExternalTest{ [WebService(Namespace = "nsExternalTest")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ExternalTest : System.Web.Services.WebService { public ExternalTest() { } [WebMethod] public int GetEmpID(string empLogin) { EmpTableTableAdapter taEmp = new EmpTableTableAdapter(); return Convert.ToInt32(taEmp.GetEmpID(empLogin)); } [WebMethod] public bool InsertMessage(string message, string empID) { bool success = true; try { TestTableTableAdapter taTestTable = new TestTableTableAdapter(); taTestTable.Insert(Convert.ToInt32(empID), message); } catch (Exception) { success = false; } return success; } [WebMethod] public string GetMyMessages(int empID) { StringWriter sw = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(sw); writer.Formatting = Formatting.None; writer.WriteStartElement("TestTableMessages"); try { TestTableTableAdapter taTestTable = new TestTableTableAdapter(); taExternalTest.TestTableDataTable dtTestTable = new taExternalTest.TestTableDataTable(); taTestTable.FillByEmpID(dtTestTable, empID); foreach (taExternalTest.TestTableRow row in dtTestTable) { writer.WriteStartElement("TestMessage"); writer.WriteAttributeString("Message", row.TestMessage); writer.WriteEndElement(); } } catch (Exception ex) { writer.WriteStartElement("Error"); writer.WriteAttributeString("message", ex.ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); return sw.ToString(); } }}
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ExternalTester.ascx.cs" Inherits="Controls_ExternalTester" %><table> <tr id="trGetEmpID" runat="server"> <td> <table> <tr> <td> <h2> Get Employee ID</h2> </td> </tr> <tr> <td> <asp:Button ID="btnGetEmpID" runat="server" Text="Get Employee ID" OnClick="btnGetEmpID_Click" /> </td> </tr> </table> </td> </tr> <tr id="trInsertMessage" runat="server"> <td> <table> <tr> <td colspan="2"> <h2> Add a Message</h2> </td> </tr> <tr> <td> Employee ID: </td> <td> <asp:Label ID="lblEmpID" runat="server" ForeColor="Blue" Font-Bold="true"></asp:Label> </td> </tr> <tr> <td> New Message: </td> <td> <asp:TextBox ID="txtNewMessage" runat="server"></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnAddMessage" runat="server" Text="Add Message" OnClick="btnAddMessage_Click" /> </td> </tr> <tr> <td colspan="2"> <asp:CheckBox ID="chkShowView" runat="server" Text="Show Message Button" OnCheckedChanged="chkShowView_CheckedChanged" AutoPostBack="True" /> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnViewMessages" runat="server" Text="View Your Messages" OnClick="btnViewMessages_Click" /> </td> </tr> </table> </td> </tr> <tr id="trGetMyMessages" runat="server"> <td> <table> <tr> <td> <h2> Message List</h2> </td> </tr> <tr> <td> <asp:GridView ID="grdMessages" runat="server"> </asp:GridView> </td> </tr> <tr> <td> <asp:Button ID="btnAddAnother" runat="server" Text="Add Another Message" OnClick="btnAddAnother_Click" /> </td> </tr> </table> </td> </tr></table>
using System;using System.Data;using System.IO;public partial class Controls_ExternalTester : System.Web.UI.UserControl{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { resetForm(); } } private void resetForm() { trGetEmpID.Visible = true; trInsertMessage.Visible = false; trGetMyMessages.Visible = false; } private void resetAddMessage() { trGetEmpID.Visible = false; trInsertMessage.Visible = true; trGetMyMessages.Visible = false; lblEmpID.Text = ""; txtNewMessage.Text = ""; chkShowView.Checked = false; btnViewMessages.Visible = false; ExternalServiceTest.ExternalTest extTest = new ExternalServiceTest.ExternalTest(); int empID = extTest.GetEmpID("mlarsen"); lblEmpID.Text = empID.ToString(); } private void resetMessageList() { int empID = Convert.ToInt32(lblEmpID.Text); trGetEmpID.Visible = false; trInsertMessage.Visible = false; trGetMyMessages.Visible = true; ExternalServiceTest.ExternalTest extTest = new ExternalServiceTest.ExternalTest(); StringReader sr = new StringReader(extTest.GetMyMessages(empID)); DataSet dsMember = new DataSet(); dsMember.ReadXml(sr); grdMessages.DataSource = dsMember; grdMessages.DataBind(); } protected void btnGetEmpID_Click(object sender, EventArgs e) { resetAddMessage(); } protected void chkShowView_CheckedChanged(object sender, EventArgs e) { if (chkShowView.Checked) { btnViewMessages.Visible = true; } else { btnViewMessages.Visible = false; } } protected void btnAddMessage_Click(object sender, EventArgs e) { ExternalServiceTest.ExternalTest extTest = new ExternalServiceTest.ExternalTest(); if (extTest.InsertMessage(txtNewMessage.Text, lblEmpID.Text)) { txtNewMessage.Text = ""; } } protected void btnViewMessages_Click(object sender, EventArgs e) { resetMessageList(); } protected void btnAddAnother_Click(object sender, EventArgs e) { resetAddMessage(); }}