【发布时间】:2015-06-29 11:04:05
【问题描述】:
我的 sql 数据库中有一个表。我正在使用 gridview 在网页中显示它。问题是什么都没有显示。我使用 AJAX 发布方法从数据库中获取数据,但网格仍然为空。我想使用 AJAX 发布方法在按钮单击时在 gridview 中显示数据。
我的 sql 数据库中有一个表。我正在使用 gridview 在网页中显示它。问题是没有显示任何内容。我使用 AJAX 发布方法从数据库中获取数据,但网格仍然为空。我想使用 AJAX 发布方法在按钮单击时在 gridview 中显示数据。 1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDummyRow();
}
}
private void BindDummyRow()
{
DataTable dummy = new DataTable();
dummy.Columns.Add("CarServiceId");
dummy.Columns.Add("CarServiceName");
dummy.Columns.Add("CityId");
dummy.Columns.Add("Address");
dummy.Columns.Add("Contact1");
dummy.Columns.Add("Contact2");
dummy.Columns.Add("CarType");
dummy.Columns.Add("NoOfCar");
dummy.Columns.Add("MinimumPrice");
dummy.Columns.Add("MaximumPrice");
dummy.Rows.Add();
carServiceGridView.DataSource = dummy;
carServiceGridView.DataBind();
}
[WebMethod]
public static string GetCarService()
{
DataTable carDT = CarService.GetCarService(); //get data from Car Service table ( select * from CarService)
DataSet ds = new DataSet();
ds.Tables.Add(carDT);
return ds.GetXml();
}
Javascript:
function GridDisplay(){
GetCarService();
}
function GetCustomers() {
// debugger;
$.ajax({
type: "POST",
url: "Default.aspx/GetCarService",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
// debugger;
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("CarService");
var row = $("[id*=carServiceGridView] tr:last-child").clone(true);
$("[id*=carServiceGridView] tr").not($("[id*=carServiceGridView] tr:first-child")).remove();
$.each(customers, function () {
// debugger;
var customer = $(this);
$("td", row).eq(0).html($(this).find("CarServiceID").text());
$("td", row).eq(1).html($(this).find("CarServiceName").text());
$("td", row).eq(0).html($(this).find("Address").text());
$("td", row).eq(1).html($(this).find("Contact1").text());
$("td", row).eq(2).html($(this).find("Contact2").text());
$("td", row).eq(0).html($(this).find("MinimumPrice").text());
$("td", row).eq(1).html($(this).find("MaximumPrice").text());
$("td", row).eq(2).html($(this).find("NoOfCar").text());
$("td", row).eq(2).html($(this).find("CarType").text());
$("[id*=carServiceGridView]").append(row);
row = $("[id*=carServiceGridView] tr:last-child").clone(true);
});
};
.aspx:
<asp:Button runat="server" ID="modalTransportSearchButton" Text="Search" OnClientClick="GridDisplay();" Width="100px" />
<asp:GridView ID="carServiceGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="CarServiceId" Width="100%" Height="100%" AlternatingRowStyle-BackColor="WhiteSmoke">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:RadioButton ID="carServiceGridViewRadioButton" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CarServiceId" HeaderText="Id " />
<asp:BoundField DataField="CarServiceName" HeaderText="Service " />
<asp:BoundField DataField="Address" HeaderText="Address " />
<asp:BoundField DataField="Contact1" HeaderText="Contact 1 " />
<asp:BoundField DataField="Contact2" HeaderText="Contact 2 " />
<asp:BoundField DataField="MinimumPrice" HeaderText="Minimum Price " />
<asp:BoundField DataField="MaximumPrice" HeaderText="Maximum Price " />
<asp:BoundField DataField="NoOfCar" HeaderText="No Of Car " />
<asp:BoundField DataField="CarType" HeaderText="Car Types " />
</Columns>
</asp:GridView>
【问题讨论】:
-
什么是警报?
-
在成功或错误部分?
-
试试
error: function (xhr, ajaxOptions, thrownError) {alert(xhr.statusText);}看看是什么错误。也可以看xhr.responseText -
你在
xhr.responseText得到什么? -
现在没有错误...只有空白的gridview
标签: javascript c# asp.net ajax