Browser can not parse HTML properly - Grails 2 - html

I am generating HTML from controller (Backend) but browser can not parse that HTML properly.
Controller code:
def getValue (returnIndex) {
String dropDown = "<select name='" + returnIndex + "' style=''>"
def CatArr = new BudgetViewDatabaseService().executeQuery("SELECT c.id,c.name AS categoryName FROM chart AS a LEFT JOIN crtClass AS b ON a.id=b.chart_class_type_id LEFT JOIN chart_group AS c ON b.id=c.chart_class_id WHERE c.status=1")
if (CatArr.size()) {
for (int i = 0; i < CatArr.size(); i++) {
def catId = CatArr[i][0]
def ProductArr
ProductArr = new BudgetViewDatabaseService().executeQuery("SELECT id,accountCode,accountName FROM bv.crtMsr where status='1' AND chart_group_id='" + catId + "'")
if (ProductArr.size()) {
dropDown += "<optgroup label='" + CatArr[i][1] + "'>"
for (int j = 0; j < ProductArr.size(); j++) {
dropDown += "<option value='" + ProductArr[j][1] + "' >" + ProductArr[j][1] + " " + ProductArr[j][2] + "</option>"
}
dropDown += "</optgroup>"
}
}
}
dropDown += "</select>"
return dropDown
}
view page's code:
<div class="fieldContainer fcCombo">
<label>
My GL <b>:</b>
</label>
${new CoreParamsHelperTagLib().getValue('formFourGLAccount')}
</div>
issue:
Generated HTML looks like:
When I am opening that HTML on edit mode from browser then its looks like:
<select name='formFourGLAccount' style=''><optgroup
label='Overige immateriële bezittingen'><option value='0430' >0430 Overige niet
materiële bezittingen</option></optgroup><optgroup label='Computers en
computerapparatuur'><option value='0210' >0210 Computers en
computerapparatuur</option><option value='0211' >0211 Afschrijving computers en
computerapparatuur</option></optgroup><optgroup label='Overige materiële
bezittingen'><option value='0250' >0250 Overige materiële
bezittingen</option><option value='0251' >0251 Afschrijving overige materiele
bezittingen</option></optgroup><optgroup label='Waarborgsommen'><option
value='0300' >0300 Waarborgsommen</option></optgroup><optgroup
label='Deelnemingen in andere bedrijven'><option value='0310' >0310 Aandeel of belang
in andere bedrijven</option></optgroup><optgroup label='Strategische langlopende
beleggingen'><option value='0320' >0320 Strategische langlopende
beleggingen</option></optgroup><optgroup label='Verstrekte langlopende leningen
(hypotheek ed)'><option value='0330' >0330 Verstrekte langlopende leningen (hypotheek
ed)</option></optgroup><optgroup label='Overige financiële
bezittingen'><option value='0340' >0340 Overige financiële bezittingen</option></optgroup><optgroup label='Voorraad'><option
If I copy returns result (HTML) from controller and past it on browser manually then its working fine

You have not shown how that HTML is being rendered so it isn't clear specifically how to fix it, but what is happening is the content is being HTML encoded, which you do not want if you want the browser to evaluate the HTML tags.
EDIT Based On Comment:
<div class="fieldContainer fcCombo">
<label>
My GL <b>:</b>
</label>
${new CoreParamsHelperTagLib().getGLAccountExpanseBudgetForReconcilationOthersDropDown('formFourGLAccount')}
</div>
There is no good reason to create an instance of a taglib. You should invoke the tag as a GSP tag.
You are returning hardcoded HTML from your controller as a model variable. That is a bad idea, but not what you are asking about. If you really do want to do that, then you will need to prevent the data from being HTML encoded in your GSP. You can use the raw(your unescaped html code here) method in your GSP as one way to avoid the encoding.

Related

Add query string to a Google Webapp URL

//htmlFileName: String
function createTemplate(htmlFileName) {
let {protocol,stdName} = getProtocolData()
let params = protocol // protocol: String, exemple 987654321
let url = 'https://script.google.com/a/macros/xxxxx.xxx.xx/s/beenchangedforsecurity/exec'
url += '?' + 'p' + '=' + params
/** **MY GOAL: url** *
* https://script.google.com/a/macros/xxxxx.xxx.xx/s/beenchangedforsecurity/exec?p=987654321
*/
let html = HtmlService.createTemplateFromFile(htmlFileName)
html.protocol = protocol
html.stdName = stdName
html.url = url
Logger.log(html.protocol)
Logger.log(html.stdName)
Logger.log(html.url)
return html.evaluate().getBlob().getDataAsString() }
ERROR:
Exception: Malformed HTML content: https://script.google.com/a/macros/xxxxx.xxx.xx/s/beenchangedforsecurity/exec?p=000002036
I reviwed my HTML code and made this change:
original:
<p>
<form id="btn1" action= " https://script.google.com/macros/s/blablabla/exec/?p='1234567890' " >
<button typy="submit" id="uploadButton1"> Click here to send docs (param value with singlew quotes) </button>
</form>
<p>
new code:
<p>
<a id="clickhere" class="button" href= " <?!= url ?> " >
Clique aqui para enviar os documentos
</a>
</p>
I understood that in can not send query string by GET method. #Cooper, thank you again for your tip.

