ASP VB. NET Highlight Row to Red color - asp.net-mvc-4

I would like to select the row and highlight to Red if status equal to Offline.
How can i do it in ASP .NET?
I saw quite alot of example talking about protected void OnRowCreated(object sender, GridViewRowEventArgs e).
I should create the class in Controller? or View? Very confuse on the new MVC >_<
Please help

You should just reference the row values Online/Offline status from the model and change the CSS accordingly to highlight the row. Since no code has been provided I have mocked an example up:
I.e.
public class OnlineOfflineElements {
public List<Element> elements { get; set; }
}
public class Element {
public bool isOnline { get; set; }
}
Then in your View:
#foreach (var status in elements) {
if (status.isOnline) {
<tr class="Online">
} else {
<tr class="Offline">
}
// Other model content here
}

I tried the method CSS TD Class to red, but i could not get it work. Any idea?
My code as follow:-
#For Each item In Model
Dim currentItem = item
Dim status = Html.Action("showPing", New With {.ipaddress = currentItem.IP})
If status.Equals("Online") Then
#<tr>
<td>
#Html.DisplayFor(Function(modelItem) currentItem.Name)
</td>
<td>
#Html.DisplayFor(Function(modelItem) currentItem.IP)
</td>
<td>
#Html.DisplayFor(Function(modelItem) currentItem.Location)
</td>
<td>
#Html.Action("showPing", New With {.ipaddress = currentItem.IP})
</td>
</tr>
Else
#<tr>
<td class="red">
#Html.DisplayFor(Function(modelItem) currentItem.Name)
</td>
<td class="red">
#Html.DisplayFor(Function(modelItem) currentItem.IP)
</td>
<td class="red">
#Html.DisplayFor(Function(modelItem) currentItem.Location)
</td>
<td class="red">
#Html.Action("showPing", New With {.ipaddress = currentItem.IP})
</td>
</tr>
End If
Next
It showed all red. I am using ASP .NET VB MVC4

I have just been able to complete this for a gridview in my webpage. I needed to be able to change a row of data when offline to red.
Protected Sub grdName_RowDataBound(ByVal sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdName.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If DataBinder.Eval(e.Row.DataItem, "Status").ToString() = "OffLine" Then
e.Row.BackColor = System.Drawing.Color.Red
End If
End If
End Sub
Hope this helps.
Betty

Related

Spring WebFlux+Thymeleaf How to display reactive collection size

I am developing a spring boot service to manage a REST server.
The service displays a reactive list on one of the forms.
It is very simple code.
Table in thymeleaf template
<table>
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Alive</td>
</tr>
</thead>
<tbody>
<tr th:each="item : ${items}">
<td th:text="${item.counterpartID}" />
<td th:text="${item.counterpartName}" />
<td th:text="${item.alive}"/>
</tr>
</tbody>
</table>
Controller
#GetMapping("/counterparties")
public String init(Model model) {
IReactiveDataDriverContextVariable reactiveDataDrivenMode =
new ReactiveDataDriverContextVariable(
webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/findAllCounterpart")
.build())
.retrieve()
.bodyToFlux(Counterpart.class)
, 1);
model.addAttribute("items", reactiveDataDrivenMode);
return "counterparties.html";
}
Now I want to display collection size (numbers of rows in table).
I added to the html temlate tag
<label th:text="'total rows: ' + ${#lists.size(items)}">rows number in table</label>
And I got an unexpected result
total rows: 1
How to display the real number of rows in a reactive collection ?
Thanks.
I decided to use javascript.
<script language="JavaScript">
function countLoadedRows() {
var table = document.getElementById('tableId');
var tbodyRowCount = table.tBodies[0].rows.length;
console.log('total rows: ' + tbodyRowCount);
document.getElementById('totalRowsId').textContent = tbodyRowCount
}
document.addEventListener('readystatechange', countLoadedRows)
</script>
and html tags is
<input type = "button" onclick = "countLoadedRows()" value = "Total rows: "><label id="totalRowsId">???</label>
<table id="tableId">
...
</table>

How generate MVC View Controls dynamically?

Is possible to generate controls in View depending on data returned from DB
Example
Model
public string Type{ get; set; }
Controller
public ActionResult Index()
{
return View(db.TypeModel.ToList());
}
View
#model IEnumerable<Sample.Models.TypeModel>
#foreach (var item in Model)
{
<tr>
<td>
#*if Item in model is T generate text box, if C generate text..*#
</td>
</tr>
}
There are multiple solutions to this kind of problem, I personally think that you should take a look in EditorFor html helper.
This method generates different HTML markup depending on the data type
of the property that is being rendered, and according to whether the
property is marked with certain attributes. https://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor%28v=vs.118%29.aspx?f=255&MSPPError=-2147217396
Your code can look like this...
#model IEnumerable<Sample.Models.TypeModel>
#foreach (var item in Model)
{
<tr>
<td>
#Html.EditorFor(m=> item.Property,item.Type)
</td>
</tr>
}

