Solved

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

Posted on 2006-07-04
5
391 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 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