Force UpdatePanel with datalist to refresh on database changed - SqlDependency
-
26-12-2019 - |
Question
I'm using SQLDependency to catch changes in my database. http://code.msdn.microsoft.com/How-to-use-SqlDependency-5c0da0b3
This code really works. When I'm changing something in table, event is fired.
private void RefreshWithSqlDependency()
{
iquery = from order in context.Order
where order.Client.Nickname==nickName && order.StatusId!=5
select new { Id = order.Id, Description = order.Description, OrderStatus = order.Status.Name };
notification = new ImmediateNotificationRegister<Order>(context, iquery);
notification.OnChanged += NotificationOnChanged;
}
protected void NotificationOnChanged(object sender, EventArgs e)
{
BindOrderDataList(); //this is executed
}
In BindOrderDataList
I'm setting datasource of my datalist.
private void BindOrderDataList()
{
DataListOrders.DataSource = context.Order.Where(x => x.Client.Nickname == nickName && x.StatusId != 5)
.Select(x => new { Id = x.Id, Description = x.Description, OrderStatus = x.Status.Name }).ToList();
DataListOrders.DataBind();
}
Of course nothing happened. Then I put Datalist
inside UpdatePanel
.
<asp:UpdatePanel ID="UpdatePanelOrdersList" runat="server" UpdateMode="Conditional" OnLoad="UpdatePanelOrdersList_Load">
<ContentTemplate>
<asp:DataList ID="DataListOrders" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" OnItemDataBound="DataListOrders_ItemDataBound"
CellPadding="5" Width="100%" OnItemCommand="DataListOrders_ItemCommand" EnableViewState="False">
<ItemStyle Wrap="True" HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<asp:Panel ID="Order" runat="server">
<div style="padding: 3px; border: 3px solid; border-color: #F0F0F0">
<h4>Order
<asp:Label ID="OrderId" runat="server" Text='<%# Eval("Id") %>'></asp:Label></h4>
<h5>Desc:
<asp:Label ID="Description" runat="server" Text='<%# Eval("Description") %>'></asp:Label></h5>
<h5>Status:
<asp:Label ID="OrderStatus" runat="server" Text='<%# Eval("OrderStatus") %>'></asp:Label></h5>
<br />
<asp:DataList ID="DataListOrderProducts" runat="server" RepeatDirection="Vertical" RepeatColumns="1"
OnItemDataBound="DataListOrderProducts_ItemDataBound" OnItemCommand="DataListOrderProducts_ItemCommand"
EnableViewState="False">
<ItemStyle Wrap="True"></ItemStyle>
<ItemTemplate>
<asp:Panel ID="OrderItem" runat="server">
<h6>Product:
<asp:Label ID="OrderProductId" runat="server" Text='<%# Eval("LineId") %>' CssClass="hiddencol"></asp:Label></h6>
<h6>Product:
<asp:Label ID="Product" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label></h6>
<h6>Status:
<asp:Label ID="ProductStatus" runat="server" Text='<%# Eval("ProductStatus") %>'></asp:Label></h6>
<asp:LinkButton ID="ItemSubmit" runat="server" CommandName="ItemCompleted" Text="Complete" Visible="False" />
</asp:Panel>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="OrderSubmit" runat="server" CommandName="OrderCompleted" Text="Complete" Visible="false" />
</div>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="NotificationOnChanged" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DataListOrders" EventName="DataBinding" />
<asp:AsyncPostBackTrigger ControlID="DataListOrders" />
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
When I'm clicking this Button1
same method as in RefreshWithSqlDependency
is executed (NotificationOnChanged
) and this is working...
Is there any event that would force UpdatePanel
to update when content in DataList
changes?
I already tried delegate to raise update event in UpdatePanel
...
protected void NotificationOnChanged(object sender, EventArgs e)
{
BindOrderDataList();
LongTimeTask_Delegate d = new LongTimeTask_Delegate(LongTimeTask);
IAsyncResult r = d.BeginInvoke("String", new AsyncCallback(TaskCompleted), null);
d.EndInvoke(r);
}
public delegate void LongTimeTask_Delegate(string str);
public void LongTimeTask(string str)
{
Thread.Sleep(50);
}
public void TaskCompleted(IAsyncResult r)
{
UpdatePanelOrdersList.Update();
}
but no success...
Solution
SQLDependency's events can't update updatepanel's content. You have to trigger update event with other event triggers like timer. if timer ticks then updatepanel updates contents. put your update datalist code into timer event. I'm using like that and it works.
OTHER TIPS
You can trying to use timer control. Here is the sample: http://www.dotnetheaven.com/article/auto-refresh-data-on-page-using-ajax-in-vb.net
Hope this help.