Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allow only number keys 0-9 in a TextBox

Status
Not open for further replies.

BunBo900

MIS
Dec 11, 2009
50
0
0
US
Hello everyone,

How do I create a textbox that takes exactly 4 number.

Thanks in advance.
 
I tried RegEx Validation control as you suggested and I kept getting this error below. I am using it inside <formview></formview> control don't know if this would cause error because when I use it inside <form></form> control seem to work fine. Thx.

Compiler Error Message: CS0103: The name 'lblOutput' does not exist in the current context

Line 11: if (Page.IsValid)
Line 12: {
Line 13: lblOutput.Text = "Page is Valid!";
Line 14: }
Line 15: else

**********My code**************
Code:
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Calendar.aspx.cs" Inherits="Calendar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">

<script runat="server">
 
       void ValidateBtn_Click(Object sender, EventArgs e) 
       {
          if (Page.IsValid) 
          {
             lblOutput.Text = "Page is Valid!";
          }
          else 
          {
             lblOutput.Text = "Page is InValid!";
          }
       }
 </script>
    <title>Booking Calendar</title>

</head>
<body>
    <form id="form1" runat="server">
    <table border="0" width="100%">

        <tr>
            <td>
                <h2>Booking Calendar</h2>
            </td>
        </tr>
        <tr><td><br /></td></tr>
    </table>

    <asp:Calendar id="calSchedule" Caption="S6 Calendar" Font-Size="Medium" BackColor="AliceBlue" CellSpacing="5" CaptionAlign="Left" OnDayRender="calSchedule_DayRender" Runat="server" />
    <br />

    <asp:FormView id="frmSchedule" AllowPaging="false" DataSourceID="srcSchedule" Runat="server">        

        <EmptyDataTemplate>
                             
        </EmptyDataTemplate>
  
        <ItemTemplate>
         
        </ItemTemplate>
    
        <%--Insert--%>
        
        <InsertItemTemplate>        
        <table border="1">
        <tr valign="top">
             <td colspan="3">
                <asp:Label id="lblOutput" 
                     Text="Enter a 5 digit zip code" 
                     Font-Name="Verdana" 
                     Font-Size="10pt" 
                     runat="server"/>
             </td>
          </tr>
        <tr>
            <td>
                <asp:Label id="lblStartTime" Text="Start Time:" AssociatedControlID="txtStartTime" Runat="server" />
            </td>
            <td>
                <asp:TextBox id="txtStartTime" Text='<%#Bind("StartTime") %>' TextMode="SingleLine" Runat="server" />
            </td>
            <td><<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="txtStartTime"
                     ValidationExpression="\d{5}"
                     Display="Static"
                     EnableClientScript="false"
                     ErrorMessage="Enter must be 5 numeric digits"
                     runat="server"/></td>
        </tr>
        <tr>
            <td>
                <asp:Label id="lblEndTime" Text="End Time:" AssociatedControlID="txtEndTime" Runat="server" />
            </td>
            <td>
                <asp:TextBox id="txtEndTime" Text='<%#Bind("EndTime") %>' TextMode="SingleLine" Runat="server" />
            </td>
            <td><br /></td>
        </tr>
      
        <tr>
            <td colspan="3" align="center">
                <asp:Button id="btnInsert" Text="Insert" OnClick="ValidateBtn_Click" CommandName="Insert" Runat="server" />
                <asp:Button id="btnCancel" Text="Cancel" CausesValidation="false" OnClick="Cancel_OnClick" Runat="server" /> 
            </td>

        </tr>
        
        </table>             
        </InsertItemTemplate>
                        
    </asp:FormView>
 
if it is inside a formview then you need to do a findcontrol to get access to it.
 
Thanks. I look into that findconrol. Can you provide a sample specific to my case if possible.
 
In the ItemInserting event(you may have to use a different event) do something like:
Code:
Label mylabel = (Label)e.item.FindControl("lblOutput");
myLabel.Text = "Your Text Here";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top