0

I am trying to change the text on a label from a custom event. When I update the text through a button click event (Button1_Click), everything works. However, when I try to update the same label in a custom event(ArReceiver_OnArDataUpdate) nothing happens. ArReceiver_OnArDataUpdate is getting triggered properly.

Why does my event react differently than a button click event? What do I need to do to get my event working?

This is an out of the box web project in VS2013. Here's the code to explain better:

Code Behind:

namespace WinReceiver
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArReceiver.OnArDataUpdate += ArReceiver_OnArDataUpdate;
        }

        void ArReceiver_OnArDataUpdate(object sender, ArEventArgs e)
        {

            labelVoltage.Text = "Reset";
            UpdatePanel1.Update();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            labelVoltage.Text = "Button Clicked";
            UpdatePanel1.Update();
        }

    }
}

Default.aspx: (The site.master has the ScriptManager in it by default)

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="labelVoltage" runat="server" Text="..."></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

EDIT: I did find this question that says it's not possible, but can someone explain why this may not be possible? I'm ok with a page refresh too, but when I tried Respone.Redirect I got exceptions.

Community
  • 1
  • 1
  • Since the click event causes a postback, this is why the click event causes the button label to change. The event in question won't do this (this is a guess as I'm unfamiliar with this event). You can use signalR, AJAX long polling or some other technique to make a server event update the client, but I think your solution is a little too simple to work as expected. – SpaceBison Sep 29 '14 at 15:57

1 Answers1

1

If OnArDataUpdate is a static event, it's likely to be fired when the ASP.NET page rendering process is out of scope, and thus the label is not existent because it isn't in scope of the page being processed (since ASP.NET is stateless).

However, if ArReceiver is a control on the page, it should process fine, and the issue could be within the control or something overriding it (depending on when the event fires, it may get overridden by viewstate).

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • This is very old, but your direction was correct. I had very poor conceptions of how webforms worked, but I did get it figured out after this. – Matthew Hilgenfeld Nov 21 '14 at 23:34