Display the first row of view data in razor

I have a a ViewData with 8 rows. The ForEach loop works fine, but I need to extract the very first row before the foreach.
I need to extract only the first row to inject the default video in the iframe.
<iframe name="myFrame" width="800" height="500" src="#item.ID?wmode=transparent" allowfullscreen="True"></iframe>
Here is the foreach that works 100%
#foreach (var item in (List<VideoModel>)ViewData["Videos"])
{
<tr class="sep">
<td>#item.DisplayNumber</td>
<td>
#Html.ActionLink("Play Video", "IframeRedirect", "Home", new { ContentID = item.ID }, new { target = "someFrame", #class = "cbutton" })
</td>
<td>#item.Time #item.Hd</td>
<td><b>#item.Title</b><br />#item.Description</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><b>Author:</b> #item.Author <br />Subscribe to youtube channel</td>
</tr>
}
you can get the first row like following:
var firstRow = ((List<VideoModel>)ViewData["Videos"]).First();
If your model is a IEnumerable
You can get the first like following:
var first = Model.First();

select a specific value in a drop down list options in mvc

i have a list of items(here model). one of the list item - PhoneNumber, i will load values of phoneNUmber from database. user can change the phone number from the dropdownlist. however when view is first loaded, users activephone number should be the selected option in dropdwonlist. users active phone number here is lp.PhoneNumber
i created a ViewData["PhoneNumber"] for #Html.DropDownList("PhoneNumber")
#foreach (var lp in Model)
{
<tr>
<td class ="editor-field"><span class="SpaceForEditorField">#Html.EditorFor(order => lp.rKey)</span></td>
<td class ="editor-field"><span class="SpaceForEditorField">#Html.EditorFor(order => lp.AccountName)</span></td>
<td class ="editor-field"><span class="SpaceForEditorField">#Html.EditorFor(order => lp.AccountNumber)</span></td>
<td class ="editor-field">
<span class="SpaceForEditorField">
#Html.DropDownList("PhoneNumber")
</span>
</td>
</tr>
}
the code here is not displaying the active phone number as selected in dropdownlist, instead jus displays the items in dropdownlist.
i want to set the lp.PhoneNumber as selected item in the ddl "PhoneNumber"
i created ViewData["PhoneNumber"] in the controller as below
public ActionResult LoadOrder(int id, string token)
{
.....
.....
ViewData["PhoneNumber"] = new SelectList(new Ordering().GetPhoneNumberAvailableForOrder(id, token).ToList());
.......
.....
}

how to create cart content as pdf output in PRESTASHOP

I have to print the cart content in a pdf file and download.
I have try some code using pdfinvoicecontroller.php
I have created new controller - controllers/front/CartprintController.php
class CartprintControllerCore extends FrontController
{
protected $display_header = false;
protected $display_footer = false;
public $content_only = true;
protected $template;
public $filename;
public function postProcess()
{
if (!$this->context->customer->isLogged() && !Tools::getValue('secure_key'))
Tools::redirect('index.php?controller=authentication&back=cartprint');
}
public function display()
{
$displayproducts = $this->context->cart->getProducts();
$pdf = new PDF($displayproducts, PDF::TEMPLATE_INVOICE_CART, $this->context->smarty, $this->context->language->id);
$pdf->render();
}
public function getTemplate()
{
$template = _PS_THEME_PDF_DIR_.'/cartprint.tpl';
return $template;
}
}
Added
const TEMPLATE_INVOICE_CART = 'Cartprint';
line in classes/pdf/PDF.php
3.then created a HTML template file in pdf/cartprint.tpl
<table id="cart_summary" class="std">
<thead>
<tr>
<th class="cart_product first_item">{l s='Product'}</th>
<th class="cart_description item">{l s='Description'}</th>
<th class="cart_unit item">{l s='Unit price'}</th>
<th class="cart_quantity item">{l s='Qty'}</th>
<th class="cart_total item">{l s='Total'}</th>
</tr>
</thead>
{foreach $displayproducts item=prodpef name=prodpef }
<tr>
<td class="cart_product first_item">{$prodpef.name}</td>
<td class="cart_description item">{$prodpef.description_short}</td>
<td class="cart_unit item">{$prodpef.price}</td>>
<td class="cart_quantity item">{$prodpef.cart_quantity}</td>>
<td class="cart_total item">{$prodpef.total}</td>>
</tr>
{/foreach}
</table>
4.in shopping cart page i have created a link
<img src="{$img_dir}icon/pdf.gif" alt="{l s='Invoice'}" class="icon" />
but still i am not getting pdf output .
Any help ?
First it will be better to override PDF class and second you need to create a HTMLTemplateCartPrint.php file in classes dir or override classes dir in order to generate pdf file.
Best regards.