Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now