FindControl&将文本框更改为整数?

时间:2016-06-14 17:47:23

标签: asp.net vb.net findcontrol

我很陌生,FindControl让我很困惑。任何人都可以告诉我代码应该是什么来改变TextBox的输入:txtamount到一个名为'increment'的整数。我甚至找不到控件..我已经尝试了Ctype,Cint LoginView1.Findcontrol等没有任何乐趣。任何帮助表示赞赏。这是前端,然后是VB代码......

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent"Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"Runat="Server">

<div style="text-align:center;">

    <asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate>
            <h2>Please <asp:HyperLink ID="HyperLink2" runat="server"         NavigateUrl="~/register-account.aspx" CssClass="boldStyle">REGISTER</asp:HyperLink> or <asp:HyperLink ID="hypLogin" runat="server" CssClass="nyroModal boldStyle" NavigateUrl="~/login2.aspx?page=vipseller">LOG IN</asp:HyperLink></h2>

        </AnonymousTemplate>
        <LoggedInTemplate>
             <h1>Shop Keeper<h1>
             <h2>Enter cash amount being spent or redeemed by customer:</h2>

             <asp:TextBox ID="txtAmount" runat="server" MaxLength="15" TabIndex="8" ></asp:TextBox>
             <br><br>

             <asp:Button ID="btnSpent" runat="server" Text="Spent" TabIndex="10" CssClass="button greyShaded" onclick="AddPoints_Click" />
             <asp:Button ID="btnRedeemed" runat="server" Text="Redeem" TabIndex="10" CssClass="button greyShaded" /><br><br>
        </LoggedInTemplate>
    </asp:LoginView>
</div>
</asp:Content>

VB代码

Protected Sub AddPoints_Click(sender As Object, e As EventArgs)
   dim increment as integer = Cint(LoginView1.FindControl("txtamount"))
   response.write(increment)

1 个答案:

答案 0 :(得分:1)

FindControl,顾名思义,试图找到一个控件,而控件不是你可以转换为整数的东西

Dim tb As TextBox = DirectCast(LoginView1.FindControl("txtamount"), TextBox)
if tb IsNot Nothing Then
   response.write(CInt(tb.Text))

我建议添加RangeValidator以确保输入仅由有效数字组成。