473,241 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

assignment between jscript variables in a codebehind-created jscri

Hello,

After I posted yesterday "using C# class in jscript", I have a new problem:
I have a C# class - DBResult - that contains (and other variables) a string
array (and other variables), that contains data from a database query which
is done in C# in codebehind. I create a jscript - script that is injected
into the aspx-page. I need this to fill an activeX-control with data.
I assign the string-array (and - for testing - a single stringArray-Entry)
to variables in jscript. This works. But when I want to reassign these
jscript variables to other jscript variables, these newly assigned variables
only return "undefined", when I output them via "alert". I have to process
the data in jscript, so I can make it appear in my activeX control (a
2D-graph visualisation)

In Codebehind, I write the following (for testing):

// initialize DBResult for testing:
string[] strings = { "one", "two" };
DBResult res = new DBResult(strings);
// The array is publicly available as a Property named Strings

// create JScript-Code:
string script = "<script language=jscript>";
script += "function loadDB() {";
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
script += "alert(testStrings)"; // returns "System.String[]"
script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
script += "alert(testString)"; // returns "one"
script += "s1 = testStrings[0];"; // asignment from jscript variable to
jscript variable
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY
script += "return false;"; // verhindert Postback
script += "}";
script += "</script>";

RegisterClientScriptBlock("Block1", script);
this.button1.Attributes.Add("OnClick", "return loadDB();");
I hope I made clear the problem, so I hope somebody can help.

Thanks in advance.

RFS666
Nov 19 '05 #1
6 1831
This is more like a logical issue than asp.net question

The best way to debug would be to do a view source of the page from IE and
see how this line is being rendered

script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!

looks to me that you are probably assigning a single string to testString ,
not an array ..that could be the reason for following to return 'undefined'..
testStrings[0] doesnt exist, unless testStrings is an array

script += "s1 = testStrings[0];"; // asignment from jscript variable to
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY

"RFS666" wrote:
Hello,

After I posted yesterday "using C# class in jscript", I have a new problem:
I have a C# class - DBResult - that contains (and other variables) a string
array (and other variables), that contains data from a database query which
is done in C# in codebehind. I create a jscript - script that is injected
into the aspx-page. I need this to fill an activeX-control with data.
I assign the string-array (and - for testing - a single stringArray-Entry)
to variables in jscript. This works. But when I want to reassign these
jscript variables to other jscript variables, these newly assigned variables
only return "undefined", when I output them via "alert". I have to process
the data in jscript, so I can make it appear in my activeX control (a
2D-graph visualisation)

In Codebehind, I write the following (for testing):

// initialize DBResult for testing:
string[] strings = { "one", "two" };
DBResult res = new DBResult(strings);
// The array is publicly available as a Property named Strings

// create JScript-Code:
string script = "<script language=jscript>";
script += "function loadDB() {";
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
script += "alert(testStrings)"; // returns "System.String[]"
script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
script += "alert(testString)"; // returns "one"
script += "s1 = testStrings[0];"; // asignment from jscript variable to
jscript variable
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY
script += "return false;"; // verhindert Postback
script += "}";
script += "</script>";

RegisterClientScriptBlock("Block1", script);
this.button1.Attributes.Add("OnClick", "return loadDB();");
I hope I made clear the problem, so I hope somebody can help.

Thanks in advance.

RFS666

Nov 19 '05 #2
Hello Sreejith,

indeed, I use the page source view for "debugging":
The line you highlighted really returns a string value, the first string
that is contained in string array of DBResult. This was only for testing. The
code behaves right.
There is a similar line in my example that assigns the complete string-array
of DBResult to a jscript variable:
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
In page source view, the variable testStrings contains the following string:
" System.String[]", that means, a string representation of the datatype
(that should be contained in the variable instead!).
But the line
script += "testString = " + "\"" res.Strings[0] + "\"" + ";";
proves that the array can be resolved to its values.
So I don't know why this doesn't work in clean jscript when I write:
script += "s1 = testStrings[0];";

