问题
I'm programming a Forum. My showcomment.aspx
page, I use Repeater to get comment from SQL Database and display them in each topic.
I use UpdatePanel to auto update the new comment inserted in Database.
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<div onclick="__doPostBack('UpdatePanel1', '');">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Repeater ID="RepeaterComment" runat="server">
....
</asp: Repeater...>
</ContentTemplate>
</asp: UpdatePanel>
</div>
The UpdatePanel1_Load():
public void UpdatePanel1_Load(Object sender, EventArgs e)
{
int idtopic = Convert.ToInt32(Request.QueryString["idtopic"]);
BindRepeaterComment(idtopic);
}
I've recommended to use Update Panel and Timer Control to auto update the UpdatePanel.
<asp:Timer ID="AutoRefreshTimer" runat="server"
Interval="10000"
ontick="AutoRefreshTimer_Tick"/>
But I just want it only refresh when a new row is inserted into Database.
The function insert a new row to Database is in another aspx page, not in the same page with Show comment page.
I've aslo recommended to use srcipt to Update the UpdatePanel:
<script type="text/javascript">
var UpdatePanel1 = '<%=UpdatePanel1.ClientID%>';
function ShowItems()
{
if (UpdatePanel1 != null)
{
__doPostBack(UpdatePanel1, '');
}
}
</script>
But I dont know how to call ShowItem() function.
Someone ask me, try to add line: UpdatePanel1.Update()
after I add a new row into Database, but the function "add new row" is in another page, so I cant.
How can it realize the database change and auto update the new comment (just update when database has a change) without a button.
Help? I have tried to find the solution for many days, because I'm a new to ASP.net so it's so hard for me.
回答1:
You can use HTML5
Server sent events to capture any change. For more details please check following link.
http://www.w3schools.com/html/html5_serversentevents.asp
You can also check here http://forums.asp.net/t/1885055.aspx
To get the notification from sql database server you can use SQL Dependency in your ASP.NET application.
If you are not using SQL server in that case you can make a call to the db from server and send an event back to the client.
来源:https://stackoverflow.com/questions/19023048/how-to-check-when-the-table-is-inserted-a-new-row-and-then-auto-update-repeater