2

Im pretty new to this an FindControl is completely confusing me. Can anyone please tell me what the code should be to change the input from the TextBox: txtamount to an integer called 'increment'. I can't even find the control.. I've tried Ctype, Cint LoginView1.Findcontrol etc with no joy. Any help is appreciated. Here's the Front end followed by the VB Code...

<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 Code

Protected Sub AddPoints_Click(sender As Object, e As EventArgs)
   dim increment as integer = Cint(LoginView1.FindControl("txtamount"))
   response.write(increment)
Eggybread
  • 161
  • 1
  • 12
  • I assume you want the integer value of the text of the TextBox. You are currently converting the TextBox itself to an integer. – j.f. Jun 14 '16 at 17:53

1 Answers1

2

FindControl, as the name suggests, tries to find a control and a control is not something that you can convert to an integer

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

I suggest also to add a RangeValidator to be sure that the input is made only of valid numbers.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Many thanks for helping. That is giving me error: BC30108: 'Control' is a type and cannot be used as an expression. – Eggybread Jun 14 '16 at 17:57
  • I am learning myself and know quite a bit of VB now but think I need to do some starter courses to get the basics right first. – Eggybread Jun 14 '16 at 17:58
  • My fault, declared the variable C# way. – Steve Jun 14 '16 at 18:00
  • That works.. You're a life saver Steve !! Thanks so much. It's the main part of VB I need to get my head around. Although it works, It's still a bit tricky for me to follow even though I know it's probably easy :-) – Eggybread Jun 14 '16 at 18:05