Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-07-04
5
Medium Priority
?
396 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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