C# ASP.NET如何使OnRowDataBound事件异步?
我目前有一个嵌套的datagrid。顶级网格从存储过程获取数据。嵌套网格根据每行的列值从存储过程获取数据 这是我的ASPX代码C# ASP.NET如何使OnRowDataBound事件异步?,c#,asp.net,webforms,C#,Asp.net,Webforms,我目前有一个嵌套的datagrid。顶级网格从存储过程获取数据。嵌套网格根据每行的列值从存储过程获取数据 这是我的ASPX代码 <%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CS" Debug="true" %> &l
<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CS" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
* {
margin:0;
padding: 0;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="ID" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" width="25px" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="135px" DataField="Label" HeaderText="Label" />
<asp:BoundField ItemStyle-Width="150px" DataField="IDs" HeaderText="IDs" />
<asp:BoundField ItemStyle-Width="150px" DataFormatString="${0:###,###,###.00}" DataField="Total" HeaderText=Total" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="Source" HeaderText="Source" />
<asp:BoundField ItemStyle-Width="150px" DataField="Label" HeaderText="Label" />
<asp:BoundField ItemStyle-Width="150px" DataFormatString="${0:###,###,###.00}" DataField="Total20" HeaderText="Total20" />
<asp:BoundField ItemStyle-Width="150px" DataFormatString="${0:###,###,###.00}" DataField="Total1" HeaderText="Total1" />
<asp:BoundField ItemStyle-Width="150px" DataFormatString="{0:P1}" DataField="Average_Fee_PCT" HeaderText="Average Fee" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
* {
保证金:0;
填充:0;
}
$(“[src*=plus]”。实时(“单击”,函数(){
(“+$(this).next().html()+”)之后
$(this.attr(“src”,“images/minus.png”);
});
$(“[src*=减号]”).live(“单击”,函数(){
$(this.attr(“src”、“images/plus.png”);
$(this).closest(“tr”).next().remove();
});
不,您不能使数据绑定异步,这不是webforms的工作方式。您要么需要对异步webmethod使用某种ajax调用,要么重新考虑您的设计,这样就不需要这些嵌套调用(PEHAP仅在行上显示它们,并使用单独的ajax调用展开)。我没有仔细查看,但嵌套网格一直在展开吗?如果可以添加扩展类型UI,则只能在选中/扩展网格行时提取子数据。这也意味着您不需要加载不必要的数据。
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.Data.SqlClient;
using System.Configuration;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<script>console.log('Page Loaded');</script>");
if (!IsPostBack)
{
gvCustomers.DataSource = GetData("exec ReturnData '" + DateTime.Now.ToString("yyyy-MM-dd") + "'");
gvCustomers.DataBind();
}
}
private static DataTable GetData(string query)
{
using (SqlConnection con = new SqlConnection("Data Source=SQLSERVER;Initial Catalog=DBNAME;User Id=user;Password=pass"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
Response.Write("<script>console.log('Next Column...." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "');</script>");
if (e.Row.RowType == DataControlRowType.DataRow)
{
Response.Write("<script>console.log('Pulling data...." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "');</script>");
string ID = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("exec ReturnDataSingle '" + ID + "', '"+ DateTime.Now.ToString("yyyy-MM-dd") + "'"));
gvOrders.DataBind();
Response.Write("<script>console.log('Pulled data...." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "');</script>");
}
}
}