assignment between jscript variables in a codebehind-created jscri

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RFS666

    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(string s);
    // The array is publicly available as a Property named Strings

    // create JScript-Code:
    string script = "<script language=jscrip t>";
    script += "function loadDB() {";
    script += "testString s = " + "\"" + res.Strings + "\"" + ";"; // this works!
    script += "alert(testStri ngs)"; // returns "System.Str ing[]"
    script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
    script += "alert(testStri ng)"; // 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>";

    RegisterClientS criptBlock("Blo ck1", script);
    this.button1.At tributes.Add("O nClick", "return loadDB();");


    I hope I made clear the problem, so I hope somebody can help.

    Thanks in advance.

    RFS666
  • Sreejith Ram

    #2
    RE: assignment between jscript variables in a codebehind-created jscri

    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:
    [color=blue]
    > 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(string s);
    > // The array is publicly available as a Property named Strings
    >
    > // create JScript-Code:
    > string script = "<script language=jscrip t>";
    > script += "function loadDB() {";
    > script += "testString s = " + "\"" + res.Strings + "\"" + ";"; // this works!
    > script += "alert(testStri ngs)"; // returns "System.Str ing[]"
    > script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
    > script += "alert(testStri ng)"; // 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>";
    >
    > RegisterClientS criptBlock("Blo ck1", script);
    > this.button1.At tributes.Add("O nClick", "return loadDB();");
    >
    >
    > I hope I made clear the problem, so I hope somebody can help.
    >
    > Thanks in advance.
    >
    > RFS666[/color]

    Comment

    • RFS666

      #3
      RE: assignment between jscript variables in a codebehind-created jscri

      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 += "testString s = " + "\"" + 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.Obj ect[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:
      [color=blue]
      > 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:
      >[color=green]
      > > 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(string s);
      > > // The array is publicly available as a Property named Strings
      > >
      > > // create JScript-Code:
      > > string script = "<script language=jscrip t>";
      > > script += "function loadDB() {";
      > > script += "testString s = " + "\"" + res.Strings + "\"" + ";"; // this works!
      > > script += "alert(testStri ngs)"; // returns "System.Str ing[]"
      > > script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
      > > script += "alert(testStri ng)"; // 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>";
      > >
      > > RegisterClientS criptBlock("Blo ck1", script);
      > > this.button1.At tributes.Add("O nClick", "return loadDB();");
      > >
      > >
      > > I hope I made clear the problem, so I hope somebody can help.
      > >
      > > Thanks in advance.
      > >
      > > RFS666[/color][/color]

      Comment

      • Bruce Barker

        #4
        Re: assignment between jscript variables in a codebehind-created jscri

        lets look at the javascript code generated:

        function loadDB()
        {
        testStrings = "System.Str ing[]";
        alert(testStrin gs)";
        testString = "one";
        alert(testStrin g)";
        s1 = testStrings[0];
        }

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

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

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

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

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

        -- bruce (sqlwork.com)


        "RFS666" <RFS666@discuss ions.microsoft. com> wrote in message
        news:2571C14B-E7CC-4D86-A0CC-41762C7EB10D@mi crosoft.com...[color=blue]
        > 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(string s);
        > // The array is publicly available as a Property named Strings
        >
        > // create JScript-Code:
        > string script = "<script language=jscrip t>";
        > script += "function loadDB() {";
        > script += "testString s = " + "\"" + res.Strings + "\"" + ";"; // this
        > works!
        > script += "alert(testStri ngs)"; // returns "System.Str ing[]"
        > script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this
        > works!
        > script += "alert(testStri ng)"; // 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>";
        >
        > RegisterClientS criptBlock("Blo ck1", script);
        > this.button1.At tributes.Add("O nClick", "return loadDB();");
        >
        >
        > I hope I made clear the problem, so I hope somebody can help.
        >
        > Thanks in advance.
        >
        > RFS666[/color]


        Comment

        • Sreejith Ram(MCSD.NET)

          #5
          RE: assignment between jscript variables in a codebehind-created j

          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 += "testString s = " + "\"" + res.Strings + "\"" + ";";

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

          testStrings = "System.Str ing[]";

          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.Len gth ;intCnt ++)
          {
          ' If not first element ,put a comma before the next value
          if intCnt > 0
          script += ","
          script += "'" + res.Strings(int Cnt).ToString() + "'"
          }
          script += ")";

          Declaring varaiable script as StrignBuilder would give better performance


          "RFS666" wrote:
          [color=blue]
          > 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 += "testString s = " + "\"" + 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.Obj ect[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:
          >[color=green]
          > > 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:
          > >[color=darkred]
          > > > 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(string s);
          > > > // The array is publicly available as a Property named Strings
          > > >
          > > > // create JScript-Code:
          > > > string script = "<script language=jscrip t>";
          > > > script += "function loadDB() {";
          > > > script += "testString s = " + "\"" + res.Strings + "\"" + ";"; // this works!
          > > > script += "alert(testStri ngs)"; // returns "System.Str ing[]"
          > > > script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
          > > > script += "alert(testStri ng)"; // 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>";
          > > >
          > > > RegisterClientS criptBlock("Blo ck1", script);
          > > > this.button1.At tributes.Add("O nClick", "return loadDB();");
          > > >
          > > >
          > > > I hope I made clear the problem, so I hope somebody can help.
          > > >
          > > > Thanks in advance.
          > > >
          > > > RFS666[/color][/color][/color]

          Comment

          • intrader

            #6
            Re: assignment between jscript variables in a codebehind-created jscri

            On Fri, 14 Oct 2005 06:41:04 -0700, RFS666 wrote:
            [color=blue]
            > 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(string s);
            > // The array is publicly available as a Property named Strings
            >
            > // create JScript-Code:
            > string script = "<script language=jscrip t>";
            > script += "function loadDB() {";
            > script += "testString s = " + "\"" + res.Strings + "\"" + ";"; // this works!
            > script += "alert(testStri ngs)"; // returns "System.Str ing[]"
            > script += "testString = " + "\"" res.Strings[0] + "\"" + ";"; // this works!
            > script += "alert(testStri ng)"; // 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>";
            >
            > RegisterClientS criptBlock("Blo ck1", script);
            > this.button1.At tributes.Add("O nClick", "return loadDB();");
            >
            >
            > I hope I made clear the problem, so I hope somebody can help.
            >
            > Thanks in advance.
            >
            > RFS666[/color]
            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.

            Comment

            • RFS666

              #7
              RE: assignment between jscript...

              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

              Comment

              Working...