[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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

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
daveokst
Asked:
daveokst
  • 2
1 Solution
 
aprestoCommented:
Hi there,

apologies but i cant see where {0}_ is being set?
0
 
daveokstAuthor Commented:
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
 
daveokstAuthor Commented:
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
 
DarthModCommented:
PAQed with points refunded (500)

DarthMod
Community Support Moderator
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now