2

I am having issues with the output of my C# script embedded in my asp.net code. The output is generated after clicking a submit button for a web form. This web form is at the top of the page. The output, when clicking submit, is currently being placed above the web form which is in turn pushing the web form underneath it. I would like the opposite to happen. I want it to output below my web form. The way I generate output from my script is as follows:

Response.Write("<p>");
foreach(obj in arr){
    Response.Write(obj);
}
Response.Write("</p>");

Also if it matters, I initialize the script with runat="server". The script gets called when the user selects "submit" near the web form. Thanks in advance. I've been trying to format this thing for quite some time now.

4
  • So you want to append a paragraph after </html> tag closing the output page? Commented Dec 29, 2011 at 20:58
  • Please add the rest of the code in your page to your question, or at least the form part so it can be compared to your display code snippet. Commented Dec 29, 2011 at 20:59
  • @WiktorZychla yes, I suppose that would do the trick. Commented Dec 29, 2011 at 21:01
  • @Abbas I cannot get the text box to show html code for some reason... but the form stuff is written after the script tag ends, and the form is written in html. The submit button is asp:button which runs the script when pressed. Commented Dec 29, 2011 at 21:11

2 Answers 2

5

You would be better off putting a 'literal' object in the place on your page precisely where where you want the result to appear, and then, instead of spitting out HTML with response.write, you assign the desired text to the literal in your code-behind.

Like this:

<html>

<p>
  <asp:Literal ID="ltlTest" runat="server"></asp:Literal>
</p>

</html>

and then in your code behind:

ltlTest.Text = "the string you want to show...";

You can include html tags in the string assignment, though generally I try not to.

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

3 Comments

Ah, can't wait to try it out. However, my aspx page still needs a code-behind page haha. Never learned that part yet, I've been doing all the coding within the one aspx file. Thanks for the reply!
You don't need a code-behind, you can still do it in the same page - I just assumed you were using one.
I finally got a chance to implement this today. It's worked out perfectly!
1

You've got some choices.

  1. You can make arr a public property, and then use <% foreach (var obj in arr) Response.Write(obj); %> directly in the page markup where you want it.

  2. You can put in an <asp:Literal runat="server" ID="Literal1"> control and then set Literal1.Text = ... in your code. This achieves the same, but with ViewState (so the value is persisted on postbacks).

  3. If you'd like the result to be rendered within <span /> tags, you can use an <asp:Label /> control. This is usually the best choice for displaying messages to the user.

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.