程序员人生 网站导航

用AJAX的方式执行ASP.NET中的用户控件

栏目:综合技术时间:2013-10-23 00:50:29

【建站学院文档】前些日子无意中看到<<使用User Control做HTML生成>>这篇文章,感觉这样很不错,在以后的代码中可以大量的使用这种技巧。

以下为引用的内容:

Web Service:

/// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string ExecuteControl()
        {
            var page = new XPage();

            var url = "~/WebUserControl1.ascx";

            var ctr = page.LoadControl(url);
            page.Controls.Add(ctr);

            StringWriter output = new StringWriter();

            HttpContext.Current.Server.Execute(page,output,false);

            return output.ToString();
        }
    }

    public class XPage : Page
    {
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
    }

 

client:

<input type="button" value ="click" onclick="asdfasdf();" />
    <script language="javascript" type="text/javascript">
        function asdfasdf() {
            WebApplication10.WebService1.ExecuteControl(ok);

        }
        function ok(res) {
            $get("kkk").innerHTML = res;
        }
    </script>

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