On the asp page i have created this function to check if two strings are equal:
<script type="text/javascript">
function ButtonClick(a, b)
{
if (a == b)
{
alert("Correct!");
}
else
{
alert("Wrong!");
}
}
</script>
Then, i have created this function which I use when the page loads, to display everything:
public void FillPageSpelling()
{
ArrayList videoList1 = new ArrayList();
if (!IsPostBack)
{
videoList1 = ConnectionClass.GetSpelling(1);
}
else
{
int i = Convert.ToInt32(DropDownList1.SelectedValue);
videoList1 = ConnectionClass.GetSpelling(i);
}
StringBuilder sb = new StringBuilder();
foreach (Spelling sp in videoList1)
{
sb.Append(
string.Format(
#"<table class='VideoTable'>
<tr>
<td align='center'><font face='Verdana'> <font size='3'>Level:</font> <font size='2'>{3}</font></font></td>
</tr>
<tr>
<td align='center'><font face='Verdana'> <font size='3'>Sentence:</font> <font size='2'>{1}</font></font></td>
</tr>
<tr>
<td align='center'><font size='3'>Sound:<audio controls><source src=sound/{2}></audio>
<font face='Verdana'> <font size='2'> </font> </font></td>
</tr>
<tr>
<tr><td align='center'><font face='Verdana'> <font size='3'>Write the word here: <input type=text name=TextBox1></font></font> </td> </tr>
<td><button name=btnCheck type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Check</button> </td>
<td><button name=btnCheat type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Cheat</button> </td>
</tr>
<tr>
<td align='center'><font face='Verdana'> <font size='3'>Word:</font> <font size='2'><asp:Label ID=lblWord runat=server>{4}</asp:Label></font></font></td>
</tr>
</br>
</table>", sp.SID, sp.Sentence, sp.Sound, sp.Level, sp.Word));
lblOutput.Text = sb.ToString();
}
Well, it turns out I have made a mistake here: <td><button name=btnCheck type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Check</button> </td>
I changed label lblWord to be a textbox - TextBox2 instead, and this is how you should call the function:
<input type=button value='Check' class='p-userButton' onClick='ButtonClick(document.getElementById(""TextBox1"").value, document.getElementById(""TextBox2"").value);'/>
JavaScript functions do not have typed parameters. Try your function like this:
function ButtonClick(a, b)
{
if (a == b)
{
alert("Correct!");
}
else
{
alert("Wrong!");
}
}
function ButtonClick(string a, string b)
javascript does not support parameters with data type:
try : function ButtonClick(a, b)
Related
Using RazorEngine to compile the HTML getting error as
RazorEngine.Templating.TemplateCompilationException: Errors while compiling a Template.
Please try the following to solve the situation:
* If the problem is about missing/invalid references or multiple defines either try to load
the missing references manually (in the compiling appdomain!) or
Specify your references manually by providing your own IReferenceResolver implementation.
See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
Currently all references have to be available as files!
* If you get 'class' does not contain a definition for 'member':
try another modelType (for example 'null' to make the model dynamic).
NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
Or try to use static instead of anonymous/dynamic types.
private string RunCompile(string rootPath, string templateName, EmailViewModel model, string templateKey = null)
{
string result = string.Empty;
if (string.IsNullOrEmpty(rootPath) || string.IsNullOrEmpty(templateName) || model == null) return result;
string templateFilePath = Path.Combine(rootPath, templateName);
if (File.Exists(templateFilePath))
{
string template = File.ReadAllText(templateFilePath);
if (string.IsNullOrEmpty(templateKey))
{
templateKey = Guid.NewGuid().ToString();
}
result = Engine.Razor.RunCompile(template, templateKey, typeof(EmailViewModel), model);
}
return result;
}
HTML template
#model ViewModel.EmailViewModel
<table bgcolor="white" align="left" border="0" cellspacing="0" cellpadding="0" style="color:#5E5E5E;font-size:14px;font-family:Proxima Nova,Century Gothic,Arial,Verdana,sans-serif;width:100%;border:1px solid #F0F0F0; padding: 32px;">
<tbody>
<tr align="center">
<td style="padding-top:30px;padding-bottom:32px;">
<img src="https://ok6static.oktacdn.com/bc/image/fileStoreRecord?id=fs01fviisxo2dNOCM2p7" alt="monash university logo"/>
</td>
</tr>
<tr>
<td style="padding-top:24px;">
<div style="font-family:Consolas,Courier New,Courier,monospace;text-align:left;margin:10px;">
<h1>
Thank you for the registration
</h1>
</div>
</td>
</tr>
<tr>
<td style="padding-top:24px;">
<div style="font-family:Consolas,Courier New,Courier,monospace;text-align:left;margin:10px;">
<p>Please click on the below link to confirm email address</p>
</div>
</td>
</tr>
<tr>
<td style="padding-top:24px;">
<div style="font-family:Consolas,Courier New,Courier,monospace;text-align:left;margin:10px;">
#Model.Message
</div>
</td>
</tr>
</tbody>
</table>
I had the same issue. And resolved it with removing string #model ViewModel.EmailViewModel
I have searched for help but can find any topic that could help me.
I have a table like this
<table id="tableftest" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="" data-sortable="false">
<input type="checkbox" id="checkall" title="chọn tất cả" onclick="" />
</th>
<th data-column-id="ip">IP</th>
<th data-column-id="network" hidden="hidden">Network</th>
<th data-column-id="sender">GateWay</th>
<th data-column-id="received">SubnetMask</th>
<th data-column-id="viewdetail">VPS được gán</th>
#*<th data-column-id="commands" data-formatter="commands" data-sortable="false"></th>*#
</tr>
</thead>
<tbody>
#foreach (VPSIP ipitem in ip)
{
<tr>
#if (ipitem.VPSID != null)
{
<td></td>
}
else
{
<td>
<input type="checkbox" class="ipDelListClass" name="ipDelList" value="#ipitem.IpID" />
</td>
}
<td>#(ipitem.IPAddress)</td>
<td hidden="hidden">#ipitem.NetworkRanx.NetworkAddress</td>
<td>#(ipitem.NetworkRanx.Gateway)</td>
<td>#ipitem.NetworkRanx.SubnetMask</td>
#if (ipitem.VPSID != null)
{
<td>#(ipitem.VPSs.VPSName)</td>
#*<td></td>*#
}
else
{
<td></td>
#*<td>
<button title="Xóa ip" class="btn btn-danger zmdi zmdi-delete" data-toggle="modal" data-target="#IpDel_#ipitem.IpID" style="font-size:medium"></button>
</td>*#
}
</tr>
}
</tbody>
</table>
When I check all checkboxs in table and click button delete, it will post data to this controller
[HttpPost]
public ActionResult IPDeleteMany(FormCollection f)
{
var ipDelArray = f.Get("ipDelList");
ServerBusiness serBu = new ServerBusiness();
string[] ipDelList = ipDelArray.Split(',');
try
{
foreach (string ipId in ipDelList)
{
var iprow = serBu.getIPById(int.Parse(ipId));
serBu.removeIPById(iprow);
}
TempData["Success"] = "IP đã bị xóa!";
return RedirectToAction("ViewListIP", "TechnicalStaff");
}
catch
{
TempData["Error"] = "Lỗi!";
return RedirectToAction("ViewListIP", "TechnicalStaff");
}
}
When it runs, it only get values of rows that is visible, not all the rows in table.
Is there anyway to get all of values checked in table include invisible rows?
change your hidden="hidden" to style="display:none;".
And for you other pages, because you didnt use Ajax your data isn't exist in fact, so you can:
use Ajax and hide your old rows and append new rows where exist
all of your desired rows in your html however some of them be
hidden.
force your users to submit current page data and then switch pages.
I need extract value of just one specific td from the table by using XPath, but code always return null. How can I fix this?
var location = GetLocation(document.Result.DocumentNode.SelectSingleNode("//*[#id='detailTabTable']/tbody/tr[3]/td[2]"));
and the code
private string GetLocation(HtmlNode h)
{
try
{
string location = null;
if (h == null)
{
location = "N/A";
}
else
{
location = h.InnerText;
location = location.Substring(0, location.IndexOf(",", StringComparison.InvariantCulture));
}
return location;
}
catch (Exception ex)
{
log.ErrorFormat("Error in Link Data Repository {0} in Parse Links {1}", ex.Message, ex.StackTrace);
throw new Exception(ex.Message);
}
}
And small simple table:
<table id="detailTabTable" width="99%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="detailTabContentLt">Current List Price:</td>
<td class="detailTabContentPriceRt">
<span class="aiDetailCurrentPrice">AED 6,600,000</span>
</td>
</tr>
<tr>
<td class="detailTabContentLt" style="white-space: nowrap;">Plot size (Sq. Ft.):</td>
<td class="detailTabContentRt">N/A</td>
</tr>
<tr>
<td class="detailTabContentLt" valign="top">Locality</td>
<td class="detailTabContentRt">Dubai, Dubai</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
</table>
I have just tested your code. As mentioned in the comments when you do remove tbody from your xpath expression everything works fine. This worked fine for
me.
private static void htmlAgilityPackTest()
{
string html = " <table id=\"detailTabTable\" width=\"99%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"detailTabContentLt\">Current List Price:</td><td class=\"detailTabContentPriceRt\"><span class=\"aiDetailCurrentPrice\">AED 6,600,000</span></td> </tr><tr> <td class=\"detailTabContentLt\" style=\"white-space: nowrap;\">Plot size (Sq. Ft.):</td><td class=\"detailTabContentRt\">N/A</td></tr> <tr><td class=\"detailTabContentLt\" valign=\"top\">Locality</td> <td class=\"detailTabContentRt\">Dubai, Dubai</td> </tr> <tr><td colspan=\"2\"></td> </tr> </table>";
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
var node = document.DocumentNode.SelectSingleNode("//*[#id='detailTabTable']/tr[3]/td[2]");
string location = GetLocation(node);
Console.WriteLine("Location: " + location);
}
In case I misunderstood anything please let me know.
You can use fizzler and select stuff the CSS way :)
http://blog.simontimms.com/2014/02/24/parsing-html-in-c-using-css-selectors/
The short and "straight" question:
A Controller action method (A) is called by a form with some parameters. In the same View, there is another form, which when submitted calls another action method (B) in the same controller.
How can I access the values of (B) form (but essentially they are the content values of dropdowns and textboxes) from the action method A?
The Long and winding explanation:
I have a View with two forms, like this:
using (Html.BeginForm("List", "Rapporti"))
{
<span>Ricerca Rapporti per Linea </span>
#Html.DropDownList("KLinea",
new SelectList(new RapportiCT.Domain.Entities.RapportiEntities().TLinea, "KLinea", "DLinea")
, "Scegli una linea");
<span>Data inizio</span>
#Html.TextBox("startDate", DateTime.Now.AddDays(-4).ToShortDateString(), new { #class = "dateTextBox" });
<span>Data fine</span>
#Html.TextBox("endDate", DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy"), new { #class = "dateTextBox" })
<input type="submit" value="Cerca" />
}
if (Model.Count() > 0)
{
var lastArticolo = "";
<h2>Rapporti Capoturno #Model.First().rapportoCT.DLinea</h2>
<table>
#foreach (var rapporto in Model)
{
if (lastArticolo != rapporto.rapportoCT.DCodArt)
{
<tr>
<td class="td_title">
#rapporto.rapportoCT.DCodArt
</td>
<td colspan="3" class="td_title">
#rapporto.rapportoCT.DDescArt
</td>
<td class="td_title">
Rendimento
</td>
<td class="td_title">
#Pallet control.
</td>
<td class="td_title">
#Pallet accanton.
</td>
<td class="td_title">
Ore fermo linea
</td>
</tr>
lastArticolo = rapporto.rapportoCT.DCodArt;
}
using (Html.BeginForm("ControlloQualita", "Rapporti"))
{
<tr>
<td class="td_detail">
#rapporto.rapportoCT.Data
</td>
<td class="td_detail">
#rapporto.rapportoCT.Turno
</td>
<td class="td_detail">
#rapporto.rapportoCT.Lettera
</td>
<td class="td_detail">
#rapporto.rapportoCT.DNota
</td>
<td class="td_detail">
#rapporto.RendimentoLinea
</td>
<td class="td_detail">
#Html.TextBox("PalletControllati", rapporto.PalletControllati, new { style="width:50px" })
</td>
<td class="td_detail">
#Html.TextBox("PalletAccantonati", rapporto.PalletAccantonati, new { style = "width:50px" })
</td>
<td class="td_detail">
#Html.TextBox("OreFermoLinea", rapporto.OreFermoLinea, new { style = "width:50px" })
</td>
<td>
<input type="submit" value="Salva" />
</td>
</tr>
}
}
</table>
}
else
{
#:Nessun record trovato
}
Both forms post to the same RapportiController: the first form is used to query the database and show the resulting records, with the second one I'd like to update the database record.
View (snapshot):
So, my Controller class is like this:
// No problem here
public ViewResult List(int? KLinea = null, DateTime? startDate = null, DateTime? endDate = null)
{
IQueryable<VRapportiCT> qVRapporti = repository
.ViewRapporti(KLinea.Value, startDate.Value, endDate.Value);
List<VRapportiCT> lRapporti = qVRapporti.ToList();
List<RapportoRendimentoViewModel> listRRVM = new List<RapportoRendimentoViewModel>();
foreach (var rapporto in lRapporti)
{
RapportoRendimentoViewModel rrVM = new RapportoRendimentoViewModel();
rrVM.rapportoCT = rapporto;
rrVM.RendimentoLinea = repository.GetRendimentoLinea(rapporto);
rrVM.PalletControllati = "0";
rrVM.PalletAccantonati = "0";
rrVM.OreFermoLinea = "0";
listRRVM.Add(rrVM);
}
return View(listRRVM);
}
public RedirectToRouteResult ControlloQualita(int PalletControllati, int PalletAccantonati, int OreFermoLinea)
{
// How can I access the 1°form values here (a.k.a. the DropDown and Date text box values?
// ...Fake values, only to assert that the parameters get passed to the action...
RouteValueDictionary dic = new RouteValueDictionary();
dic.Add("KLinea", 55);
dic.Add("startDate", DateTime.Now.AddDays(-4));
dic.Add("endDate", DateTime.Now);
return RedirectToAction("List", "Rapporti", dic);
}
I see 2 options here:
Store last submitted "List" form in, lets say, Session, or TempData, or any other storage you like and restore these values on "ControlloQualita" form submission. This approch assumes that you need last submitted values from "List" form rather than latest.
Intercept "ControlloQualita" form submission in order to append values of "List" form to the request. This can be achieved with javascript by adding hidden inputs to the "ControlloQualita" form.
So I have this HTML page that is exported to an Excel file through an MVC action. The action actually goes and renders this partial view, and then exports that rendered view with correct formatting to an Excel file. However, the view is rendered exactly how it is seen before I do the export, and that view contains an "Export to Excel" button, so when I export this, the button image appears as a red X in the top left corner of the Excel file.
I can intercept the string containing this HTML to render in the ExcelExport action, and it looks like this for one example:
<div id="summaryInformation" >
<img id="ExportToExcel" style=" cursor: pointer;" src="/Extranet/img/btn_user_export_excel_off.gif" />
<table class="resultsGrid" cellpadding="2" cellspacing="0">
<tr>
<td id="NicknameLabel" class="resultsCell">Nick Name</td>
<td id="NicknameValue" colspan="3">
Swap
</td>
</tr>
<tr>
<td id="EffectiveDateLabel" class="resultsCell">
<label for="EffectiveDate">Effective Date</label>
</td>
<td id="EffectiveDateValue" class="alignRight">
02-Mar-2011
</td>
<td id ="NotionalLabel" class="resultsCell">
<label for="Notional">Notional</label>
</td>
<td id="NotionalValue" class="alignRight">
<span>
USD
</span>
10,000,000.00
</td>
</tr>
<tr>
<td id="MaturityDateLabel" class="resultsCell">
<label for="MaturityDate">Maturity Date</label>
</td>
<td id="MaturityDateValue" class="alignRight">
02-Mar-2016
-
Modified Following
</td>
<td id="TimeStampLabel" class="resultsCell">
Rate Time Stamp
</td>
<td id="Timestamp" class="alignRight">
28-Feb-2011 16:00
</td>
</tr>
<tr >
<td id="HolidatCityLabel" class="resultsCell"> Holiday City</td>
<td id="ddlHolidayCity" colspan="3">
New York,
London
</td>
</tr>
</table>
</div>
<script>
$("#ExportToExcel").click(function () {
// ajax call to do the export
var actionUrl = "/Extranet/mvc/Indications.cfc/ExportToExcel";
var viewName = "/Extranet/Views/Indications/ResultsViews/SummaryInformation.aspx";
var fileName = 'SummaryInfo.xls';
GridExport(actionUrl, viewName, fileName);
});
</script>
That <img id="ExportToExcel" tag at the top is the one I want to remove just for the export. All of what you see is contained within a C# string. How would I go and remove that line from the string so it doesn't try and render the image in Excel?
EDIT: Would probably make sense also that we wouldn't need any of the <script> in the export either, but since that won't show up in Excel anyway I don't think that's a huge deal for now.
Remove all img tags:
string html2 = Regex.Replace( html, #"(<img\/?[^>]+>)", #"",
RegexOptions.IgnoreCase );
Include reference: using System.Text.RegularExpressions;
If it's in a C# string then just:
myHTMLString.Replace(#"<img id="ExportToExcel" style=" cursor: pointer;" src="/Extranet/img/btn_user_export_excel_off.gif" />","");
The safest way to do this will be to use the HTML Agility Pack to read in the HTML and then write code that removes the image node from the HTML.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);
HtmlNode image =doc.GetElementById("ExportToExcel"]);
image.Remove();
htmlString = doc.WriteTo();
You can use similar code to remove the script tag and other img tags.
I'm just using this
private string RemoveImages(string html)
{
StringBuilder retval = new StringBuilder();
using (StringReader reader = new StringReader(html))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
if (!line.StartsWith("<img"))
{
retval.Append(line);
}
}
} while (line != null);
}
return retval.ToString();
}