GridView的数据显示一般是固定的,假如要像Ajax那样实现GridView数据动态更新而不刷新页面,就有些难度了.客户提出的需求,没办法,也得去试着想办法实现.
涉及到Asp.Net事件机制的实现"_doPostback".Asp.Net的底层封装了XMLHttpRequest异步请求对象.在WebResource.axd中可以查看到这些封装的JavaScript.对于页面的异步请求,我们需要实现ICallbackEventHandler接口,并显示实现其中的GetCallbackResult()和RaiseCallbackEvent()这两个方法.废话不多少,假如了解Asp.Net机制的话一看就明白了.代码就贴在下面:
public
partial
class
upload_MixInfo : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GetAllUser();
}
string cbReference = Page.ClientScript.GetCallbackEventReference(Page, "", "Refresh", "");
btnGet.Attributes.Add("onclick",cbReference);
string script="";
script+="<script>\r\n";
script+="function Refresh(DataFromServer){\r\n";
script+="document.getElementById('Result').innerHTML=DataFromServer;\r\n";
script+="}\r\n</script>\r\n";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Refresh", script);
}
public string GetCallbackResult()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RenderControl(Test);
}
public void RaiseCallbackEvent(string eventArgument)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GetAllUser();
}
private string RenderControl(Control c)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringWriter writer1 = new StringWriter(CultureInfo.InvariantCulture);
HtmlTextWriter writer = new HtmlTextWriter(writer1);
c.RenderControl(writer);
writer.Flush();
writer.Close();
return writer1.ToString();
}
protected void GetAllUser()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sql = @"(Transform Sum(Nums)
select [Users].[UserName] as 地区负责人 from (([Users] left join [User_Type] on [Users].UserId=[User_Type].UserId) left join [Type] on [User_Type].TypeId=[Type].TypeId) GROUP BY [Users].UserName
PIVOT [Type].TypeId)";
OleDbConnection con = DBConn.NewConnection();
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataTable dt=new DataTable();
adapter.Fill(dt);
dt.Columns.Remove(dt.Columns[1]);
sql = "select * from [Type]";
cmd.CommandText = sql;
DataTable t = new DataTable();
adapter=new OleDbDataAdapter(cmd);
adapter.Fill(t);
DBConn.ResourceDispose(cmd);
for (int i = 0; i < t.Rows.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int j = 0; j < dt.Columns.Count; j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (t.Rows[i]["TypeId"].ToString() == ((DataColumn)dt.Columns[j]).ColumnName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dt.Columns[j].ColumnName = t.Rows[i]["TypeName"].ToString();
}
}
}
Test.DataSource = dt;
Test.DataBind();
}
}
<
body
>
<
form
id
="form1"
runat
="server"
>
<
input
type
="button"
runat
="server"
id
="btnGet"
value
="Get"
style
="display:none"
/>
<
div
id
="Result"
>
<
asp:GridView
ID
="Test"
runat
="server"
CellPadding
="4"
ForeColor
="#333333"
>
<
FooterStyle
BackColor
="#507CD1"
Font-Bold
="True"
ForeColor
="White"
/>
<
RowStyle
BackColor
="#EFF3FB"
/>
<
PagerStyle
BackColor
="#2461BF"
ForeColor
="White"
HorizontalAlign
="Center"
/>
<
SelectedRowStyle
BackColor
="#D1DDF1"
Font-Bold
="True"
ForeColor
="#333333"
/>
<
HeaderStyle
BackColor
="#507CD1"
Font-Bold
="True"
ForeColor
="White"
/>
<
EditRowStyle
BackColor
="#2461BF"
/>
<
AlternatingRowStyle
BackColor
="White"
/>
</
asp:GridView
>
</
div
>
</
form
>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
script
type
="text/javascript"
>
function btnclick()
{document.getElementById("btnGet").click();}setInterval("btnclick()",10000);
</
script
>
</
body
>