所有这一切对我来说都是新鲜的,所以如果这些都是愚蠢的问题我会道歉。
1 - 我的GridView从中拉出的表不包含空行,那么为什么我的GridView中的每一行都是空白的?请参阅下面的HTML和C#代码:
2 - 我似乎无法正确格式化日期。通常,我只是插入DataFormatString:" {0:MM / dd / yyyy}"进入GridView的每个日期行。那不行。日期以" YYYY-DD-MMT00:00:00-06:00"格式。我意识到这与JSON有关,但是如何让日期返回到" MM / DD / YYYY"格式?
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script src="ASPSnippets_Pager.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
GetC(1);
});
$("[id*=txtC]").live("keyup", function () {
GetC(parseInt(1));
});
$(".Pager .page").live("click", function () {
GetC(parseInt($(this).attr('page')));
});
function SearchTerm() {
return jQuery.trim($("[id*=txtC]").val());
};
function GetC(pageIndex) {
$.ajax({
type: "POST",
url: "Default.aspx/GetC",
data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
var row;
function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var c = xml.find("C");
if (row == null) {
row = $("[id*=GridView1] tr:last-child").clone(true);
}
$("[id*=GridView1] tr").not($("[id*=GridView1] tr:first-child")).remove();
if (c.length > 0) {
$.each(company, function () {
var company = $(this);
$("td", row).eq(1).html($(this).find("A").text());
$("td", row).eq(2).html($(this).find("B").text());
$("td", row).eq(3).html($(this).find("C").text());
$("td", row).eq(4).html($(this).find("D").text());
$("td", row).eq(5).html($(this).find("E").text());
$("td", row).eq(6).html($(this).find("F").text());
$("td", row).eq(7).html($(this).find("G").text());
$("td", row).eq(8).html($(this).find("H").text());
$("td", row).eq(9).html($(this).find("I").text());
$("td", row).eq(10).html($(this).find("DateApproved").text());
$("td", row).eq(11).html($(this).find("J").text());
$("td", row).eq(12).html($(this).find("K").text());
$("td", row).eq(13).html($(this).find("L").text());
$("td", row).eq(14).html($(this).find("M").text());
$("[id*=GridView1]").append(row);
row = $("[id*=GridView1] tr:last-child").clone(true);
});
var pager = xml.find("Pager");
$(".Pager").ASPSnippets_Pager({
ActiveCssClass: "current",
PagerCssClass: "pager",
PageIndex: parseInt(pager.find("PageIndex").text()),
PageSize: parseInt(pager.find("PageSize").text()),
RecordCount: parseInt(pager.find("RecordCount").text())
});
$(".Company").each(function () {
var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
$(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchTerm() + "</span>"));
});
} else {
var empty_row = row.clone(true);
$("td:first-child", empty_row).attr("colspan", $("td", row).length);
$("td:first-child", empty_row).attr("align", "center");
$("td:first-child", empty_row).html("No records found for the search criteria.");
$("td", empty_row).not($("td:first-child", empty_row)).remove();
$("[id*=GridView1]").append(empty_row);
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<table style="font-family: 'Franklin Gothic Book'">
<tr>
<td colspan="2" style="background-color: #273B69; text-align: center" class="auto-style1">
<h1 class="auto-style2"><span class="auto-style3"></span>
</h1>
</td>
</tr>
<tr>
<td style="background-color: #D8D8D8;" class="auto-style8">
<h3>
</h3>
</td>
<td style="vertical-align: top" class="auto-style7">
<h5 class="auto-style6">
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
Font-Names="Franklin Gothic Book"
Font-Size="Small"
CellPadding="4"
ForeColor="#333333"
GridLines="Both"
BorderWidth="1px" AutoGenerateSelectButton="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="A" HeaderText="A" />
<asp:BoundField DataField="B" HeaderText="B" />
<asp:BoundField DataField="C" HeaderText="C" />
<asp:BoundField DataField="D" HeaderText="D" />
<asp:BoundField DataField="E" HeaderText="E" />
<asp:BoundField DataField="F" HeaderText="F" />
<asp:BoundField DataField="G" HeaderText="G" />
<asp:BoundField DataField="H" HeaderText="H" />
<asp:BoundField DataField="I" HeaderText="I" />
<asp:BoundField DataField="J" HeaderText="J" />
<asp:BoundField DataField="K" HeaderText="K" />
<asp:BoundField DataField="L" HeaderText="L" />
<asp:BoundField DataField="M" HeaderText="M" />
<asp:BoundField DataField="N" HeaderText="N" Visible="false" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private static int PageSize = 10;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDummyRow();
}
}
private void BindDummyRow()
{
DataTable dummy = new DataTable();
dummy.Columns.Add("A");
dummy.Columns.Add("B");
dummy.Columns.Add("C");
dummy.Columns.Add("D");
dummy.Columns.Add("E");
dummy.Columns.Add("F");
dummy.Columns.Add("G");
dummy.Columns.Add("H");
dummy.Columns.Add("I");
dummy.Columns.Add("J");
dummy.Columns.Add("K");
dummy.Columns.Add("L");
dummy.Columns.Add("M");
dummy.Columns.Add("N");
dummy.Rows.Add();
GridView1.DataSource = dummy;
GridView1.DataBind();
}
[WebMethod]
public static string GetC(string searchTerm, int pageIndex)
{
string query = "[GetC_Pager]";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
return GetData(cmd, pageIndex).GetXml();
}
private static DataSet GetData(SqlCommand cmd, int pageIndex)
{
string strConnString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "C");
DataTable dt = new DataTable("Pager");
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageSize");
dt.Columns.Add("RecordCount");
dt.Rows.Add();
dt.Rows[0]["PageIndex"] = pageIndex;
dt.Rows[0]["PageSize"] = PageSize;
dt.Rows[0]["RecordCount"] = cmd.Parameters["@RecordCount"].Value;
ds.Tables.Add(dt);
return ds;
}
}
}
}
}