Click here to Skip to main content
15,875,017 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hey all,

My design page code is

JavaScript
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ClientSales.aspx/BindDatatoDropdown",
data: "{}",
dataType: "json",
success: function (data) {
$.each(data.d, function (key, value) {
$('#%=ddlCategory.ClientID%>').append($("<option><;/option>").val(value.cat_id).html(value.cat_name));
});
},
error: function (data) {
}
});
});


This above function gets all the categories from database. I want to keep this function in different javascript file. I will be calling that function from all aspx pages. So, I don't know how to get data from jquery function and bind it in aspx dropdownlist
Posted
Updated 1-Sep-14 4:51am
v2

I think you required to create a java script function for getting data and call it on master page body tag. like

PHP
function myFunction(){
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ClientSales.aspx/BindDatatoDropdown",
data: "{}",
dataType: "json",
success: function (data) {
$.each(data.d, function (key, value) {
$('#%=ddlCategory.ClientID%>').append($("<option><;/option>").val(value.cat_id).html(value.cat_name));
});
},
error: function (data) {
}
});
});

}


add referance of js file to aspx page. and call it from onload evant of body tag.
i think u use master page so call it from master page body tag
 
Share this answer
 
First, make another js file.

ex)common.js

JavaScript
var util = util || {};

util.ajax = function (url, data, done, fail) {
    $.ajax({
        url: url,
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: data,
        cache: false
    }).done(done).fail(fail);
}


In all of your html files, you can use this function like this.

JavaScript
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    util.ajax('ClientSales.aspx/BindDatatoDropdown', {}, function (data) {
        $.each(data.d, function (key, value) {
        $('#%=ddlCategory.ClientID%>').append($("<option><;/option>").val(value.cat_id).html(value.cat_name));
        });
        },
    function (data) {

    });
});

</script>
 
Share this answer
 
Comments
RAHUL(10217975) 2-Sep-14 5:44am    
Thanks, this works but I don't want to expose my page name and webmethod name in page view source like ClientSales.aspx/BindDatatoDropdown. It is clearly visible in page source. How Can I hide that?

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