开发者

add js file ref from javascript function

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 the input control. Now I want to add reference to the currently created js file in my aspx page.

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

[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 开发者_如何学GoBlur(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 the aspx page:

 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.


the solution to your problem is to use the following line of code in the event wherever you are using the js functionality or simply on the page load ClientScript.RegisterClientScriptInclude("MyScript","MyJavaScript.js") more detailed discussion goes here


if you only intend to use javascript block to do this then have a look at it if it can be of any help

Click Here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