正如您所说“由于它与下拉菜单相同的数据源,我得到一个
下拉列表中较小的不同列表。”
您可以将Database Information 保留在ViewState 中。这样,您可以阻止 Request to Database 为您的客户端请求。因此Reducing the Access time。
示例
public DataTable Employees
{
get
{
if (ViewState["Employees"] == null)
{
return FollowsDAL.GetAllEmployees();
}
return (DataTable)ViewState["Employees"];
}
set
{
ViewState["Employees"] = value;
}
}
如何使ViewState 数据过滤更快?
答案是 - 请不要使用Update Panel。我将使用Page Method。
请查看以下示例。我用Update Panel 和Script Manager。
输出
要显示 22 个字符的字符串,您可以检查正在接收和发送到服务器的数据量。想象一下
- 如果您考虑使用更新面板将每个请求发送到数据库并且您的 GridView 位于
Update Panel!!!!!!
- 如果您要为每个请求使用 ViewState 数据,并在
GridView 内使用 Update Panel。
根据我的理解,上述两种技术都是最差的。
现在我将向您描述页面方法
更新面板上的页面方法
Page methods 允许ASP.NET AJAX 页面直接执行Page’s Static Methods,使用JSON (JavaScript Object Notation)。我们可以使用web method 代替posting back and then receiving HTML markup 到completely replace our UpdatePanel’s contents,只请求我们感兴趣的信息。
示例代码
输出
所以结论是我肯定会使用ViewState BUT 和Page Methods。
ViewState 数据的过滤技术。
public static class GetFilteredData
{
public static DataTable FilterDataTable(this DataTable Dt,
string FilterExpression)
{
using (DataView Dv = new DataView(Dt))
{
Dv.RowFilter = FilterExpression;
return Dv.ToTable();
}
}
}
示例
DataTableObject.FilterDataTable("Search Expression")
您好 vpiTriumph... 我发现代码有所改进。以下是建议的方法。
C Sharp 中的示例代码
private void DsataBaseInteraction()
{
using (SqlConnection con = new SqlConnection("Your Connection String"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "Your Stored Procedure name";
using (SqlDataReader DR = cmd.ExecuteReader())
{
}
}
}
}
@KevinDeus - 我假设正在使用的Database 是SQL Server。所以下面提到的是我在Database中对Stored Procedure的建议。
Create Proc ProcedureName
@UserName Varchar(50),
@Password Varchar(50),
@Email Varchar(50)
As
SET NOCOUNT ON
SET XACT_ABORT ON
Begin Try
Begin Tran
Insert into Account (Username,Password, Email)
Values(@UserName, @Password, @Email)
Commit Tran
End Try
Begin Catch
Rollback Tran
End Catch