LoadControl,用户控件中的WebMethod [英] LoadControl, usercontrol in WebMethod

查看:221
本文介绍了LoadControl,用户控件中的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过的WebMethod 下面是code我通常定期回发

使用发送电子邮件

  [的WebMethod]
公共静态无效SendEmail(字符串名称,手机串,串EMAILADDRESS,串兴趣,字符串评论)
{
    无功控制= LoadControl(〜/共享/控制/ EmailTemplate_PartnerWithUs.ascx);    StringBuilder的SB =新的StringBuilder();
    StringWriter的SW =新的StringWriter(某人);
    Html32TextWriter HTW =新Html32TextWriter(SW);    control.RenderControl(HTW);    sb.Replace(%名称%,名);
    sb.Replace(%电话%,电话);
    sb.Replace(%EMAILADDRESS%,EMAILADDRESS);
    sb.Replace(%权益%,利息);
    sb.Replace(%注释%,评论);
    EmailUtility.SendEmailMessage(SettingsManager.GetContactRecipientEmails(),CoyleHomeBuyers.com合作伙伴形式,sb.ToString());
}

我得到的错误是:

 的对象引用是必需的非静态字段,方法或属性System.Web.UI.TemplateControl.LoadControl(字符串)

有没有办法来加载的WebMethod内这种控制?


解决方案

 用户控件UC =新用户控件();
控制控制= uc.LoadControl(〜/共享/控制/ EmailTemplate_PartnerWithUs.ascx);

I am trying to send an email through a WebMethod Below is the code I typically use on a regular post back

[WebMethod]
public static void SendEmail(string name, string phone, string emailaddress, string interest, string comments)
{
    var control = LoadControl("~/Shared/Controls/EmailTemplate_PartnerWithUs.ascx");

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    Html32TextWriter htw = new Html32TextWriter(sw);

    control.RenderControl(htw);

    sb.Replace("%name%", name);
    sb.Replace("%phone%", phone);
    sb.Replace("%emailaddress%", emailaddress);
    sb.Replace("%interest%", interest);
    sb.Replace("%comments%", comments);
    EmailUtility.SendEmailMessage(SettingsManager.GetContactRecipientEmails(), "CoyleHomeBuyers.com Partner Form", sb.ToString());
}

The error I'm getting is:

An object reference is required for the non-static field, method, or property 'System.Web.UI.TemplateControl.LoadControl(string)'

Is there a way to load this control within the WebMethod?

解决方案

UserControl uc= new UserControl();
Control control = uc.LoadControl("~/Shared/Controls/EmailTemplate_PartnerWithUs.ascx");

这篇关于LoadControl,用户控件中的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