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:
[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:
function LoadControls() {
try {
var types = $find("<%= ddlPost.ClientID %>");
var Category = $find("<%= ddlCategory.ClientID %>");
var typeID = types.get_value();
var catID = Category.get_value();
$('#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);
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.