2

This question most likely has an easy solution, but I cannot figure it out. I have a .aspx page with the following code:

<%@ Page Title="Update ASV Information" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpdateASV.aspx.cs" Inherits="COAF_Process_to_ASV_Relation_Tool.UpdateASV" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<asp:Button runat="server" ID="asv_update" OnClick="asvUpdate_Click" Text="Update    ASV" />
</asp:Content>

I want to be able to write text from the C# code behind this page (UpdateASV.aspx.cs). Whenever I try:

Response.Write("some text");

It puts the code behind the content2 placeholder. I want it inside. Is there an easy way to do this?

3
  • 2
    Is there a reason not to use Label or Literal control? Are you trying to add text to Master page from ASPX page? Commented Jul 11, 2014 at 14:11
  • I basically have that placeholder producing a bunch of textboxes dynamically in the code behind, but I want to have breaks in between them, so I thought I would have to have those in the actual C# code. Commented Jul 11, 2014 at 14:18
  • Omg I got it, I was putting my label in the wrong method!! Commented Jul 11, 2014 at 14:23

2 Answers 2

2

Response.Write directly modifies the response. You don't want to do that in your web forms application. You should be modifying the contents of a Label, Literal, or PlaceHolder control from the code-behind.

Use Label to put text into the page

Use Literal to put raw html into the page

Use PlaceHolder to add new controls to the page dynamically.

Either way, the placement of the control(Label, Literal, or PlaceHolder) on your page determines where on the page your output will be rendered.

Sign up to request clarification or add additional context in comments.

1 Comment

And don't tell anyone I know about web forms stuff. They might make me work on a web forms app.
0

If you are determined to stick to server side controls:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostback)
    {
        Literal myText = new Literal() 
        { 
            Text = "some text" 
        };
        Content2.Controls.Add(myText);
    }
}

Otherwise, just put the code inline

<%=Response.Write("some text") %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.