Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2005

Tip: Looking for answers? Try searching our database.

assignment between jscript variables in a codebehind-created jscri

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RFS666 - 14 Oct 2005 14:41 GMT
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       
Sreejith Ram - 14 Oct 2005 15:35 GMT
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

> Hello,
>
[quoted text clipped - 39 lines]
>
> RFS666       
RFS666 - 14 Oct 2005 15:57 GMT
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:

> This is more like a logical issue than asp.net question
>
[quoted text clipped - 53 lines]
> >
> > RFS666       
Sreejith Ram(MCSD.NET) - 14 Oct 2005 18:11 GMT
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

> Hello Sreejith,
>
[quoted text clipped - 83 lines]
> > >
> > > RFS666       
RFS666 - 17 Oct 2005 10:27 GMT
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   
Bruce Barker - 14 Oct 2005 18:10 GMT
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)

> Hello,
>
[quoted text clipped - 45 lines]
>
> RFS666
intrader - 14 Oct 2005 19:42 GMT
> Hello,
>
[quoted text clipped - 39 lines]
>
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.