Hello,
I'm trying to post data and return it back to my page using ajax and jquery. Don't seem to know what i'm doing wrong. Please help..
Below is what I have done.
Markup
<script type="text/javascript">
$(document).ready(function() {
$('#<%= PracticeButton.ClientID %>').click(BindTextbox);
});
function BindTextbox() {
$.ajax({
type: "POST",
url: '/dotnetnuke5/DesktopModules/GlobalPay.MainBoard/ViewMainBoard.ascx/GetDetails',
data: "{}",
contentType: "application/json",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#table1").append("<tr><td>" + data.d[i].FirstName + "</td><td>" + data.d[i].Age + "</td></tr>");
}
// $('#<%= PractiseTextbox.ClientID %>').val(data);
// data.d.GetDetails);
}
})
}
</script>
<div>
<asp:TextBox runat="server" ID="PractiseTextbox"></asp:TextBox>
<asp:Button runat="server" ID="PracticeButton" Text="Test" />
<table style="width: 100%;" id="table1">
</table>
</div
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
DotNetNuke.Framework.jQuery.RequestRegistration();
//register the accordion script
if (!DotNetNuke.UI.Utilities.ClientAPI.IsClientScriptBlockRegistered(Page, "ui.core.js"))
{
DotNetNuke.UI.Utilities.ClientAPI.RegisterClientScriptBlock(Page,
"ui.core.js", "<script src=\"" + Request.ApplicationPath +
"/jquery-1.4.1.min.js\" type=\"text/javascript\"></script>");
}
[WebMethod]
public static Names[] GetNames()
{
List<Names> list = new List<Names>();
DataTable dt = DataStore.GetDataTable();
foreach (DataRow row in dt.Rows)
{
Names _names = new Names();
_names.FirstName = row["Name"].ToString();
_names.Age = row["age"].ToString();
list.Add(_names);
}
return list.ToArray();
}
}
public class DataStore
{
public static DataTable GetDataTable()
{
DataTable dt = new DataTable("Names");
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Age");
dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
DataRow dr1 = dt.NewRow();
dr1[0] = "Ahmed";
dr1[1] = "27";
DataRow dr2 = dt.NewRow();
dr2[0] = "Peter";
dr2[1] = "30";
DataRow dr3 = dt.NewRow();
dr3[0] = "John";
dr3[1] = "20";
DataRow dr4 = dt.NewRow();
dr4[0] = "Ali";
dr4[1] = "30";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
dt.Rows.Add(dr4);
return dt;
}
}
public class Names
{
private string _firstName;
private string _age;
public string FirstName
{
set { _firstName = value; }
get { return _firstName; }
}
public string Age
{
set { _age = value; }
get { return _age; }
}
}
}