开发者

Content getting outside ContentPlace - ASP.NET

I'm on VS 2008, Windows Xp.

My page:

<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<%=  WriteReport()%> 
<%= "Hellooo" %> 
</asp:Content>

Code-behind:

public string WriteReport()
{

  StringWriter swriter = new StringWriter();
  using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
  {
      //Write a table...
  }

  return swriter.ToString();
}

The funny thing is the "Heloo" goes in the contentPlace, but the method's return does not. It's place below the div of the ContentPlace (ends up in the footer div).

Thank you for your help.

EDIT----------------------

Temporary solution

<%= WriteReport()%>
<asp:Table runat="server" ID="testeme">
 </asp:Table>

I added a bogus table (id=testeme), it does nothing and has nothing in it. But it works now, what the hell. Only works when below the method.

EDIT2--------------------

I tried to guess the relevant parts from the MasterPage and placed them here.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyAppV2.Views.MasterPage" %>


    <div id="general">


    <form id="form1" runat="server" enctype="multipart/form-data">
    <div id="main">

                <div>

                    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
                    </asp:contentplaceholder>

    开发者_JS百科            </div>


            </div>

    </form>

</div>  

    <div id="footer">

            <p>MYFOOTER</p>

    </div>

First line of the view using the master page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MP5._Default" MasterPageFile="~/MasterPage.Master" %>


Instead of injecting code into your page using <%= ... %>, you can use an <asp:literal> tag:

<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <asp:Literal id="MyReport" runat="server" />
</asp:Content>

Then you can write to it from your code-behind using the text property. You can call the WriteReport() method from within Page_Load.

public void WriteReport()
{

  StringWriter swriter = new StringWriter();
  using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
  {
      //Write a table...
  }

  MyReport.Text = swriter.ToString();
}

That should solve any oddities of where the code ends up on the page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