Response.Write in particular section of Markup

Samarth Agarwal picture Samarth Agarwal · Jan 14, 2013 · Viewed 9.6k times · Source

I have a web application in ASP.NET, C# and MS SQL Server. I am using an external CSS Metro UI CSS that provides me styled components like Tiles (similar to Win8), notices, buttons and so on. I want to show content fetched from the Database or calculated data in those styles DIVs. But since they are not ASP.NET controls, they cannot be bound to data sources.

I tried using Response.Write("MY HTML CODE") to generate content dynamically and that works but its not perfect as the content is placed different from the markup. I actually don't know what is the difference but it does not fit well into my existing static page content. Is there a way to insert content into existing HTML using Response.Write()?

Right now I am trying this,

Response.Write("<div class='message-dialog bg-color-blue fg-color-white' style='width: 50%; z-index:1000;'><h1>Congratulations!<br/></h1><h3>You Account is created.Please Sign In.</h3><a href='Default.aspx'><button class='button-set active bg-color-red fg-color-white' style='float:right'> OK </button></a></div>");

And this shows a Styles Box with proper text but that appears on the top of my existing page with _z-index_ and behind page without it.

Answer

Blachshma picture Blachshma · Jan 14, 2013

Any control, even a specially styled div can be modified in the server-side by adding the runat="server" attribute to it.

So in your aspx, you can do something like this:

<div class='message-dialog bg-color-blue fg-color-white' style='width: 50%; z-index:1000;' runat="server" id="myDiv" />

And in code behind do whatever you want... For instance:

myDiv.Controls.Add(new Label() { Text = "Hello World" });

or even

 myDiv.InnerHtml = "<h1>Hello World</h1>";

Using Response.Write is probably the wrong way to go in this situation...