The only thing that could be imaginable (in my opinion) is, that this line
returns the first letter of testStrings, that means the "S" from the string
"System.Object[0]". But this is not
useful. But I don't know why this returns "undefined".

Hope, somebody can help. Thanks in advance.
Regards

RFS666

The problem lies at an other line:
"Sreejith Ram" wrote:
This is more like a logical issue than asp.net question

The best way to debug would be to do a view source of the page from IE and
see how this line is being rendered

script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!

looks to me that you are probably assigning a single string to testString ,
not an array ..that could be the reason for following to return 'undefined'..
testStrings[0] doesnt exist, unless testStrings is an array

script += "s1 = testStrings[0];"; // asignment from jscript variable to
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY

"RFS666" wrote:
Hello,

After I posted yesterday "using C# class in jscript", I have a new problem:
I have a C# class - DBResult - that contains (and other variables) a string
array (and other variables), that contains data from a database query which
is done in C# in codebehind. I create a jscript - script that is injected
into the aspx-page. I need this to fill an activeX-control with data.
I assign the string-array (and - for testing - a single stringArray-Entry)
to variables in jscript. This works. But when I want to reassign these
jscript variables to other jscript variables, these newly assigned variables
only return "undefined", when I output them via "alert". I have to process
the data in jscript, so I can make it appear in my activeX control (a
2D-graph visualisation)

In Codebehind, I write the following (for testing):

// initialize DBResult for testing:
string[] strings = { "one", "two" };
DBResult res = new DBResult(strings);
// The array is publicly available as a Property named Strings

// create JScript-Code:
string script = "<script language=jscript>";
script += "function loadDB() {";
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
script += "alert(testStrings)"; // returns "System.String[]"
script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
script += "alert(testString)"; // returns "one"
script += "s1 = testStrings[0];"; // asignment from jscript variable to
jscript variable
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY
script += "return false;"; // verhindert Postback
script += "}";
script += "</script>";

RegisterClientScriptBlock("Block1", script);
this.button1.Attributes.Add("OnClick", "return loadDB();");
I hope I made clear the problem, so I hope somebody can help.

Thanks in advance.

RFS666

Nov 19 '05 #3
lets look at the javascript code generated:

function loadDB()
{
testStrings = "System.String[]";
alert(testStrings)";
testString = "one";
alert(testString)";
s1 = testStrings[0];
}

the last line fail because testStrings is not an array, but a simple string
whose value is "System.String[]". thats because the codebehind line

script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this
works!

called ToString() on res.Strings which returned its type name. you need to
build a javascript array. say:

script += "testStrings = {"
string sep = "";
for (int i=0; i < res.String.Length; ++i)
{
script += sep + "testString = " + "\"" res.Strings[0] + "\"";
sep = ",";
}
script += "};"

or use the handy builtin feature, RegisterArrayDeclaration() to create the
array.

-- bruce (sqlwork.com)
"RFS666" <RF****@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com...
Hello,

After I posted yesterday "using C# class in jscript", I have a new
problem:
I have a C# class - DBResult - that contains (and other variables) a
string
array (and other variables), that contains data from a database query
which
is done in C# in codebehind. I create a jscript - script that is injected
into the aspx-page. I need this to fill an activeX-control with data.
I assign the string-array (and - for testing - a single stringArray-Entry)
to variables in jscript. This works. But when I want to reassign these
jscript variables to other jscript variables, these newly assigned
variables
only return "undefined", when I output them via "alert". I have to process
the data in jscript, so I can make it appear in my activeX control (a
2D-graph visualisation)

In Codebehind, I write the following (for testing):

// initialize DBResult for testing:
string[] strings = { "one", "two" };
DBResult res = new DBResult(strings);
// The array is publicly available as a Property named Strings

