如何多次使用部分代码

时间:2013-06-06 22:06:01

标签: asp.net methods code-behind

我是编程新手,但我会首先展示代码然后解释我想要做什么

private void ModalBody()
{
    LoadTemplateOverride();
    divModalBody.InnerHtml = 
}
private void LoadTemplateOverride()
{
    string templateURL = String.Format("{0}/leadgen/eodemails/templatecontroller.aspx?eodid={1}", site.Get("InternalApiUrl"), Email.ID);
    HttpWebRequest request = HttpWebRequest.Create(templateURL) as HttpWebRequest;

    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

    tbSubject.Text = response.Headers["EmailSubject"];
    tbBody.Text = (new StreamReader(response.GetResponseStream())).ReadToEnd();
}

所以我要做的就是将“LoadTemplateOverride”调用到ModalBody,然后从LoadTemplateOverride使ModalBody中的divModalBody.InnerHtml =等于(new StreamReader(response.GetResponseStream())).ReadToEnd();。但无论何时,我都不承认“回应”......

1 个答案:

答案 0 :(得分:2)

我对你要做的事情感到非常困惑,但有一个问题很明显,那就是你似乎没有意识到LoadTemplateOverride()中的“响应”实例你正试图来自ModalBody()的调用对于找到它的方法是私有的,并且ModalBody()无法访问。

不仅如此,一旦LoadTemplateOverride()完成执行,在其中声明的对象不再存在用于任何实际目的。如果您尝试将divModalBody.InnerHtml的值设置为LoadTemplateOverride()执行时出现的响应实例的值,那么我建议您使LoadTemplateOverride()不返回void,而是返回HttpWebsponse。然后,您对LoadTemplateOverride()的调用可以与将divModalBody.InnerHtml的值设置为它返回的值相结合。例如:

private void ModalBody()
{
    divModalBody.InnerHtml = LoadTemplateOverride();
}
private HttpWebResponse LoadTemplateOverride()
{
    string templateURL = String.Format("{0}/leadgen/eodemails/templatecontroller.aspx?eodid={1}", site.Get("InternalApiUrl"), Email.ID);
    HttpWebRequest request = HttpWebRequest.Create(templateURL) as HttpWebRequest;

    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

    tbSubject.Text = response.Headers["EmailSubject"];
    tbBody.Text = (new StreamReader(response.GetResponseStream())).ReadToEnd();

    return response;
}