0

So, I am building a blog that will sometimes include code for demonstrating basic number theory algorithms, and sometimes images of different food dishes. I am using ASP.Net Web Pages. I am stuck on figuring out how to display blog as html, because the order of writing, code and images will vary. For instance, do we store the blog complete with tags and read directly into: <%=GetBlog()%> or store the blog with home made tags and parse them, then create some kind of loop using: <%...%> I really like the format of this site where we can enter writing

and code and images Is there a pattern that is ideal for achieving what I have described?

2
  • With Webforms and Asp.Net, typically you populate a data source with data in the code behind (e.g. in C#) and pass it to a server side Asp.Net control that renders Html. Commented Dec 17, 2017 at 16:31
  • Great, derloopkat. I have the data stored in a class, but I want to display <p>, <code> and <img> tags from class Blog separately and dynamically. So, how do I create the Asp.Net control dynamically? Maybe that is what I am asking, Commented Dec 17, 2017 at 16:36

1 Answer 1

1

For example in your C# code behind:

protected void Page_Load(object sender, EventArgs e)
{
    this.Literal1.Text = "my paragraph here";
    this.Image1.ImageUrl = "http://simpleicon.com/wp-content/uploads/smile.png";
    this.Image1.Height = 20;
    this.MyCodeControl.InnerText = "my code here";
}

In your .aspx file you have:

<p><asp:Literal ID="Literal1" runat="server"></asp:Literal></p>
<asp:Image ID="Image1" runat="server" />
<code id="MyCodeControl" runat="server"> </code>

Since an Internet browser doesn't understand Asp nor C#, this code runs at server and becomes into this Html:

<p>my paragraph here</p>
<img id="Image1" 
     src="http://simpleicon.com/wp-content/uploads/smile.png"
     style="height:20px;" />
<code id="MyCodeControl">my code here</code>

I don't know what server side control generates code and p Html tags, so I have added ordinary Html tags with runtat="server" attribute. This allows me to access to the control in C# code behind.

Also check out how to use more complex controls like Repeater and ListView.

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

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.