// create JScript-Code:
string script = "<script language=jscript>";
script += "function loadDB() {";
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this
works!
script += "alert(testStrings)"; // returns "System.String[]"
script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this
works!
script += "alert(testString)"; // returns "one"
script += "s1 = testStrings[0];"; // asignment from jscript variable to
jscript variable
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY
script += "return false;"; // verhindert Postback
script += "}";
script += "</script>";

RegisterClientScriptBlock("Block1", script);
this.button1.Attributes.Add("OnClick", "return loadDB();");
I hope I made clear the problem, so I hope somebody can help.

Thanks in advance.

RFS666

Nov 19 '05 #4
oh! sorry, i over looked the variable names testStrings & testString

Some thing look at is
When view source, How does the result for this line looks like?

script += "testStrings = " + "\"" + res.Strings + "\"" + ";";

When you use res.Strings this way , asp.net renders res.Strings.ToString()
which returns the type of it as System.String[] and the HTML output would be
looking like

testStrings = "System.String[]";

Instead, the output should be looking like below for it to be a javascript
array

testStrings = new Array('one', 'two', 'three')

This can be done by building this above string by looping through the
res.Strings array.

script += "var strArray = new Array(")
' add server-side Array Members as to the client-side script
for (intCnt = 0;intCnt < res.Strings.Length ;intCnt ++)
{
' If not first element ,put a comma before the next value
if intCnt > 0
script += ","
script += "'" + res.Strings(intCnt).ToString() + "'"
}
script += ")";

Declaring varaiable script as StrignBuilder would give better performance
"RFS666" wrote:
Hello Sreejith,

indeed, I use the page source view for "debugging":
The line you highlighted really returns a string value, the first string
that is contained in string array of DBResult. This was only for testing. The
code behaves right.
There is a similar line in my example that assigns the complete string-array
of DBResult to a jscript variable:
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
In page source view, the variable testStrings contains the following string:
" System.String[]", that means, a string representation of the datatype
(that should be contained in the variable instead!).
But the line
script += "testString = " + "\"" res.Strings[0] + "\"" + ";";
proves that the array can be resolved to its values.
So I don't know why this doesn't work in clean jscript when I write:
script += "s1 = testStrings[0];";

The only thing that could be imaginable (in my opinion) is, that this line
returns the first letter of testStrings, that means the "S" from the string
"System.Object[0]". But this is not
useful. But I don't know why this returns "undefined".

Hope, somebody can help. Thanks in advance.
Regards

RFS666

The problem lies at an other line:
"Sreejith Ram" wrote:
This is more like a logical issue than asp.net question

The best way to debug would be to do a view source of the page from IE and
see how this line is being rendered

script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!

looks to me that you are probably assigning a single string to testString ,
not an array ..that could be the reason for following to return 'undefined'..
testStrings[0] doesnt exist, unless testStrings is an array

script += "s1 = testStrings[0];"; // asignment from jscript variable to
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY

"RFS666" wrote:
Hello,

After I posted yesterday "using C# class in jscript", I have a new problem:
I have a C# class - DBResult - that contains (and other variables) a string
array (and other variables), that contains data from a database query which
is done in C# in codebehind. I create a jscript - script that is injected
into the aspx-page. I need this to fill an activeX-control with data.
I assign the string-array (and - for testing - a single stringArray-Entry)
to variables in jscript. This works. But when I want to reassign these
jscript variables to other jscript variables, these newly assigned variables
only return "undefined", when I output them via "alert". I have to process
the data in jscript, so I can make it appear in my activeX control (a
2D-graph visualisation)

In Codebehind, I write the following (for testing):

// initialize DBResult for testing:
string[] strings = { "one", "two" };
DBResult res = new DBResult(strings);
// The array is publicly available as a Property named Strings

// create JScript-Code:
string script = "<script language=jscript>";
script += "function loadDB() {";
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
script += "alert(testStrings)"; // returns "System.String[]"
script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
script += "alert(testString)"; // returns "one"
script += "s1 = testStrings[0];"; // asignment from jscript variable to
jscript variable
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY
script += "return false;"; // verhindert Postback
script += "}";
script += "</script>";

