c# – WebConxt中的LoadControl,usercontrol

前端之家收集整理的这篇文章主要介绍了c# – WebConxt中的LoadControl,usercontrol前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过WebMethod发送电子邮件下面是我通常在常规帖子上使用的代码
[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());
}

我得到的错误是:

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

有没有办法在WebMethod中加载此控件?

解决方法

UserControl uc= new UserControl();
Control control = uc.LoadControl("~/Shared/Controls/EmailTemplate_PartnerWithUs.ascx");
原文链接:https://www.f2er.com/csharp/244550.html

猜你在找的C#相关文章