更改为服务器端时如何获取标签文本?

时间:2014-07-18 20:35:12

标签: javascript asp.net vb.net

我试图在不回发时将标签文本分配给隐藏字段,但是我失败了。这就是我所做的。

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

问题出现在else子句中,其中值没有使用新标签值进行更新。 lblPayPeriodStartDt.Text没有更新。

标签的值是日期,每次我在客户端使用日历控件更改日期时它都会更新。但是,标签的值不会刷新该值。

<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);"/>

2 个答案:

答案 0 :(得分:2)

您不会在后面的代码中获得您在客户端修改的<asp:Label的值。如果我是正确的,ASP.NET标签将在客户端呈现为span元素:

我认为只有在客户端更改为输入控件和值的控件才会在viewstate上更新,因此您唯一的选择是坚持隐藏字段。

你只需要做相反的事情。

1 。将隐藏字段传递给js函数,并在js函数PayPeriodsPayroll中更新客户端隐藏字段的值,如下所示

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

然后在你的页面加载

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

答案 1 :(得分:1)

每次更改日历控件中的日期时,您肯定不会回发到服务器。 您可以使用__doPostback() function.从javascript对服务器进行回发 看到这个链接: __doPostback function example

相关问题