add div to page from the code behind Asp.net/C# - c#

Hello I have a item list that shows on my asp.net page like this:
<div class="search-result-item">
<div class="image">
<img src="images/1.png"/>
</div>
<div class="logo">
<img src="images/logo.png"/>
</div>
<div class="header">
Title
</div>
<div class="message">
Message
</div>
<div class="keywords">
key1 | key2 | key3
</div>
<div class="links">
Go to
</div>
</div>
This shows a predefined layout for an item on the page.
Items are generated in the code behind.
I want for each item to create a div like the one above on the page.
How can I do that using the code behind?
Thanks a lot

You create a Control and add it to your page.
There are a large range of controls, but if you want that exact markup, and don't need any server-side processing, the LiteralControl control is probably the most appropriate.

Use a repeater control You create a control that basically connects to datasource and then repeats info based on the data. Very crude description sorry. Look at it on Google.

Related

C# Group blocks beetwen divs

I getting source code from some website.
Code is like this:
<div class="block">
<div class="blocktext">Bla</div>
</div>
<div class="block">
<div class="blocktext">Bla</div>
</div>
How to group blocks and view text from blocktext?
Example:
var blocks[] = list all "div block";
blocks[1] = HERE IS TEXT from blocktext
Thanks. I need to make a program that downloads the ads from the pages
Have a look at this: https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx
and then, when you're mastering C#, this: https://htmlagilitypack.codeplex.com/

Left align text but right align button inside of a panel using Bootstrap and ASP.NET

I am writing my first web application using Twitter Bootstrap and ASP.NET with C# on the back-end. I have buttons and labels inside of a Bootstrap panel. I would like to align the labels to the left and the buttons to the right. Here is my code so far:
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Overview</h3>
</div>
<div class="panel body">
<p>
<asp:Label ID="BandsProducedLabel" runat="server" Text="Bands Produced:"></asp:Label>
<asp:Button class="btn btn-default" ID="BandsProducedBtn" runat="server" Text="Hello" style="text-align:right" />
</p>
<\div>
<\div>
How can I accomplish this task? This is only a small snippet of code. I have about 15 other panels that I would like to apply the same styling to.
If you want to use standard Bootstrap styles, you can give the label the CSS class "pull-left" and the button "pull-right".
For the correct result, you might have to reverse the source order, that is, have the button come first and then the label, in the markup.
Another option would be to use the Bootstrap grid system to place these two elements side by side.

dynamically add html tag in asp.net

I need to add html tags dynamically in asp.net code behind , here is the scenario :
List<Product> products = ProductManager.GetProductList();//read data from database
Product is a class containing information I need to show in website such as product name, image,… .
For adding html code I tried this to show 5 products per page :
String finalHtmlCodeContainer="";
for(int i=0;i<=5;i++)
{
String myBox= "<div class=\"box\"> </div>";
finalHtmlCodeContainer+=mybox;
}
boxWrapper.InnerHtml=finalHtmlCodeContainer;
boxWrapper is a div that would contain our 5 product info.up to now everything is ok but problem appears when insted of "<div class=\"box\"> </div>",myBoxcontains long characters of html code , the original myBox should include this:
<div class="boxWrapper">
<div class="box">
<div class="rightHeader">rightHeader</div>
<div class="leftHeader">leftHeader</div>
<div class="article">
<img src="../Image/cinemaHeader.jpg" />
<p>
some text here <!--product.content-->
</p>
</div><!--end of article-->
<div class="rightFooter"><span>rightFooter</span>
<div class="info">
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
</div>
</div><!--end of rightFooter-->
<div class="leftFooter"><span>leftFooter</span>
</div><!--end of leftFooter-->
</div><!--end of box-->
</div><!--end of boxWrapper-->
you see if i add producet.atrributes to code snippet above, it would be more messy,hard to debug , has less scalability and... . what is your solution?
It depends on the underlying product. If you are using web forms, I'd suggest using a data bound control that supports templating like the ListView or Repeater. It gives you full control over the UI, and also supports reloading the UI in ViewState. In your scenario, you'd have to build the UI on every postback. The ListView or Repeater track it in ViewState.
If you are using MVC, well using a helper method can make this easier as you can stick the item template within and then use a foreach and render out the helper.
If you are talking doing it in JavaScript, then there are templating components for doing the same thing.
I suggest you to Add repeater in your <p> tag ,
<p>
<asp:Repeater runat="server" ID="rpDetail">
<ItemTemplate>
<div class="box">
<%# Eval("YourDataField") %>
</div>
</ItemTemplate>
</asp:Repeater>
</p>
And make your products list as Repeater's datasourse in code behind .
rpDetail.DataSource = products ;
rpDetail.DataBind();

How to access HTML controls in source code

I have following code in my user control, and I want to access each control in this list either by accessing or tag.
<div class="menubarbackground">
<ul>
<li id="first" class="heigfbtitle">
1 First Item
</li>
</ul>
</div>
How do I implement the code to access these control in code behind?

Dynamically adding menu items randomly disappear in ASP .net

I have a header control which is included in every page.
I create dynamic menu in Header control based on user rights and modules provided.
Menu is created in following string format.
Code Behind
`
string dynamicMenu =
<ul>
<li>
<a href='/User/HomePage' runat='server' id='HOME'>Home</a>
</li>
<li>
<a href='/User/Files' runat='server' id='MyFiles'>My Files</a>
</li>
</ul>;
divMenu.InnerHtml = dynamicMenu;
`
ASPX
<div id="divMenu" runat="server"></div>
Problem is that, some times menu disappears and rendered menu div has only <ul/> tag and l
looks like
<div id="divMenu" runat="server"> <ul/> </div>
How can I resolve this issue.
replace <div id="divMenu" runat="server"></div> with panel in your aspx
in code behind
string dynamicMenu = "<ul><li><a href='/User/HomePage' runat='server' id='HOME'>Home</a></li><li><a href='/User/Files' runat='server' id='MyFiles'>My Files</a></li></ul>";
Panel1.Controls.Add(new LiteralControl(dynamicMenu));

Categories

Resources