If statement in HTML - html

Hello i have list of products this my html
<telerik:RadListBox ID="RadListBox1" runat="server" DataSourceID="LinqDataSourceCategories" Height="200px" Width="250px" CssClass="ui-droppable" >
<EmptyMessageTemplate>
No records to display.
</EmptyMessageTemplate>
<ItemTemplate >
<div class="ui-draggable ui-state-default" data-shortid="<%#Eval("ID")%>">
<em>Active : </em><span><%# Eval("LoadProduct") %></span><br />
<em>ProductId: </em><span><%# Eval("ProductId") %></span><br />
</div>
</ItemTemplate>
</telerik:RadListBox>
I have Active that is say if LoadProduct true or false
in client it look like this
<em>Acrive : </em>
<span>False</span>
<br>
<em>ProductId: </em>
<span>101-01-056-02</span>
<br>
<em>ShortID: </em>
<span class="ShortID" data-shortid="0">0</span>
<br>
I want to replace text with img ,i need to check if <%# Eval("LoadProduct") %> ==true put img scr=/green.png/ else img scr=/red.png/ that clien will look like this
<em>Acrive : </em>
<span><img src='green.jpg'/></span>
<br>
<em>ProductId: </em>
<span>101-01-056-02</span>
<br>
<em>ShortID: </em>
<span class="ShortID" data-shortid="0">0</span>
<br>
Sow how it can be done?
To add if statement to HTML,
or to catch event that building all elements and there check if LoadProduct==true and append html to item?

try this
<em>Active : </em><span><%# Convert.ToBoolean(Eval("LoadProduct")) ? "<img src='green.jpg'/>" : "<img src='red.jpg'/>"%></span><br />

Come to think of it, I don't really like the idea of back-end conditionals in front-end.
Code-behind is one thing and frontside is the other - it's not a good idea to mix it if it isn't inevitable.
The purest way to get that would be to set the variable (or a function, in your case) in code-behind and just to show it in front-end, like:
Code behind:
protected string GreenOrRed(bool isLoadProduct)
{
return isLoadProduct ? "green" : "red";
}
(the function or variable has to be at least protected in order to be accessible in aspx page, because the aspx page is the derivative from base class of aspx.cs )
front-end:
<span><img src='<%# GreenOrRed((bool)Eval("LoadProduct")) %>.jpg'/></span>
After that, remeber to add
this.DataBind();
in your Page_Load() function.
Let the code-behind decide, let the front-end only show the result.

<%
if((bool)Eval("LoadProduct") == true)
{
Response.Write("src='green.jpg'");
}
else Response.Write("src='red.jpg'");
%>

Try this
<img src='<%# (bool)Eval("LoadProduct") ? "green.jpg" : "red.jpg" %>'/>

Related

Why erased code (OnClick event) is still running?

This little html code (my first html) ask an integer between 1 and 20 and makes a multiplication table. The multiTable is put in an aspx.cs file together. I got a plus task to change mainheader color using javascriptcode (written in script area) by OnCLientClick event. To try whether the coloring javascript function works I temporary deleted OnCLick event connected to Page_Load event. As you can see in the code asp:Button (ID=ButtonCreateMultiplicationTable) hasn't got OnClick event. But it is still functioning. Examin the input and if it is correct makes the table. How it is possible? The Code is not exist. I used: Rebuild Solution, Clean Solution, Restarted VisualStudio, Disabled cache in Chrome Developer Tools. Thank you for any help.
<div id="Date" class="dateTime"><%= (DateTime.Now.Date).ToString("yyyy/MM/dd") + " " + HungarianWeekName((DateTime.Now.DayOfWeek).ToString()).ToString() %> </div>
<div id="mainheader" class="header">
<p class="header1">Szorzótábla </p>
</div>
<div id="InputContainer" class="inputcontainer">
&nbsp
<div id="InputText2" class="inputitem1">Adjon meg egy számot 0 és 21 között </div>
&nbsp
<asp:TextBox class="inputitem2" runat="server" ID="TextBox1"></asp:TextBox>
&nbsp
<asp:Button class="inputitem3" runat="server" Text="Készít" OnClientClick="return ChangeColor()" ID="ButtonCreateMultiplicationTable" />
&nbsp
</div>
<div class="multiplicationtable" id="multitablediv">
<div class="multiplicationcontainer">
<asp:Literal ID="DynamicTable" runat="server"></asp:Literal>
</div>
</div>
An asp:button will always perform a PostBack, whether or not there is an OnClick event.
You either need to return false from the ChangeColor function.
function ChangeColor() {
//rest of your js code
return false;
}
Or don't use an asp:button
<button class="inputitem3" type="button" onclick="ChangeColor()" id="ButtonCreateMultiplicationTable">
Készít
</button>

Replacing span element text with color