SQL where condition is only receiving one word from the value of the field

I need to pass to a where condition a string instead of the id of the row, because of reasons. The problem is that the condition are somehow only catching the first word of the selected value. For example, look at this piece of code:
<select id="brand" name="brand" required>
<option value="Lorem Ipsum Dolor" selected>Lorem Ipsum Dolor</option>
</select>
Then, by echoing the last_query(), the condition will show:
... WHERE `table.column` = `Lorem` ...
Instead of the whole value.
This is my query:
public function find_id_ano_modelo($marca, $modelo, $ano, $comb)
{
$this->db
->select('ano_modelo.id')
->join('modelo', 'modelo.id = ano_modelo.id_modelo')
->join('marca', 'marca.id_marca = modelo.id_marca')
->where('marca.id_marca', $marca)
->where('modelo.modelo', $modelo)
->where('ano_modelo.ano', $ano)
->where('ano_modelo.combustivel', $comb)
->where('marca.tipo = 1');
$query = $this->db->get($this->table);
echo $this->db->last_query();
if ($query) {
foreach ($query->result_array() as $q) {
return $q['id'];
}
}
}
That is being called in my edit method from my controller:
...
$marca = $this->input->post('id_marca');
$modelo = $this->input->post('id_modelo');
$ano = $this->input->post('ano');
$comb = $this->input->post('combustivel');
$data['id_ano_modelo'] = $this->Modelos_model->find_id_ano_modelo($marca, $modelo, $ano, $comb);
....
After discussing it in chat, the error was solved:
The request was being sent via an ajax call and the dropdown was built with:
modelo.append('<option value=' + v.modelo + '>' + v.modelo + '</option>');
The problem was the missing double-quotes around the value. Without them, each word of the value was transformed into an html property:
For this string:
QQ 1.0 ACT 12V 69cv 5p
This was generated:
<option value="QQ" 1.0="" act="" fl="" 12v="" 69cv="" 5p="">QQ 1.0 ACT FL 12V 69cv 5p</option>
Adding qoutes here solved it:
modelo.append('<option value="' + v.modelo + '">' + v.modelo + '</option>');

send value from client side to server side using C# in asp.net

I'm trying to create a website content management using asp.net and C# and bootstrap. I already done this using asp.net and C# and a server control like gridview but I want create this version one like as wordpress CMS.
I will describe my project to clear my purpose.
First I fill a DataTable from database. This Datatable has messageId int, Subject varchar, name varchar, email varchar, message text, isRead bit, and so on columns.isRead column is bit type for specifies that the message is read or not.
I Fill my DataTable using below Method:
DataTable dt = cls.Fill_In_DataTable("MessageFetchMessage");
Then I generate html text using another method dynamically: on Page_Load
protected void Page_Load(object sender, EventArgs e)
{
messeges = cls.fetchMessages();
}
messege the string variable, will append generated html code to aspx page:
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-clock-o fa-fw"></i> Last messages From users</h3>
</div>
<div class="panel-body">
<div class="list-group">
<%=messeges %>
</div>
<div class="text-right">
View All messages <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
</div>
</div>
the message content has these text from fetchMessages()method:
public string fetchMessages()
{
string post = ""; string readed = "";
DataTable dt = cls.Fill_In_DataTable("MessageFetchMessage");
if (dt.Rows.Count>0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DateTime dtTemp = DateTime.Parse(dt.Rows[i]["messageDate"].ToString());
if (dt.Rows[i]["isRead"].ToString() == "True")
readed = "MessageReaded";
else
readed = "MessageNew";
post += "<div class='modal fade' id='myModal" + dt.Rows[i]["messageId"].ToString() + "' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>"
+ "<div class='modal-dialog' role='document'>"
+ "<div class='modal-content'>"
+ "<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'><span style='font-weight:bold'>Subject</span> : " + dt.Rows[i]["subject"].ToString() + "</h4></div>"
+ "<div class='modal-header'><p><span style='font-weight:bold'>Date</span> : " + dtTemp.ToString("yyyy/MM/dd") + "</p>"
+ "<p><span style='font-weight:bold'>Time</span> : " + dt.Rows[i]["messageTime"].ToString() + "</p>"
+ "<p><span style='font-weight:bold'>Email</span> : " + dt.Rows[i]["email"].ToString() + "</p></div>"
+ "<div class='modal-body'>" + dt.Rows[i]["message"].ToString() + "</div>"
+ "<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>Close</button><input type='submit' ID='btn" + dt.Rows[i]["messageId"].ToString() + "' class='btn btn-danger' onserverclick='btn_Click' value='Delete message' /></div>"
+ "</div></div></div>";
string narrow = Special.TimeToNarrow(dt.Rows[i]["messageDate"].ToString(), dt.Rows[i]["messageTime"].ToString());
post += "<a data-toggle='modal' data-target='#myModal" + dt.Rows[i]["messageId"].ToString() + "' href='#' class='list-group-item " + readed + "'><span class='badge'>" + narrow + "</span><i class='fa fa-fw fa-comment'></i> <span>"
+ dt.Rows[i]["name"].ToString() + "</span> : <span>" + dt.Rows[i]["subject"].ToString() + "</span></a>";
}
}
return post;
}
finally I add server code behind for btn_Click Event:
protected void btn_Click(object sender, EventArgs e)
{
string id = (sender as Control).ClientID;
//Then give Id to database class for CRUD Intractions
}
but btn_Click never called from client side. I search for similar question within 2 days and didn't get answer. please help me :)
Here I will put my Website screen Shots:
Then after click on one of the rows a pop up window will show using modal bootstrap:
Add your Modal, button and any other mark up you have to the ASPX page (mark up). Then you can get your ID on click event. The dynamic generation of your code is not registering the controls with the server side in Webforms.
Once you have captured your Message ID, you can place it in ViewState, Session or a hidden field on the UI. That way you can have the ID to use whenever you need it.

