![](/img/trans.png)
[英]How to load a .aspx page into contentplaceholder dynamically without postback?
[英]Dynamically change label text in aspx page without postback or page-load
我目前正在用C#创建一个asp.net Webforms网站。
我在此网站上的目标是能够从我当前运行的mqtt代理接收mqtt消息,并将其显示在一个简单的网站上。
我目前可以正常进行通讯,并且可以按我的意愿订阅和接收消息,但是我的问题是,在收到我的代码隐藏消息后,我无法在aspx中动态显示它们! 我目前正在尝试在asp:label中显示一个值,并且每当我收到一个新值时,我都希望更新标签文本以反映此情况。
再次,我的后台代码按预期工作,但是我的问题似乎是来自我的mqtt代理的消息没有引起页面加载或回发,这意味着我的aspx没有得到刷新。 我尝试使用JavaScript解决此问题,但这似乎不起作用! 这是我的代码的简化版本:
Aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="proof_of_concept.WebForm1" %>
<head runat="server">
<script type="text/javascript">
var jsVariable1;
function GetValues(){
var someVar1 = "<%=Variable1 %>";
if(someVar1 != null && someVar1 != jsVariable1){
jsVariable1 == someVar1;
$('#Label1').innerHTML = "Variable 1 =<br/>" + jsVariable1;
}
setTimeout(GetValues, 5000);
}
GetValues();
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="container" id="container">
<img src="/Images/TestImage.jpg" style="width:100%;" />
<div class="position1">
<asp:Label ID="Label1" runat="server" Text="Var1: <br/> Value"></asp:Label>
</div>
</div>
</form>
</body>
.cs:
namespace proof_of_concept
{
public partial class WebForm1 : System.Web.UI.Page
{
private static MqttClient myClient;
public String Variable1 = "No data yet";
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
if (!IsPostBack)
{
//Initialize connection to the mqtt broker (with a hardcoded URL)
myClient = new MqttClient("myBrokerurl", 1883, false, null, null, 0, null, null);
//Connect to the broker with an autogenerated User-ID
myClient.Connect(Guid.NewGuid().ToString());
//Check if the connection was established
Debug.WriteLine("Client connected: " + myClient.IsConnected);
//Subscribe to a topic at the broker (Again in this example the topic has been hardcoded)
myClient.Subscribe(new string[] { "mySubscribedTopic/#" },
new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
//Sets up an eventhandler for received messages to the subscribed topic(s)
myClient.MqttMsgPublishReceived += myClient_MqttMsgPublishReceived;
}
}
protected void myClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
//Check if a message was received
Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic);
variableSelector(e.Topic, Encoding.UTF8.GetString(e.Message));
}
protected void variableSelector(String topicString, String messageString)
{
if (topicString.Contains("var1") == true)
{
Variable1 = messageString;
//Databinding here was a test that didnt seem to do anything
Page.DataBind();
}
}
}
我不确定我的JavaScript是否相关,但是我将其编写为解决我的问题的变通办法(这是当我从经纪人处收到新消息时,标签文本没有得到更新)。
在我看来,代理正在将消息发送到设备A的服务器,并且页面的客户端在计算机B上,并且您尝试进行同步(如果是这种情况),请更新服务器上的数据库并使用与我的例子。
在我的示例中,我将重点介绍“我试图使用javascript解决此问题,但这似乎不起作用!”。
无论哪种方式,您都选择了错误的技术来执行此操作,WebForms会让您头疼。
使用Asp.Net MVC ...会容易得多。
页
<form id="form1" runat="server">
<asp:ScriptManager ID="MyScriptManager" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
<ContentTemplate>
<asp:Label ID="MyLabel" runat="server"> Postback number <%= Counter %></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
代码隐藏
public partial class MyPage: System.Web.UI.Page
{
protected int Counter { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
// MyUpdatePanel will perform a async post-back every 1000 milliseconds
int _newCounter;
var newCount = Request.Form["__EVENTARGUMENT"];
if (newCount != null)
{
if (int.TryParse(newCount, out _newCounter))
{
Counter = _newCounter;
}
}
return;
}
// Loading javascript that will trigger the postback
var _pageType = this.GetType();
var script =
string.Concat(
"var counter = 0;",
"setInterval(function() { ",
"__doPostBack('", MyUpdatePanel.UniqueID, "', counter);",
"counter++; }, 1000);");
ClientScript.RegisterStartupScript(_pageType, "AutoPostBackScript", script, true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.