當你試圖用JSON數據綁定radgrid控件這就是爲什麼你得到這個錯誤。
要解決此問題,您必須使用client side binding method綁定您的radgrid,或爲json數據綁定添加虛擬數據。
添加虛擬數據的JSON數據綁定:
ASPX
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<script src="Script/jquery-1.10.2.min.js"></script>
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
var GridData;
function OnbuttonClient() {
jQuery.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '',
dataType: 'JSON',
url: 'Forum.aspx/BindGrid',
success: function (result) {
GridData = result.d;
if (GridData.length > 0) {
var divGridContainer = document.getElementById('divGridContainer');
divGridContainer.style.display = "";
var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
tableView.set_dataSource(GridData);
tableView.dataBind();
}
else {
var divGridContainer = document.getElementById('divGridContainer');
divGridContainer.style.display = "none";
}
},
error: function() {
alert('Error on binding the data');
}
});
return false;
}
</script>
</telerik:RadCodeBlock>
</head>
<body>
<form id="form1" runat="server">
<div>
<telerik:RadScriptManager ID="RadScriptManager1" EnablePageMethods="true" runat="server">
</telerik:RadScriptManager>
<asp:Button ID="Button1" Text="Bind Grid" runat="server" OnClientClick="return OnbuttonClient();" />
<div id="divGridContainer" style="display: none;">
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false">
<MasterTableView HierarchyLoadMode="Client" DataKeyNames="ID,ParentID" ClientDataKeyNames="ID,ParentID">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</div>
</div>
</form>
</body>
</html>
ASPX.CS
protected void Page_Init(object source, System.EventArgs e)
{
}
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dynamic data = new[] {
new { ID = "1", Name ="Name11",ParentID = "0"}
};
RadGrid1.MasterTableView.DataSource = data;
RadGrid1.DataBind();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
[WebMethod]
public static List<Employee> BindGrid()
{
List<Employee> list = new List<Employee>(); ;
Employee obj = new Employee();
obj.ID = "1";
obj.Name = "Name1";
list.Add(obj);
obj = new Employee();
obj.ID = "2";
obj.Name = "Name2";
list.Add(obj);
return list;
}
[Serializable]
public class Employee
{
public string ID { get; set; }
public string Name { get; set; }
}
請檢查this鏈接瞭解更多細節。
我做了這種方式創建虛擬數據..在頁面加載時,我創建了虛擬數據綁定到網格。然後用戶點擊搜索按鈕,真正的代碼被執行。然而,我仍然得到相同的錯誤.. – 2014-10-31 11:00:30
你能提供你的代碼嗎? – 2014-10-31 19:39:08
您可以在您的應用程序中創建一個新頁面,並複製下面的所有代碼鏈接並進行檢查。如果它仍然不能正常工作,我會提供工作演示。 http://www.telerik.com/forums/get-radgrid-cell-value-on-button-click#QfNXbRpwLE-5GL6yMd70hQ – 2014-11-03 06:13:29