【发布时间】:2012-02-07 14:24:40
【问题描述】:
我有一个 sqldatareader 在页面加载时在页面上吐出 SQL 数据,但我想添加功能以单击 jquery 按钮从一组新结果中向用户刷新数据,而不刷新整个页面.
虽然我是一个初学者,但不知道从哪里开始。
我的 ASPX 中有以下 DIV 内容
<div id="blockOver">
<% while (Reader.Read()) {
string filename = Reader.GetString(1);
string date = Reader.GetSqlDateTime(3).ToString();
string filetype = Reader.GetString(4);
Int32 height = (Int32)Reader.GetSqlInt32(5);
Int32 width = (Int32)Reader.GetSqlInt32(6);
string uploadGroup = Reader.GetString(7);
string title = Reader.GetString(8);
string uniqueID = Reader.GetString(9);
string uploader = Reader.GetString(10);
string uniqueIDnoExt = Reader.GetString(12);
%>
<div class="block">
<a href="#t=<%= uniqueID %>" onmouseover="defaultJs.displayInfo ('<%= title %>', '<%= date %>', '<%= filetype %>', '<%= uniqueIDnoExt %>')" onclick="defaultJs.showFile('<%= title %>', '<%= date %>', '<%= filetype %>', '<%= uniqueIDnoExt %>', '<%= uniqueID %>')" onmouseout="defaultJs.hideInfo()">
<img title="<%= title %>" src="thumbs/<%= uniqueIDnoExt %>.jpg" />
</a>
</div>
<% } %>
在我的 ASPX.CS 中,我有:
public partial class _Default : System.Web.UI.Page
{
private SqlDataReader reader = null;
public SqlDataReader Reader { get { return reader; } set { reader = value; } }
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM uploads ORDER BY id DESC", connection);
command.Parameters.Add(new SqlParameter("uploader", "anonymous"));
Reader = command.ExecuteReader();
}
}
这适用于在页面加载时加载数据,但我现在想做的是合并 jquery/ajax 以便能够在 jquery 调用时刷新此数据,而无需刷新整个页面。
我不确定从哪里开始,我确定这是你们的第二天性,谁能指出一些简单的资源或提供一些示例代码?非常感谢。
【问题讨论】:
-
首先,您不应该混合代码和内容 (HTML),因为这会使您的页面更难阅读和维护。此外,您使用的嵌入式 SQL 也不好 - 使用带有 CommandType.StoredProcedure 类型命令对象的存储过程要好得多。
-
来自一位程序员的提示:不要使用数字索引来检索阅读器值;使用命名检索。
标签: c# jquery asp.net ajax visual-studio