kmarks2
kmarks2

Reputation: 4885

asp.net Label cannot ever be modified from code-behind

I have a panel/updatepanel that contains an asp.net label. It seems that under no circumstances whatsoever the Text field of this control can be changed. Code:

    <asp:Panel runat="server" ID="Panel1" Width="100%">
    <asp:UpdatePanel runat="server" ID="updRouteGroup" UpdateMode="Conditional">
      <Triggers>
        <asp:PostBackTrigger ControlID="btnDisableOnHold" />
      </Triggers>
      <ContentTemplate>        
       <asp:Panel ID="pnlImpExcel" runat="Server" >
      <div style="width:100%">
       <table colspan="0" width="100%" cellpadding="0" cellspacing="0">
          <tr>
             <th colspan="3">
                On Hold Music
             </th>
          </tr>
          <tr style="height:10px"></tr>
          <tr>
             <td align="left" valign="middle" style="width:33%"><div id="fine-uploader"></div></td>
             <td align="center" valign="middle"><asp:Label ID="lblOnHoldFile" runat="server" Text="Current file: none" /></td>
             <td align="right" valign="middle"><asp:Button ID="btnDisableOnHold" runat="server" style="margin-right:7px;width:87px;" Text="Disable Hold Music" CssClass="button" OnClick="btnDisableOnHold_OnClick" /></td>
          </tr>
          <tr><td colspan ="2" align="center"><asp:Label runat="server" ID="lblUploadError" style="color: Red;" Visible="false" /></td></tr>
       </table>              
       </div>
       </asp:Panel> 
       </ContentTemplate> 
    </asp:UpdatePanel> 
    </asp:Panel>

When a postback occurs I might need to update this, so I check some conditions and then attempt to modify the text by doing:

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.IsPostBack)
       lblOnHoldFile.Text = "Some text.";
}

This code is probably bad, I've little experience with ASP.NET and have inherited this project. But I still cannot understand what the point of exposing these labels in the code-behind is if you cannot modify them at all. How can I set the .Text field of the control lblOnHoldFile? Thanks.

Here is what's in btnDisableHold_OnClick:

  protected void btnDisableOnHold_OnClick(object sender, EventArgs e)
  {
     //Update some records

     // Clear out any existing file from the label:
     lblOnHoldFile.Text = "Current file: none";
  }

Upvotes: 1

Views: 4163

Answers (3)

Karl Anderson
Karl Anderson

Reputation: 34834

That is your problem, this line:

lblOnHoldFile.Text = "Current file: none";

You have undone the text change you made in Page_Load with the above line in your click event handler.

The Text property of your label got changed twice; once to Some text in the Page_Load and then back to its original value in btnDisableOnHold_OnClick method. This makes it appear that setting the Text value does nothing, when in reality it was changed.

UPDATE:

To have JavaScript create a postback for your code, then you need to invoke the __doPostBack function.

Read Understanding the JavaScript __doPostBack Function

Upvotes: 2

Nick
Nick

Reputation: 886

kmarks2,

Your markup:

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"> <asp:Label ID="someLabel" runat="server"></asp:Label>
</asp:Content>

C# Code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        someLabel.Text = "This is text generated from code-behind, in c#";
    }

Upvotes: 1

Gloria
Gloria

Reputation: 1053

Try adding updRouteGroup.Update() after the line where you change the text of the label. This line with an explicit call is needed as the Label is inside an UpdatePanel with UpdateMode Conditional. It should not be required when the button btnDisableOnHold is clicked though.

Or else just change the UpdateMode to Always and that should work.

To read more about the Update Panels and their UpdateMode Property, check this link:

http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode.aspx

Upvotes: 3

Related Questions