user8811
user8811

Reputation: 75

How do I get text of label when changed to server side?

I'm trying to assign the text of a label to a hidden field when not post back but I fail. This is what I done.

If Not IsPostBack Then
            Dim structPayperiod As strcPayperiodDet


            structPayperiod = objTimeSystem.getCurrentPayPeriod()
            hdnPayperiodseq.Value = structPayperiod.Payperiodid
            hdnPayPeriodStartDt.Value = structPayperiod.startdate.ToString

            lblPayPeriodStartDt.Text = structPayperiod.startdate

            displayPayrollIdOrgs(objTimeSystem.getPayrollIDOrgs())

            grd_Employees.Visible = False

            RptErrorsMessages.DataSource = objTimeSystem.getErrorMessages()
            RptErrorsMessages.DataBind()
        Else
            hdnPayPeriodStartDt.Value = lblPayPeriodStartDt.Text.ToString

        End If

Problem comes in else clause where the value doesn't get update with new label value. lblPayPeriodStartDt.Text is not updating.

The value of label is date and it updates every time I change the date using calender control on client side. But, the value of the label doesn't refresh with that value.

<asp:Label ID="lblPayPeriodStartDt" runat="server"></asp:Label>
<img src="../Images/calendar.gif" class="clsCursorHand" alt="" title="Select Pay Period"
                            onclick="Javascript:PayPeriodsPayroll('<%=lblPayPeriodStartDt.ClientId %>',event);"/>

Upvotes: 3

Views: 1106

Answers (2)

Dennis R
Dennis R

Reputation: 3237

You are not going to get the value of the <asp:Label you modified on client side at the code behind. If I'm correct ASP.NET label is rendered as a span element in the client side:

I think that only the controls that are rendered as input controls and values changed at client side are updated on viewstate, so your only resort is to stick to the hidden field.

You just have to do the other way around.

1.Pass the hidden field to the js function and update the value of the hidden field at the client side in your js function PayPeriodsPayroll like below

 function PayPeriodsPayroll (hdnObj)
 {
   var hdnPayPeriod = document.getElementById(hdnObj);
   hdnPayPeriod.val('the value you want to set');
 }

Then in your pageload

If Not IsPostBack Then
   ....
Else
    // update label with the hidden field value if you need it
    lblPayPeriodStartDt.Text = hdnPayPeriodStartDt.Value
End If

Upvotes: 2

Nahum Fabian
Nahum Fabian

Reputation: 180

surely you are not posting back to the server every time you change the date in the calendar control. You can do a postback to the server from javascript using the __doPostback() function. see this link: __doPostback function example

Upvotes: 1

Related Questions