Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
hi,

I am working on a task where I am creating some input control dynamically and i am creating a js file using streamwriter in c# code to validate these input control. Now I want to add ref of the currently created js file in my aspx page.

my crate input control method is a webmethod and I call it using ajax-json from client side.
web method:

XML
[WebMethod]
    public static string LoadDatanGenerateControls(int PostTypeID, int CategoryID)
    {
        DataTable dtControls = new DataTable();
        StringBuilder sb = new StringBuilder();
        dtControls = PostingExtendedFields.GetExtendedDFields(PostTypeID, CategoryID);
        sb.Append("<table>");
        foreach (DataRow dr in dtControls.Rows)
        {
            sb.Append("<tr><td>");
            sb.Append("<input value='" + dr["RequiredError"].ToString() + "' type='text' size='20' id='txt_" + dr["FieldName"].ToString() + "' class='riTextBox riEmpty' style='width:450px;' onfocus ='Focus(this);' onblur=\"Blur(this,'" + dr["RequiredError"].ToString() + "');\"/>");
            sb.Append("</td></tr>");
        }
        sb.Append("</table>");
        //create the js file
        string path = HttpContext.Current.Server.MapPath("") + @"\";
        if (File.Exists(path + "sale.js")) File.Delete(path + "sale.js");//delete if exist
        using (StreamWriter sw = new StreamWriter(path + "sale.js"))
        {
            sw.WriteLine("function Focus(obj) {");
            sw.WriteLine("var id = obj.id;");
            sw.WriteLine("$('#' + id).val('');");
            sw.WriteLine(" }");
            sw.WriteLine("function Blur(obj, title) {");
            sw.WriteLine("var id = obj.id;");
            sw.WriteLine("if ($('#' + id).val() == '') {");
            sw.WriteLine("$('#' + id).val(title);");
            sw.WriteLine("}");
            sw.WriteLine(" }");
        }
        return sb.ToString();
    }


on aspx page:

C#
function LoadControls() {
            try {
                var types = $find("<%= ddlPost.ClientID %>");
                var Category = $find("<%= ddlCategory.ClientID %>");
                var typeID = types.get_value();
                var catID = Category.get_value();
                ///web method
                $('#TDDynamicContents').html('');
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/LoadDatanGenerateControls",
                    data: "{'PostTypeID':'" + typeID + "','CategoryID':'" + catID + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        if (msg.d.length > 0) {
                            $('#TDDynamicContents').html(msg.d);
                            //add js file path
                            addjsfile("sale.js", "js")
                        }
                    },
                    async: false,
                    error: function(xhr, status, error) {
                        alert(xhr.statusText);
                    }
                });
            } catch (e) { }
        }

        function addjsfile(filename, filetype) {
            alert(filetype);
            if (filetype == "js") {
                var fileref = document.createElement('script');
                fileref.setAttribute("type", "text/javascript");
                fileref.setAttribute("src", filename);
            }
            if (typeof fileref != "undefined") {
                document.getElementsByTagName("head")[0].appendChild(fileref);
                alert(fileref);
            }
        }



but my addjsfile function is not working at all.
its not adding the refrence in the Head section and I am unable to use functions in the js file.

any help would be greatly appreciated.
Posted
Updated 11-May-11 19:55pm
v2

document.write("");
 
Share this answer
 
Why are you creating the .js file if each time you are creating it...better append the JS code inside the HTML generation code block of the webmethod and return it to the client...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900