RegisterClientScriptBlock("Block1", script);
this.button1.Attributes.Add("OnClick", "return loadDB();");
I hope I made clear the problem, so I hope somebody can help.

Thanks in advance.

RFS666

Nov 19 '05 #5
On Fri, 14 Oct 2005 06:41:04 -0700, RFS666 wrote:
Hello,

After I posted yesterday "using C# class in jscript", I have a new problem:
I have a C# class - DBResult - that contains (and other variables) a string
array (and other variables), that contains data from a database query which
is done in C# in codebehind. I create a jscript - script that is injected
into the aspx-page. I need this to fill an activeX-control with data.
I assign the string-array (and - for testing - a single stringArray-Entry)
to variables in jscript. This works. But when I want to reassign these
jscript variables to other jscript variables, these newly assigned variables
only return "undefined", when I output them via "alert". I have to process
the data in jscript, so I can make it appear in my activeX control (a
2D-graph visualisation)

In Codebehind, I write the following (for testing):

// initialize DBResult for testing:
string[] strings = { "one", "two" };
DBResult res = new DBResult(strings);
// The array is publicly available as a Property named Strings

// create JScript-Code:
string script = "<script language=jscript>";
script += "function loadDB() {";
script += "testStrings = " + "\"" + res.Strings + "\"" + ";"; // this works!
script += "alert(testStrings)"; // returns "System.String[]"
script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
script += "alert(testString)"; // returns "one"
script += "s1 = testStrings[0];"; // asignment from jscript variable to
jscript variable
script += "alert(s1)"; // returns undefined, BUT I DON'T KNOW WHY
script += "return false;"; // verhindert Postback
script += "}";
script += "</script>";

RegisterClientScriptBlock("Block1", script);
this.button1.Attributes.Add("OnClick", "return loadDB();");
I hope I made clear the problem, so I hope somebody can help.

Thanks in advance.

RFS666

I would render script code to initialize a jscript array via an array
literal: var myJscriptArray = ['first','second'...];
You must dynamically build this array.
There is a lot of interesting code that deals with similar issues in
Ajax.net where they support a number of classes downloaded to the client.

Nov 19 '05 #6
Hello,
now, the code works.
I dynamically created the array in a loop as proposed!

Thanks to all who posted! Please keep up the good work in providing such
helpful
examples!

Regards

RFS666
Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Harag | last post by:
Hi All. I'm stating out doing some web developing. I was wondering which of the server side languages should I concentrate on and learn. I Know CSS, HTML, T-SQL I can look at the client...
1
by: Jenny | last post by:
Hi all, I wrote a simple asp.net with JScript. But it doesn't work properly. And I don't know why! The CodeBehind: Public Class WebForm1 Inherits System.Web.UI.Page ....
2
by: moondaddy | last post by:
I'm using vb.net and have an aspx page where I want to call a function in the code behind to do something on the backend and I want to call this function from a jscript function in the aspx page. ...
4
by: RFS666 | last post by:
Hello, I have the following problem: I have a web project where I display an activeX control that displays 2D-graphs on an aspx-page. I use jscript to access and modify the properties of the...
7
by: RFS666 | last post by:
Hello, I would like to use variables with a type in jscript.NET. I declare them as follows: var x : double = 5.03; This doesn't work in my script, that I write to the page in codebehind with...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
1
by: Andrew Wan | last post by:
How can VBScript code access JScript code variables in the same ASP page? <SCRIPT LANGAUGE="VBScript"> Dim a a = 10 </SCRIPT> <SCRIPT LANGUAGE="JScript"> Response.Write(a); </SCRIPT>
0
by: smoltz | last post by:
Hello All, New to the forum... looking for some help on executing JScript from C#. Nothing web related here, this is entirely within the confines of a windows app running on a single machine. ...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.