イベントをサブスクライブするメソッド内の UpdatePanel 内のコントロールを更新しようとしている ASP.Net noob は、うまくいきません。
いくつかの背景として、私は標準の POTS 電話とインターフェイスする DLL を作成し、電話側でのさまざまな発生 (電話のピックアップ、電話の呼び出し音など) を .Net イベントにマップしました。
私の ASP.Net ソリューションでは、DLL を追加し、電話をインスタンス化し、イベントをサブスクライブするメソッドで、メソッドに渡される EventArgs オブジェクト内の情報で UpdatePanel 内のさまざまなラベルを更新したいと考えています。
ブレークポイントを使用すると、Phone オブジェクトが期待どおりに機能していること、イベントが想定どおりに発生していること、EventArgs に想定されている情報が含まれていることがわかります。
ただし、UpdatePanel 内のラベルは更新されません。以前のバージョンのアプリケーションを Windows フォームとして作成しましたが、別のスレッドから UI を更新するたびに、最初に true かどうかを確認する必要があったことを思い出しInvokeRequired
ます。true の場合はメソッドを呼び出しますが、Invoke
これに相当するものはわかりません。これは ASP.Net にあります (存在する場合)。
マークアップは以下のとおりです (これは、基本を把握するために作成した単なるサンプル プロジェクトです)。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!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">
#form1
{
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" style="text-align: center" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ScriptManager1" EventName="Load" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
コードは次のとおりです。
using System;
//My DLL
using Maximiser;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
//My Phone Object
private Phone _ThisPhone { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//Instantiating Phone Object with IP Address and Port
_ThisPhone = new Phone("8.8.8.8", 8888);
//Hookstate event indicates phone is off/on the hook
_ThisPhone.HookState += new EventHandler<Maximiser.Events.Hookstate>(thisPhone_HookState);
}
void thisPhone_HookState(object sender, Maximiser.Events.Hookstate e)
{
//I want to update my Label with the phones current HookState
Label1.Text = e.State;
//Now I want to refresh the UpdatePanel but not reload the page
UpdatePanel1.Update();
}
}
}
以下に示すように、メソッドは確実に実行されています。