使用Web服务动态添加用户控件

时间:2011-12-13 21:41:40

标签: web-services dynamic

我正面临有关用户控制与Web服务的问题,请帮助我 我做了一个接口调用IcustomeClass,它有一个名为GetHtml的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ICustomeClass
/// </summary>
public interface ICustomeClass
{
    string GetHtml(string controlLocation);
}

我有一个名为ControlBaseClass的类,它继承自UserControl和ICustomeClass

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.HtmlControls;


/// <summary>
/// Summary description for ControlBaseClass
/// </summary>
public class ControlBaseClass : UserControl, ICustomeClass
{
    public ControlBaseClass()
    {

    }

    public string GetHtml(string controlLocation)
    {
        return GetControlHtml(controlLocation);
    }

    private string GetControlHtml(string control)
    {
        var controlMarkup = string.Empty;
        Page page = new Page();
        var customControl = page.LoadControl(control) as UserControl;
        if (customControl != null)
        {
            var htmlForm = new HtmlForm(); 
            var output = new StringWriter();
            htmlForm.Controls.Add(customControl);
            page.Controls.Add(htmlForm);
            HttpContext.Current.Server.Execute(page, output, false);
            controlMarkup = output.ToString();
        }
        return controlMarkup;
    }
}

我有一个名为ControlService的服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for LoadControlService
/// </summary>
[WebService(Namespace = "http://tempuri.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 LoadControlService : System.Web.Services.WebService
{

    public LoadControlService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string GetControlHtml(string controlLocation)
    {
        ControlBaseClass controlBase = new ControlBaseClass();
        return controlBase.GetHtml(controlLocation);
    }

}

我在我的页面上使用AJAX Script Manager

<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/Services/LoadControlService.asmx" />
            </Services>
            <CompositeScript>
                <Scripts>
                    <asp:ScriptReference Path="~/Scripts/ControlScript.js" />
                </Scripts>
            </CompositeScript>
        </asp:ScriptManager>
        <table>
            <tr>
                <td>
                    <input id="AddBrandButton" type="button" value="Brand" onclick="GetControl('~/UserControls/AddMovies.ascx')" />
                </td>
                <td>
                    <input id="AddConsoleButton" type="button" value="Console" onclick="GetControl('~/UserControls/AddLog.ascx')" />
                </td>
            </tr>
        </table>
        <div id="ResultDiv">
        </div>
    </div>
    </form>
</body>

我有一个java脚本代码,它使用Web服务来获取用户控件html

/// <reference path="../MainPage.aspx" />
/// <reference name="MicrosoftAjax.js"/>


function GetControl(control) {
    LoadControlService.GetControlHtml(control, ControlSuccess, ControlFailure, "");
}

function ControlSuccess(result) {
    var div = $get('ResultDiv');
    div.innerHTML = result;

}
function ControlFailure(error) {
    alert(error);

} 

我的页面上有两个按钮,名为添加电影和添加日志当我点击按钮时webservice返回用户控件的html并且它显示在页面上它的工作正常我的问题是当我点击第一个按钮时用户控件出现,我在文本框中添加数据,但是当我单击第二个按钮时,会出现另一个用户控件,并且先前的用户控件将消失。

我想保存以前控制文本框的数据,以便我可以一键保存数据库中的所有数据,换句话说,我正在制作一个标签控件,只需点击一下即可保存所有数据。 / p>

0 个答案:

没有答案