且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LoadControl,用户控件中的WebMethod

更新时间:2023-12-06 10:46:10

 用户控件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");