5 Replies - 4951 Views - Last Post: 18 December 2012 - 06:27 AM Rate Topic: -----

#1 martin257  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-October 12

Would you write html from the server like this?

Posted 17 December 2012 - 05:22 AM

I am trying to write html using c# because i want a dynamic nav menu and i am just not sure that the following solution looks great. I am just wandering if there is a better solution or just any opinions about the code.

if (auth_nav == "admin")
            {
                Label1.Text = "<nav><ul><li><a href='products.aspx'>Add products</a></li><li><a href='login.aspx'>Edit/delete products</a></li></ul></nav>";
}


<asp:Label ID="Label1" runat="server"></asp:Label>


Thoughts?

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Would you write html from the server like this?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Would you write html from the server like this?

Posted 17 December 2012 - 05:57 AM

Consider using the HtmlTextWriter and its descendant classes.
http://msdn.microsof...textwriter.aspx

You didn't mention if you were writing code as WinForms, ASP.NET, or WPF, although the last bit of code you posted hints at ASP.NET. If it is ASP.NET, you can have your own custom control and override the Render() method to output your desired HTML.
http://msdn.microsof...6(v=vs.71).aspx
Was This Post Helpful? 0
  • +
  • -

#3 martin257  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-October 12

Re: Would you write html from the server like this?

Posted 17 December 2012 - 06:08 AM

View PostSkydiver, on 17 December 2012 - 05:57 AM, said:

Consider using the HtmlTextWriter and its descendant classes.
http://msdn.microsof...textwriter.aspx

You didn't mention if you were writing code as WinForms, ASP.NET, or WPF, although the last bit of code you posted hints at ASP.NET. If it is ASP.NET, you can have your own custom control and override the Render() method to output your desired HTML.
http://msdn.microsof...6(v=vs.71).aspx


Yes it is. I will give it a go thanks.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: Would you write html from the server like this?

Posted 17 December 2012 - 06:30 AM

Your HTML isn't changing, just it's visibility.

When you can, make the markup part of the form do the work for you. e.g.
<asp:PlaceHolder id="AdminNav" runat="server"/>
	<nav>
		<ul>
			<li><a href='products.aspx'>Add products</a></li>
			<li><a href='login.aspx'>Edit/delete products</a></li>
		</ul>
	</nav>
</asp:PlaceHolder>



Then, usually on page load:
AdminNav1.Visible = (auth_nav == "admin");



If you want to use your approach, still prefer placeholder to label.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Would you write html from the server like this?

Posted 17 December 2012 - 07:04 AM

Moved to ASP.NET
Was This Post Helpful? 0
  • +
  • -

#6 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Would you write html from the server like this?

Posted 18 December 2012 - 06:27 AM

I generally wouldn't, I like to keep my html separate from my code. You could do as baavgai suggests or you could move the html into a user control and insert that into a placeholder control if the condition is met.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1