in my .aspx page I've got something like this- I create dynamically javascript variables which look like this
var region11 = ''
var region12 = ''
So when I click on a region of svg I get the id of the region which is for example 11 and I should set the href property region11 variable.
Unfortunately the javascript debbuger says that it cannot find region variable
<% var regions = ICYGEN.MRF.Data.CityData.SelectProvinces(); %>
<% foreach (ICYGEN.MRF.Data.Entities.Region region in regions)
{ %>
var region<%= region.RegionID %> = '<%= MRFUrlHelper.Elections2015_RegionUrl(region.RegionID,region.Title, 1026) %>';
<% } %>
jQuery(window).load(function () {
var svgMap = jQuery('#svg-map').contents().get(0);
alert(region11);//it's Ok here and it finds it
jQuery('svg path', svgMap).click(function () {
var ids = String(this.id);
location.href = region+ids; //can not find
You're using region instead of region11 in your click event handler:
location.href = region+ids; //can not find
Change to:
location.href = region11+ids; //can not find
Or I suppose:
location.href = region<%= region.RegionID %>+ids; //can not find
Related
I have a classic ASP page which is written in VB script, and there is a script tag which Java Script is used as below.
I am trying to cast VB Script Array(codeList) to Java Script Array(availableCode) with for loop but the first row of VB Script Array keeps being casted in every row of Java Script Array.
$(document).ready( function(){
var availableCode = new Array();
for (idx=0; idx < 3; idx++)
{
availableCode[idx]=<%=codeList(idx)%>;
alert (idx);
alert (<%=codeList(idx)%>);
alert (availableCode[idx]);
}
});
I tried the below as well but getting a syntax error.
availableCode[idx]=<%=codeList(%> idx <%)%>;
Could anyone advise how to cast expected values? Thank you.
I think you need to do it the other way round ie create your index (idx) within VBScript, then write it to the Javascript:
$(document).ready( function(){
var availableCode = new Array();
<%
dim idx
for idx = 0 to UBound(codeList)
%>
availableCode[<%=idx %>] = <%= codeList(idx) %>;
alert (<%= idx %>);
alert (<%= codeList(idx) %>);
alert (availableCode[<%= idx %>]);
<% next %>
});
if the elements of codeList are string values, you will need to quote to prevent javascript errors
$(document).ready( function(){
var availableCode = new Array();
<%
dim idx
for idx = 0 to UBound(codeList)
%>
availableCode[<%=idx %>] = '<%= codeList(idx) %>'; // quote here
alert (<%=idx %>);
alert ('<%= codeList(idx) %>'); // quote here
alert (availableCode[<%= idx %>]);
<% next %>
});
The issue you were having I believe was that idx did not exist on the server-side (VBScript) and therefore when used in this statement <%= codeList(idx) %>, idx was being converted implicitly to 0. Thus always returning the value in codeList(0).
You can use Join() to output your array: this will also make your client-side code more compact.
$(document).ready( function(){
var availableCode = ['<%=Join(codeList, "','")%>'];
I am New to This
I am writing this code
"OnClientClick="return OpenRadWindow('ImportSDS.aspx?id=##', 'RWImport');" >
i have js variable
var id = "<%=this.id %>";
So I want to Use This id Varable at ## How It Is Possible Please Help.
If you have the variable available, you should just use it in the method instead.
I don't have enough of your code really to go on but something like this.
function OpenRadWindow(url, x) {
var id = "<%= this.id %>";
url = url + id;
... then your code here
}
Then for your OnClientClick
OnClientClick="return OpenRadWindow('ImportSDS.aspx?id=', 'RWImport');"
var nUserID = '<% if (ids != null) {ids.userID.ToString();}%>'
Note: ids is not null nor ids.userID is null but em getting nUserID = ""
Is there any way to convert C# string to Javascript string?
For write it direct on page you need to use the Response.Write as
var nUserID = <% if (ids != null) {Response.Write(ids.userID.ToString());}%>
beside that, if you need to add at the end ; or place it inside quotas is up to you.
The <% %> did not write on page, just run the code.
The <%= %> write on the page as the Response.Write
var nUserID = '<%=((ids != null) ? ids.userID.ToString() : "0")%>';
Use Something Like this:
<script type="text/javascript">
var nUserID = '<%=ids.userID.ToString()%>';
if(!nUserID)
{
//check for null in javascript
alert(nUserID);
}
</script>
More appropriate solution would be to to save your ids.userID to data attribute of some related html element or just create invisible div element with all C# data you want to use in your javascript code as data attributes. And then use jQuery to extract that data in scripts.
<div id="settings" style="display: none;" data-user-id="<%= ids.userID %>"></div>
and javascript:
var nUserID = $('#settings').data('user-id');
I would really appreciate some guidance. This is probably simple to some of you, but i can't figure it out.
Thanks for any input.
THE REQUIREMENT
I have a multi-tabbed control. One each tab, I have a custom reportviewer control.
I have added a custom attribute to the reportviewer in the code behind called "data-report-param".
I need to access the value of custom attribute "data-report-param" on the current tab on the client-side using javascript.
I have tried several ways including the following, but can't get to the value that is being created in the DOM.
MY CODE
//Attempt 1
var reportparamattribute = $('#ReportViewer1');
var reportparametervalue = reportparamattribute.getAttribute('data-report-param');
//Attempt 2
var reportparamattribute = document.getElementById('<%= ReportViewer1.ClientID %>');
var reportparametervalue = reportparamattribute.getAttribute('data-report-param');
//Also tried accessing the dataset
var reportparametervalue = reportparamattribute.dataset.report-param;
WHAT IS BEING PRODUCED IN THE DOM
('ctl00_m_g_66e41117_8ff5_4650_bf4d_7a4a25e326f3_ctl01_ReportViewer1_ctl04').control.HideActiveDropDown();" data-report-param="1068" interactivedeviceinfos="(Collection)">
('ctl00_m_g_9d6a6c3c_11d0_4e03_bbd2_b907172c437d_ctl01_ReportViewer1_ctl04').control.HideActiveDropDown();" data-report-param="1068" interactivedeviceinfos="(Collection)">
UPDATE- WORKING CODE BELOW
The key was passing the custom data attribute from the code behind and then accessing it in the $.cache as #popnoodles below indicated, and passing the clientID of the reportviewer into the javascript function to get to the current instance of the webpart child controls.
<input type="hidden" id="<%= ASP_SSRS.ClientID %>_myDataState"
onchange="compareUnitValues(this.id, this.parentNode.id, '<%= ReportViewer1.ClientID %>', '<%= ASP_SSRS.ClientID %>', '<%= btnSendHiddenField.ClientID %>');" />
<script type ="text/javascript">
function compareUnitValues(elemt, parent, reportviewerID, value1, value2) {
var myDataUnit = $("#" + elemt),
parentObject = $("#" + parent),
reportviewerObject = $("#" + reportviewerID),
ssrs = $("#" + value1),
btnSend = $("#" + value2);
var myDataUnitValue = myDataUnit.val();
var myDataUnitJSON = jQuery.parseJSON(myDataUnitValue);
var currentmyDataUnit = myDataUnitJSON.currentUnit.objectId;
var sessioncurrentObjectId = document.getElementById('<%= hiddenCurrentObjectId.ClientID %>').value;
ssrs.val(myDataUnitValue);
var currentReportViewerParam = $("#" + reportviewerID).attr("data-report-param");
if (currentmyDataUnit != currentReportViewerParam) {
btnSend.trigger("click");
}
}
FROM CODE BEHIND CREATE THE CUSTOM DATA ATTRIBUTE
ReportViewer1.Attributes.Add("data-report-param", parsedObjectId)
getAttribute will only give you the value that was in the generated or modified HTML not what is in the DOM. The data method never updates the HTML.
jQuery creates an empty object $.cache, which is used to store the values you set via the data method. Each DOM element you add data to is assigned a unique ID which is used as a key in the $.cache object.
Setting
$('#ReportViewer1').data('report-param', 1234);
Getting
var id = $('#ReportViewer1').data('report-param');
If you can use jquery why not just:
$("#reportviewer1").data('report-param');
I have an ASP script that checks the username of the authenticated user, now I want to pass the username of the authenticated user into a javascript variable/string
ASP:
<%
Function isAuthorized()
Dim ipos
Dim ustring
Dim uname
Dim ilngth
ustring = Request.ServerVariables("AUTH_USER")
ipos = Instr(ustring, "\") + 1
ilngth = Len(ustring) + 1
uname = Trim(Mid(ustring, ipos, ilngth - ipos))
End Function
%>
Javascript:
<script>
var chk = <%uname%>;
alert(chk);
</script>
How can I make this work correctly and where in my code exactly do these elements go?
Set a property to get the username: eg in c#
Public string Username{ get { return Page.User.Identity.Name;} }
Call this in JavaScript using <%=Username%>
Try this:
<script>
var chk = "<%= uname %>";
alert(chk);
</script>