开发者

How to output results in color to a web page? (Response.Write alternative?)

开发者 https://www.devze.com 2023-02-11 11:14 出处:网络
I am converting an old script from ASP to ASP.NET and would like some advice.The original ASP script used Response.Write to output information about what happened during execution.I have rewritten the

I am converting an old script from ASP to ASP.NET and would like some advice. The original ASP script used Response.Write to output information about what happened during execution. I have rewritten the entire thing in ASP.NET but it is new to me as an old-school C programmer. The job requirements include using the VB flavor of ASP.NET, btw.

I originally put up a TextBox and edited the text property to dump my final report. Now they want different colors for different message importance and I find that the TextBox can only do one color for all lines. I fell back to the old standby R.W but I get a message that it's not declared and from looking around I see that it's an issue because I'm calling it from the code behind and that is 'out of scope' from the HTML elements of the page itself.

My que开发者_Go百科stion is what is the best way to output information to the web page with different lines being different colors from a page's code-behind?

Secondary question - if I have misunderstood anything feel free to correct my thinking. :)

Thanks!


Literal is the correct approach in my opinion too, to prevent messiness you can wrap it nicely with some functions.

First, have one literal control that will be the messages placeholder:

<asp:Literal id="litMessages" runat="server" />

Second, use CSS for the colors it's more elegant and flexible.. for example:

<style type="text/css">
.msg_information { color: blue; }
.msg_error { color: red; }
.msg_warning { color: purple; }
</style>

Now have this function:

void AddMessage(string message, string type)
{
   string strHTML = string.Format("<div class=\"msg_{0}\">{1}</div>", type, message);
   litMessages.Text += strHTML;
}

And finally to add message have such code anywhere in your code:

AddMessage("method started successfully", "information");
AddMessage("Failed to do this: " + someErrorMessage, "error");
AddMessage("Value is empty", "warning");

You can make it even more elegant by using enum for message type and more, but the above is enough for basic needs. :)

By having each message in its own <div> you make it be in its own line automatically and you can control each message easier with CSS.

Edit: my code is C# but can be converted easily to VB.NET as well.


Why not just wrap your Response.Write in a span with a color attribute based on what you are outputting? e.g.

Response.Write(String.Format(@"<span style=""color: #{0}"">{1}</span>", System.Drawing.ColorTranslator.ToHtml(Color.Red),yourMessage))

To make it simpler, you could then do something like this:

private void ReponseWriteEx(string output, Color color)
{
    Response.Write(String.Format(@"<span style=""color: #{0}"">{1}</span>", System.Drawing.ColorTranslator.ToHtml(color),output))
}

And then call it, e.g. ResponseWriteEx("Hello World!",Color.Blue);


@Deverill,

Could you use a grid view and each row is a message? That way you can apply colors to the grid view.


I am not sure why you were using a text box and not a label.
Wince you were using Response.Write i am going to assume that you do not need user input. your best bet here is the Literal control.
LiteralControl Class Unlike a label (or textbox) it can contain any html. This will let you place spans with classes around any text that you need. You may have to set some options on the literal to allow html

0

精彩评论

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