Bibek Aryal Bibek Aryal - 3 months ago 7
Javascript Question

How to call a Javascript function when I click on a <li> on codebehind

I am using code behind(something.ascx.cs) to generate an un-ordered list from my database using the following code.

foreach (DataRow row in dt.Rows)
{
htmlRender.Append("<ul>");
for (int i = 1; i < dt.Columns.Count; i++)
{
var courseCategory = row[i-1];
htmlRender.Append("<li id='"+ courseCategory + "' onclick='getCourseCategory(this.id)' >");
htmlRender.Append(row[i]);
htmlRender.Append("</li>");
}
htmlRender.Append("</ul>");
}


But when I click on the list it gives an error "getCourseCategory" is not defined.So my problem is how do I access the "getCourseCategory" function whose definition is in something.ascx file.

<script type="text/javascript">
$(function () {
function getCourseCategory(courseCategoryID) {
alert('hello');
};
});



Answer

You have written function inside document.ready it should be outside of that.

<script type="text/javascript">
   function getCourseCategory(courseCategoryID) {
    alert('hello');
}
</script>