Pass multiple parameters from ASP.NET to javascript function - javascript

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 + "')";

Related

Concat QML Properties with Javascript

I'm setting properties on QML items with java script, where "gaugetext" is a property of my QML object.
function finishCreation(setWidth,setHeight,setX,setY,setMinValue,setMaxValue,setDecPlace,setUnit,setID,SetValueObject,SetValueProperty) {
...
"gaugetext": Qt.binding(function(){return SetValueObject[SetValueProperty] + " " + setUnit})
....
}
This works well, but I want to set numbers of decimals. So I tried this:
"gaugetext": Qt.binding(function(){return "(" + SetValueObject[SetValueProperty]+ ")" + ".toFixed(" + setDecPlace + ")" + " " + setUnit})
But this results that the correct Value is shown with ".toFixed(value)".
You return a String "(<someValue>).toFixed(<someOtherValue>) <someThirdValue>".
You probably want to omit some of your " since you probably want to have the pieces that you mark as string to be executed instead.
return SetValueObject[SetValueProperty].toFixed(setDecPlace) + " " + setUnit
~~~(untetested)
Most likely however using a "finalize creation" function is a bad approach.

pass toString(number) in innerHTML

This is my first post, I am "new" to javascript but experienced in actionscript. I'm having an issue that should not be one, can't get my head around it.
In the code below all I want to do is pass a integer variable (stackID) as an img id attribute inside innerHTML. It must be some detail I just don't know but I have searched for hours on google to no avail. Help would be greatly aprreciated. Whatever I do, it seems I can only pass <img id="string">, which of course is not what I want. Can't pass <img id=numVariable>, or <img id=String(numVariable)>, there must surely be a way to do this ?
Thanks folks !
Here is the line of code :
div.innerHTML='<img src="img/xbut.png" id=String(stackID) onclick="close(this.id)">';
You're trying to pass it in the string.
What you want to do is:
div.innerHTML='<img src="img/xbut.png" id="' + String(stackID) + '" onclick="close(' + this.id + ')">';
Oh and since you're already making a string, you don't need the String() convert, you can just id="' + stackID + '"
Javascript is loosely typed and will automatically convert (to the best of its abilities) anything you want to interpolate in a string:
var num = 1;
"sup" + num; // => "sup1"
It's cool, but be careful though, it can lead to some weird surprises:
var obj = {};
"sup" + obj.missingKey; // => "supundefined"
In that case, obj.missingKey === undefined, and String(undefined) === "undefined", so "sup" + obj.missingKey becomes in fact "sup" + "undefined"
javascript has not string interpolation. Concatenate stackId like:
div.innerHTML='<img src="img/xbut.png" id="' + stackID + '" onclick="close(this.id)">';
Use '+' to join chunks like this:
div.innerHTML='<img src="img/xbut.png" id="' + String(stackID) + '" onclick="close(' + this.id + ')">';
Try this, you make the variable is dynamic:
var x = 0;
x = x +1;
div.innerHTML='<img src="img/xbut.png" id="name_' + x + '" onclick="close(this.id)">';

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 variable, different values, same line

First of all, let me apologize for the title, as it isn't so explanatory, but I could not say it in another way.
The deal is: I am doing a javascript application, in which I have an object called "ocorrencia", which was defined like this:
var ocorrencia = new Object();
that object has several children, being filled by a method:
ocorrencia.idOcorrencia = ""+ year + month + day + hour + minute + second + milisec;
idOcorrencia is the one I am having problems with, because I am running a DataBase insert with this value, and I use it 2 times in the same insert, like:
var sql = 'INSERT INTO OCORRENCIAS (id, ocorrencia, data, resolucao, urgencia, foto) VALUES (' + ocorrencia.idOcorrencia + ', "' + ocorrencia.descricao + '", "' + ocorrencia.data + '", "' + ocorrencia.resolucao + '", "' + ocorrencia.grauUrg + '", "' + ocorrencia.idOcorrencia + '.jpg"' +')';
The insert runs great, an I have all the data inserted in the DB, BUT "id" and "foto" (which were supposed to get equal values) are giving me different values by 2 or 3 miliseconds.
How can this happen, as I am not changing "ocorrencia.idOcorrencia" ?
This is beeing tested in an Android device.
EDIT: Tested on Windows browser and the problem doesn't appear to happen.
Thank you
I guess you fill idOcorrencia on runtime? So the lag is producing this difference.
Try using a hash for the id or set it before running the SQL-query.

Calling Html Helper Method from Javascript Function

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.

Categories

Resources