Eggybread Eggybread - 5 months ago 39
Vb.net Question

FindControl & Change Textbox to Integer?

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)

Answer

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.