I have below span tag generated by custom tool (I can't ask it to generate in different way)
<span tabindex="0" > FINDME </span>
I want to write a script section within and to find the text "FINDME" and replace this with :
<span tabindex="0" style="color:red">FINDME</span>
or
<span tabindex="0">
<font color="red"> FINDME </font>
</span>
Basically I want to get the text colored. Also since I would have multiple span element coming that way so I had to search by text before replacing it.
I don't know how to code it so any help will be appreciated.
thanks !
This code will search for all the spans in your code and the one that have the text " FINDME " will get the color replaced. This solution is using jQuery
$(document).ready(function(){
$("span").each(function(){
if($(this).text() == ' FINDME '){
$(this).css('color','red');
}
});
});
https://jsfiddle.net/wearetamo/mdwkakzz/
The answer is :
<script> var span3 = document.querySelector('#dashboard_page_3_tab span'); span3.innerHTML = '<font color="red"> FINDME </font>' ; </script>

.NET and SQL: Do I need to build a custom control?

I'm working off of the default Web Forms template in VS 2013, and one of the rows has 3 divs:
<div class="col-md-4">
<h2>Get Paid</h2>
<img src="Images/adp_logo.gif" class="img-responsive" alt="ADP" />
<p>
Everybody wants to get paid! Just click on the "ADP" link to monitor or change your timesheet, benefits
information, 401K contributions, etc...
</p>
<p>
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301948">Go! »</a>
</p>
</div>
<div class="col-md-4">
<h2>Get Help</h2>
<asp:ImageButton ID="imgBtn2" runat="server" ImageUrl="~/Images/user_help.png" Height="100" />
<p>
Don't know how to do something? We can help! Click the image above to find answers about how to get a badge,
share your Outlook calendar, or make a great cup of coffee (okay, maybe not the last one, but you get the idea...)
</p>
<p>
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301949">Go! »</a>
</p>
</div>
<div class="col-md-4">
<h2>Find the People You Need</h2>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/find_icon.png" Height="100" />
<p>
You can easily find information about anybody that works at ERC. Just click and go!
</p>
<p>
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301950">Go! »</a>
</p>
</div>
I'd like to be able to populate these divs with information from a database row. For example, in my database I'd have a table that had Title, Image, Description, Link and Category columns. If a row had a Category of "Featured", it would populate the first element with it's Title value, the tag with the image, etc.
Do I need to build a custom control to accomplish this, or am I just overlooking an obvious way of achieving this?
By the way, the Google Play store is what I'm modeling this after, if that helps...
Thanks...
Like David said, a .NET Repeater control would be ideal for what you want to achieve. You would add your html template once and it would, well, repeat it according to your database query and the number of entries that it would extract.
Html. Note that single quotes ARE REQUIRED when binding data to an attribute like 'src', 'href', etc:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<div class="col-md-4">
<h2><%# Eval("Title") %></h2>
<img src='<%# Eval("Image") %>' class="img-responsive" alt="ADP" />
<p>
<%# Eval("Description") %>
</p>
<p>
<a class="btn btn-default" href='<%# Eval("Link") %>'>Go! »</a>
</p>
</div>
</ItemTemplate>
</asp:Repeater>
Code-behind C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
bindRepeater();
}
}
protected void bindRepeater() {
DatabaseEntities db = new DatabaseEntities();
var query = (from q in db.myTableName
where q.Category == "Featured"
select q);
if (query.Count() > 0)
{
myRepeater.DataSource = query.ToArray();
myRepeater.DataBind();
}
}

While Loop issue when querying Database in JSP

I am having trouble understanding why my codes does not work as I had thought. I am having issue in having my input text value to be that of my resultset value. Let me apologise for my bad explanation. Before I continue, I would like to mention that I am using JSP to run my application.
Say in my database, I have the following names, "Peter", "John" , "Simon" and when I queried, I will store their names in a hidden input text, as the names will be used for other usage. But I am facing the issue where by only "PETER" name is registered in the input text, as Peter is the first entry in the database.
Below is an abstrace of my while loop. If anyone know whats the underlying cause please let me know. Thanks in advance guys.
<%while (rs.next()) { %>
<tr><td>
<img alt="logo" src= "<%= rs.getString("ItemImage")%>" width="600" height = "400"/><button type="button" onclick="initialize2()" width="600" >Location</button>
</td>
<td>
<input type = 'hidden' id = 'itemName' value = "<%=rs.getString("ItemName")%>" />
<input type = "hidden" id = "address" value = "<%= rs.getString("ItemLocation")%>" />
<div id="<%= rs.getString("ItemName")%>" style="width: 600px; height: 400px;"></div>
</td>
</tr>
<% } %>
You are repeating the same id over and over which registers only one input text.
Try, for example:
<% string name = "itemName" + counter; %>
<input type='hidden' name="<%=name%>" value="<%=rs.getString("ItemName")%>" />

Extract Specific Text from Html Page using htmlagilitypack

Hey most of my issue has been solved but i have little problem
This is Html
<tr>
<td class="ttl">
</td>
<td class="nfo">- MP4/H.263/H.264/WMV player<br />
- MP3/WAV/еAAC+/WMA player<br />
- Photo editor<br />
- Organizer<br />
- Voice command/dial<br />
- Flash Lite 3.0<br />
- T9</td>
</tr>
Currently i am using this code provided by Stackoverflow User
var text1 = htmlDoc.DocumentNode.SelectNodes("//td[#class='nfo']")[1].InnerHtml;
textBox1.Text = text1;
know problem its is getting all text
with <br>
how i can remove <br> from it and put , between them
its should look like this
MP4/H.263/H.264/WMV player,- MP3/WAV/еAAC+/WMA player,- Photo editor,- Organizer,- Voice command/dial,- Flash Lite 3.0,- T9
Also how to get this
<div id="ttl" class="brand">
<h1>Nokia C5-03</h1>
<p><img src="http://img.gsmarena.com/vv/logos/lg_nokia.gif" alt="Nokia" /></p>
</div>
i am trying this
var text41 =
htmlDoc.DocumentNode.SelectNodes("//div
id[#class='brand']")[0].InnerText;
i get invalid token error
i only want C5-03 without nokia text
You can simply use a string.Replace("<br />", ""); to remove the <br /> tags.
Better yet, use the InnerText instead of InnerHtml, so no HTML comes through:
var text1 = htmlDoc.DocumentNode.SelectNodes("//td[#class='nfo']")[1].InnerText;
If you really want to replace all <br /> tags with a , you will indeed need to use Replace:
text1.Replace("<br />", ",");
To select the value in the <H1> tag, you could use:
var text42 = htmlDoc.DocumentNode.SelectNodes("//div[id='ttl']"/h1)[0].InnerText;