Solved

Using string.Format when using stremreader to load .js file

Posted on 2006-07-04
5
392 Views
Last Modified: 2010-05-18
Hi:

I am loading a .js file into my web application but want to replace some of the javascript text with custom values.  I keep getting an error "Input string was not in a correct format.".

Here is the code:

protected override void Render(HtmlTextWriter output)
{
      base.Render(output);

      try
      {

            string _replaceFunction = this.ClientID;
            string _replaceControl = this.ClientID.Replace("_", "$");

            output.Write(string.Format(this.GetScriptFromFile(), _replaceFunction, _replaceControl));
            
//                        //      javascript to pass flash argument to postback reference
//                        output.Write(fsCommandScript1.Replace("REPLACETEXT", this.ClientID).Replace("REPLACEID", this.ClientID.Replace("_", "$")));

            //      Required to cause a postback
            Page.GetPostBackEventReference(this, "");
      }
      catch(Exception ex)
      {
            // Something bad happened. Let's tell the user what that was.
            output.Write("Error building FlashControl:<br>");
            output.Write(ex.Message);
      }
}

private string GetScriptFromFile()
{
      string jsKey = ConfigurationSettings.AppSettings[this.jsFileKey];

      string js = string.Empty;
      
      if(jsKey != null)
      {
            string path = Page.Request.MapPath(jsKey);
            
            StreamReader sr = new StreamReader(path);
            while(sr.Peek() >= 0)
            {
                  js += sr.ReadLine();
            }
            sr.Close();
      }

      return js;
}


Here is the javascript file:

<SCRIPT LANGUAGE=JavaScript>
      <!--
      var InternetExplorer = navigator.appName.indexOf('Microsoft') != -1;
      function {0}_DoFSCommand(command, args)
      {
            var fscmdObj = InternetExplorer ? document.all.{1} : document.{1};
            if (command == 'Click')
            {
                  // looking for the fields
                  for (var i = 0; i < document.forms.length; i++)
                  {
                        var frm = document.forms[i];
                        var fld = frm['{1}'];
                        if (fld != null)
                        {
                              fld.value = args;
                              break;
                        }
                  }
                  var __mustPostBack = true;
                  try
                  {
                        if (OnClick != null)
                              __mustPostBack = OnClick('{1}', args);
                  }
                  catch (ex)
                  {
                  }
                  if (__mustPostBack)
                        __doPostBack('{1}', args);
            }
      }
      // Hook for Internet Explorer
      if (navigator.appName && navigator.appName.indexOf('Microsoft') != -1 && 
            navigator.userAgent.indexOf('Windows') != -1 && navigator.userAgent.indexOf('Windows 3.1') == -1)
      {
            document.write('<SCRIPT LANGUAGE=VBScript\> \n');
            document.write('on error resume next \n');
            document.write('Sub {0}_DoFSCommand(ByVal command, ByVal args)\n');
            document.write('  call {0}_DoFSCommand(command, args)\n');
            document.write('end sub\n');
            document.write('</SCRIPT\> \n');
      }
      //-->
</SCRIPT>


You can see I am trying to add those curly braces at the location where I want to be replaced.  I have even tried double curly braces like function {{0}}_DoFSCommand but doesn't like it.  Any ideas how to render this out?

Thanks!

0
Comment
Question by:daveokst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17037549
Hi there,

apologies but i cant see where {0}_ is being set?
0
 

Author Comment

by:daveokst
ID: 17038254
Hi, thanks for responding.

The'{0}' is in the javascript file that gets read in through the stream reader...

<SCRIPT LANGUAGE=JavaScript>
     <!--
     var InternetExplorer = navigator.appName.indexOf('Microsoft') != -1;
     function {0}_DoFSCommand
    ...


What I want is for the "function {0}_DoFSCommand" to end up looking like "function myControlId_DoFSCommand".  I could use string.Replace, but would prefer to use string.Format.  

Does this make sense?
0
 

Author Comment

by:daveokst
ID: 17038425
I got it figured out... apparently the function {0}_DoFSCommand wasn't causing the problem, it was the function's curly braces.  So what I had to do was add double curly braces to the whole js file... like this:

<SCRIPT LANGUAGE=JavaScript>
      <!--
      var InternetExplorer = navigator.appName.indexOf('Microsoft') != -1;
      function {0}_DoFSCommand(command, args)
      {{
            var fscmdObj = InternetExplorer ? document.all.{1} : document.{1};
            if (command == 'Click')
            {{
                  for (var i = 0; i < document.forms.length; i++)
                  {{
                        var frm = document.forms[i];
                        var fld = frm['{1}'];
                        if (fld != null)
                        {{
                              fld.value = args;
                              break;
                        }}
                  }}
                  var __mustPostBack = true;
                  try
                  {{
                        if (OnClick != null)
                              __mustPostBack = OnClick('{1}', args);
                  }}
                  catch (ex)
                  {{
                  }}
                  if (__mustPostBack)
                        __doPostBack('{1}', args);
            }}
      }}
      if (navigator.appName && navigator.appName.indexOf('Microsoft') != -1 && 
            navigator.userAgent.indexOf('Windows') != -1 && navigator.userAgent.indexOf('Windows 3.1') == -1)
      {{
            document.write('<SCRIPT LANGUAGE=VBScript\> \n');
            document.write('on error resume next \n');
            document.write('Sub {0}_DoFSCommand(ByVal command, ByVal args)\n');
            document.write('  call {0}_DoFSCommand(command, args)\n');
            document.write('end sub\n');
            document.write('</SCRIPT\> \n');
      }}
      //-->
</SCRIPT>

This of course doesn't make the file very reusable elsewhere though.  Hard to have your cake and eat it too.

Thanks!
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17232114
PAQed with points refunded (500)

DarthMod
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question