Dynamically create a button in aspx page

i want to make some elements in my asp form like below, unfortunately input button dose not work and "btnsave_ServerClick" dose not fire!
any body has idea?
<div class="clear"></div>
<%
string matn="";
System.Data.DataTable ds = new System.Data.DataTable();
try
{
// databaselinker is a class which is connected to the database
databaselinker link = new databaselinker();
// id is QueryString parameter
ds = link.selectplan(id);
}
catch (Exception Ex) { }
int i=0;
foreach (System.Data.DataRow DRow in ds.Rows)
{
TableRow tRow = new TableRow();
matn += "<div class='frame' id='frame" + DRow["PlanID"] + "'>";
matn += " name <input id='Text1' type='text' /> * ";
matn += "</br>";
matn += "Family <input id='Text2' type='text' /> * ";
matn += "<input id='Button1' type='button' value='Save' runat='server' onServerClick='btnsave_ServerClick' />";
matn += "</div>";
i++;
}
Response.Write(matn);
%>
When you type that button as string on this line
matn += "<input id='Button1' type='button' value='Save' runat='server' onServerClick='btnsave_ServerClick' />";
the asp.net did not know anything about that button, even if you have place the runat= is still a string.
when you have render that string using the
Response.Write(matn);
asp.net compile the response.write but NOT compile the string that you render of.
And that is the reason that is not working and not fire.
To make it work you need to create it dynamically.
Some examples on how to do it:
HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
Dynamic Controls Made Easy in ASP.Net
Persistent dynamic control in ASP.Net

Asp .Net client side dynamic text

Im learning Asp.Net, after writing a master page, I couldnt make it run on client (to actually see the animation). The thread waiting action is done on server before publishing the web page so animation is not dynamic. It shows only the final state of the page.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body bgcolor="red">
<center>
<h2>Hello w3schools!</h2>
<h4 id="aa">qq</h4>
<h3>
<p><%
for (int i = 0; i < 30; i++)
{
System.Threading.Thread.Sleep(25);
int txtLeft = 300 + (int)(100*Math.Sin(i));
int txtTop = 300+(int)(100*Math.Cos(i));
string theText = "Hello there!";
Response.Write("<div style=\"position:absolute; left: "+ txtLeft + "px; top:" + txtTop + "px; \">" + theText +"</div>");
}
%></p>
</h3>
</center>
</body>
</html>
I tried
runat="client" (near the "p" letter which is in brackets)
but it failed:
Runat must have value Server.
Take a look at this jsFiddle:
$(Start);
function Start() {
var txtLeft, txtTop, theText, theHTML;
theText = "Hello there!";
for (var i = 0; i < 30; i++) {
txtLeft = 300 + (100 * Math.sin(i));
txtTop = 300 + (100 * Math.cos(i));
theHTML = '<div style="position:absolute; left: ' + txtLeft + 'px; top:' + txtTop + 'px;">' + theText + '</div>';
$('#Demo').append(theHTML);
}
}
Basically, there's no need for server-side programming to generate the output you want; use asp.net for server work. What you can do is add this script to the page in a script tag along with the jquery reference and you're done.