Reputation: 119
I have populated Grid View Dynamically with json Data ..Button Column started to appears only in First Row ..But not in below rows ..
I have Tried Code to add column in server side code as well as in Mark Up ..I also search but could not able to find any thing relevant
this is my MarkUp :
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "List.aspx/GetData",
data: "{}",
dataType: "json",
success: function (data) {
for (var i = 0; i < data.d.length; i++) {
$("#gvDetails").append("<tr><td>" + data.d[i].c1 + "</td><td>" + data.d[i].c2 + "</td><td>" + "</td></tr>");
}
},
error: function (result) {
alert(result.status + ' ' + result.statusText);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server" ShowHeaderWhenEmpty="True">
<Columns>
<asp:ButtonField Text="Button" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
this is code behind :
protected void Page_Load(object sender, EventArgs e)
{
BindColumnToGridview();
}
private void BindColumnToGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("c1");
dt.Columns.Add("c2");
dt.Rows.Add();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static tbl1[] GetData()
{
try
{
using (var context = new TestDBContext())
{
List<tbl1> lsTbl1 = new List<tbl1>();
lsTbl1 = (from c in context.tbl1 select c).ToList();
return lsTbl1.ToArray();
}
}
catch (Exception)
{
throw;
}
}
I also tried to add column from code behind
gvDetails.Columns.Add(new ButtonField() { Text = "Button" });
this is not working too
any sugestion will be helpful
Upvotes: 1
Views: 2229
Reputation: 9612
Since gvDetails
is a server side control you should use <%= gvDetails.ClientID %>
in your JS snippet.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "List.aspx/GetData",
data: "{}",
dataType: "json",
success: function (data) {
for (var i = 0; i < data.d.length; i++) {
$("#<%= gvDetails.ClientID %>").append("<tr><td>" + data.d[i].c1 + "</td><td>" + data.d[i].c2 + "</td><td>" + "</td></tr>");
}
},
error: function (result) {
alert(result.status + ' ' + result.statusText);
}
});
});
</script>
EDIT:
Upvotes: 1