assignment between jscript variables in a codebehind-created jscri

G

Guest

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
 
G

Guest

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
 
G

Guest

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 said:
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 said:
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
 
B

Bruce Barker

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)
 
G

Guest

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 said:
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 said:
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 said:
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

intrader

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top