C#:标签不包含文字

时间:2010-05-22 13:58:17

标签: c# asp.net javascript

我想从标签中获取文字。但标签文本是用Javascript设置的。 在页面上我可以看到标签中有文本,但是当我调试它时会显示:“”。

那么如何从使用Javascript设置的标签中获取文本,至少这就是我认为的问题。

我的代码:

<asp:TextBox ID="txtCount" runat="server" Width="50px" Font-Names="Georgia, Arial, sans-Serif" ForeColor="#444444"></asp:TextBox>
<ajaxToolkit:NumericUpDownExtender ID="NumericUpDownExtender1" runat="server" Minimum="1"
 TargetButtonDownID="btnDown" TargetButtonUpID="btnUp" TargetControlID="txtCount"  Width="20" />
<asp:ImageButton ID="btnUp" runat="server" AlternateText="up" ImageUrl="Images/arrowUp.png"
 OnClientClick="setAmountUp()" ImageAlign="Top" CausesValidation="False" />
 <asp:ImageButton ID="btnDown" runat="server" AlternateText="down" ImageUrl="Images/arrowDown.png"  OnClientClick="setAmountDown()" ImageAlign="Bottom" 
 CausesValidation="False" />

<asp:Label ID="lblKorting" runat="server" />

 <asp:Label ID="lblAmount" runat="server" />

<asp:Button ID="btnBestel" runat="server" CssClass="btn" Text="Bestel" OnClick="btnBestel_Click1" />

JS

function setAmountUp()
{
    var aantal = document.getElementById('<%=txtCount.ClientID%>').value-0;

    aantal+=1;
    calculateAmount(aantal);
}

function setAmountDown()
{
    var aantal = document.getElementById('<%=txtCount.ClientID%>').value-0;

    if(aantal > 1)
        aantal -=1;

    calculateAmount(aantal);
}

function calculateAmount(aantal)
{

    var prijs = document.getElementById('<%=lblPriceBestel.ClientID%>').innerHTML -0;   
    var totaal = 0;

    if(aantal < 2)
    {
        totaal = prijs * aantal;
        document.getElementById('<%=lblKorting.ClientID%>').innerHTML = "";
    }

    else if(aantal >= 2 && aantal < 5)
    {
        totaal = (prijs * aantal)*0.95;
        document.getElementById('<%=lblKorting.ClientID%>').innerHTML = "-5%";
    }

    else if(aantal >= 5)
    {
        totaal = (prijs * aantal)*0.90;
        document.getElementById('<%=lblKorting.ClientID%>').innerHTML = "-10%";
    }

    document.getElementById('<%=lblAmount.ClientID%>').innerHTML = totaal;
}

C#

 private OrderBO bestelling;
    protected void btnBestel_Click1(object sender, EventArgs e)
    {
        bestelling = new OrderBO();
        bestelling.Amount = Convert.ToInt32(lblAmount.Text); //<--- THIS IS "" in the debugger, but on the page 10
    }

1 个答案:

答案 0 :(得分:2)

存储在标签内的文本不会在回发时发送回服务器。这是一种单向的事情。

您应该做的是将您想要返回服务器的任何文本放入隐藏的表单字段中,如下所示:

ASP.NET:

<asp:HiddenField runat="server ID="hfAmount" />

JavaScript的:

document.getElementById('<%=hfAmount.ClientID%>').value = totaal;

C#:

bestelling.Amount = Convert.ToInt32(hfAmount.Value);