Calling Html Helper Method from Javascript Function - javascript

I am trying to call a custom Html helper method that I have written from a javascript function that is used by jqGrid to return formatted text, in this case a link, for a cell:
function formatGroupPlanEditLink(cellValue, options, rowObject) {
//var cellHtml = "<a href='/Insurance/GroupPlanEdit/?id=" + rowObject[0] + "'>" + rowObject[1] + "</a>";
var functionArgs = rowObject[1] + ',Url.Action("GroupPlan", "Insurance", new { id = ' + rowObject[0] + ' }),String.Format("Edit {0}", ' + rowObject[1] + '), listId,Url.Action("GroupPlanList", "Insurance"),false';
var cellHtml = '#Html.DialogFormLink(' + functionArgs + ')';
return cellHtml;
}
The problem that I have is that I cannot concatenate the entire string before the helper is executed. So the browser is trying to execute "#Html.DialogFormLink(" - which of course causes an error. I guess there must be a better way to go about this. I really want to still be able to use the Html helper method as I use it elsewhere, and it works nicely for my requirements.

I'm not that familiar with Razor, but the quotes around the #Html helper look suspicious.
var cellHtml = '#Html.DialogFormLink(' + functionArgs + ')';
The browser doesn't execute #Html.DialogFormLink; the server evaluates it. I suspect the server is injecting the literal '#Html.DialogFormLink' into your js.

Related

How to change only the second parameter of an onclick function when it's clicked

