0

I have "custom control" project that constructs a string of javascript to plug into another application. The application calls the "custom control" to determine certain behaviors.

The problem I'm having involves the need for multiple levels of quotes within my string containing javascript. In the version below, I've tried escaping the inner quotes, but IE throws an error (Could not complete the operation due to error 80020101). I have solved similar problems in the past by putting javascript in a separate js file and calling it from the plugin code. In this case, however, I cannot get the application to "see" the js file. How can I put the javascript into a string that I can pass?

The relevant code is:

class Class1 : Literal
{
    protected override void CreateChildControls()
    {
        var controlHtml = new StringBuilder();

        controlHtml.Append("<script type='text/javascript'>");
        controlHtml.Append("$('#prodcon_error_msg').css('display', 'none');" +
                           "$('#prodcon_client_id_filterimg').click(function() {" +
                           "ShowFilterDialog('Client ID', 'clientId');" +
                           "});");
        controlHtml.Append("function ShowFilterDialog(criteriaName, filterTitle) {" +
                           "alert('show dialog code here for ' + criteriaName);" +
                           "var filterDialogDiv = document.createElement('DIV');");

        controlHtml.Append("filterDialogDiv.innerHTML = \"<div id='customaccountcriteriafilterdialog' class='dialogback'><div class='dialogheadingarea'>" +
                            "<span class='dialogtitle' style='float: left;'>&nbsp;&nbsp;\" + filterTitle + \" Lookup</span>" +
                            "<img class='dialogcloseimage' onmouseup=\"removeElement('customaccountcriteriafilterdialog');\" src='../../images/boxy.gif'/>" +
                            "</div><br/>Key word: <input type='text' id='criteriaFilterKeyword' /></div>");

        controlHtml.Append("filterDialogDiv.style.position = 'absolute';" +
                           "filterDialogDiv.style.left = '300px';" +
                           "filterDialogDiv.style.top = '85px';" +
                           "filterDialogDiv.style.Zindex = 4; " +
                           "document.body.appendChild(filterDialogDiv);" +
                           "}");

        this.Text = controlHtml.ToString();
    }
}

Note: The ending script tag is added by the calling application before appending.

4

1 回答 1

0

我的建议是将整个代码放在一个 txt 文件中,并且这个 txt 文件应该被标记为嵌入式资源。如果文本的某些部分应该根据变量值进行格式化,解决方案是使用 string.Format 来这样做。

要加载 txt 文件,只需使用

try
   {
      var _assembly = Assembly.GetExecutingAssembly();
      var _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
var text = _textStreamReader.ReadToEnd();

// use the variable text for something...
   }
   catch
   {
      MessageBox.Show("Error accessing resources!");
   }

我希望它有所帮助。此致。

于 2013-05-17T20:44:46.613 回答