I have an onclick handler with two parameters. When it's clicked I want to update only the second parameter value. I prefer jQuery.
I have tried all kinds of jQuery combinations.
The included code works but I want to exclude replacing the first parameter.
The link:
<a href="#" id="messageremove" onclick="messageremove('param1', 'param2')"
The jQuery code:
$("#messageremove").attr('onclick', 'messageremove(' + "'" + param1 + "'" + ', ' + "'" + param2_new + "'" + ')');
I want to exclude replacing the first parameter, but right now both are being replaced.
Pass the variables, store the before variable and the new variable. Then update accordingly. I just switched them in this exmaple.
let before = '';
let after = '';
function messageremove(param1, param2) {
before = param1;
after = param2;
$("#messageremove").attr('onclick', `messageremove(`+ after + `,` + before + `)`)
console.log($("#messageremove").attr('onclick'))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Switch

binding variables to SQL statements using node-sqlite3

I am trying to convert this:
var query_string = 'SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM ' + organism + PIPE_output_table +
' WHERE ' + score_type + ' > ' + cutoff['range'] + ' AND protein_A = "' + item + '" ' +
'UNION SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM ' + organism + PIPE_output_table +
' WHERE ' + score_type + ' > ' + cutoff['range'] + ' AND protein_B = "' + item + '";';
db.each(query_string, function (err, row) {
...
To this:
var query_string = "SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM $table WHERE $score_type > $score AND protein_A = '$protein'" +
" UNION SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM $table WHERE $score_type > $score AND protein_A = '$protein'";
var placeholders = {
$table: organism + PIPE_output_table,
$score_type: score_type,
$score: cutoff['range'],
$protein: item
};
var stmt = db.prepare(query_string, placeholders, function(err) {
console.log(err);
stmt.each(function(err,row) {
...
})
}
but I keep getting this error:
Error: SQLITE_ERROR: near "$table": syntax error
But I am not sure what is syntactically wrong here since the format is as I have seen it in the API documentation. I have tried '?', '#', and ':' before each variables but none seem to be recognized.
What's wrong in my code?
Bind parameters only work for values in the WHERE clause. Table and column names (collectively called "identifiers") won't work.
"SELECT foo FROM bar WHERE this = $that" # OK
"SELECT foo FROM bar WHERE $this = 'that'" # Not OK
Normally you'd work around this by escaping and quoting identifiers and inserting them into the query. A good database library has a method call for this...
var this = db.quote_literal(input_column);
'SELECT foo FROM bar WHERE ' + this + ' = ?'
Unfortunately, node-sqlite3 doesn't appear to have one. :(
SQLite does provide a quoting function, the %w operator, but node-sqlite3 doesn't appear to make it available.
You'll have to write your own. Follow the instructions from this answer in Python and convert them to Javascript.
Ensure the string can be encoded as UTF-8.
Ensure the string does not include any NUL characters.
Replace all " with "".
Wrap the entire thing in double quotes.
I'm not very good with Javascript, so I'll leave you to code that.

error due to single quote in a parameter in url query string

I have a screen from which I am passing a parameter to other screeen. This parameter contains a single quote. (For eg. ABC's xyz). The other screen shows this paramter correctly on screen. However, I am not able to submit the request on load to servlet. I am not able to figure out why?
Tried using URL encoding, replacing ' with \' and replacing ' with %27. Didn't work for me. Plz help.
The code is as follows.
child page var scheme = document.forms[0].scheme.value;
window.location.href = redirectVal+".jsp?levelOfReport="
+ levelOfReport
+ "&circleCode="
+ circleCode
+ "&networkCode="
+ networkCode
+ "&moduleCode="
+ moduleCode
+ "&regionCode="
+ regionCode
+ "&branchCode="
+ branchCode
+ "&circleName="
+ circleName
+ "&moduleName="
+ moduleName
+ "&branchName="
+ branchName
+ "&Amount="
+ Amount
+ "&scheme="
+ scheme + "&fromDate=" + fromDate + "&toDate=" + toDate;
Child page
SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=<%=request.getParameter("scheme")%>
You're missing the end to your method call.
SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=<%=request.getParameter("scheme")%>
Map your quotes to eachother, you're telling it to do something like
SubmitButton('./SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=HTTP
You need to add an additional '); at the end of that line

javascript onchange event calling a function with multiple arguments

I am having problems with the following code:
I have n numbers of select box, whose id are differentiated by the values that the variables ModName and level take:
container.innerHTML = '<form id="listToolForm_'+ModName+level+'"><select id="listToolSelect_'+ModName+level+'" onChange="selValue(this.selectedIndex,'+ModName'+, level)"><option value="0">---</option></select></form>';
The select box is then populated by values taken from a database (this part is not really relevant here). With each form, there is a hidden input with id= idArg'+ModName+'_lev'+level, that will store the index selected by the user, using onChange and the function selValue(x):
Here is the function selValue(index, ModName, level):
function selValue(index, ModName, level){
document.getElementById("idArg"+ModName+"_lev"+level).value = index;
}
I tried the script setting var ModName = "SL", and var level = 0. I get an error: 'SL is not defined'. Another test that worked is by replacing the function that fires up onChange:
onChange="selValue(this.selectedIndex)"
and
function selValue(index){
var level = 0;
var ModName = "SL";
document.getElementById("idArg"+ModName+"_lev"+level).value = index;
}
So if the function fired up onChange has one argument, the code works, but does not when using several arguments. I suspect it's because of the line:
onChange = "selValue(this.selectedIndex,'+ModName'+, level)"
I tried:
onChange="'+window["selValue"](this.selectedIndex, ModName, level)+'"
without success.
The following (I assumed '+ModName'+ was a typo in the question and not in the actual code):
onChange="selValue(this.selectedIndex, ' + ModName + ', level)"
causes JavaScript to substitute ModName with its value, which results in the actual HTML being:
onChange="selValue(this.selectedIndex, SL, level)"
When handling the onChange event, the JavaScript engine "thinks" SL is a variable (hence the error), but you actually want it to be a string (if I understood well).
To turn SL into a string, you should surround it with single quotes. To do that, you will need the \' escape sequence, since you're already using nested quotes. Simply replace:
onChange="selValue(this.selectedIndex, ' + ModName + ', level)"
with:
onChange="selValue(this.selectedIndex, \'' + ModName + '\', level)"
Also, didn't you mean:
onChange="selValue(this.selectedIndex, \'' + ModName + '\', ' + level + ')"
?

Pass multiple parameters from ASP.NET to javascript function

Essentially what I'm trying to do is fairly simple....pass more than one parameter (4 total eventually) into a javascript function from my ASP.NET code behind.
What I've tried to do is this in the ASCX file...
function ToggleReportOptions(filenameText, buttonText) { /*stuff happens here*/ }
and this in the ASCX.cs file...
string testing123 = "testStringOne";
string testing124 = "testStringTwo";
optReportOptionsRunRealTime.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
but this doesn't seem to work as in my output the first variable contains "testStringOne, testStringTwo" and the 2nd variable is "undefined"
Any help on clearing up my probably stupid issue here would be great (i'm very inexperienced with javascript, more of a .NET developer)
You've missed out a few single-quotes, meaning that you're passing a single string containing a comma rather than two separate strings. That is, you're passing 'testStringOne, testStringTwo' rather than 'testStringOne' and 'testStringTwo'.
Try this instead:
optReportOptionsRunRealTime.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";

Categories

Resources