How to format a field with ASP.NET Webforms C#

Asked

Viewed 311 times

0

I need to format Textbox <asp:TextBox runat="server" id="txtValor" MaxLength="20"></asp:TextBox and I’m having a lot of difficulty, first this grid is open in a modal, I mean, when I create the main page I can’t get the control by javascript and put the mask, because these controls haven’t been initialized yet.

I need to perform this formatting only when opening this modal and loading this grid, as I can perform this formatting to decimal field?

<asp:UpdatePanel runat="server" ID="upnVincGridFuncionarios" RenderMode="Inline" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:GridView runat="server" ID="gdvVincFuncionarios" Width="100%" OnRowDataBound="gdvVincFuncionarios_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:Label runat="server" ID="lblNameHead" Text='Nome'></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblNameEmployee" Text='<%#DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Msisdn" HeaderText="Celular" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                        <asp:BoundField DataField="Cpf" HeaderText="CPF" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                        <asp:BoundField DataField="NumRegistration" HeaderText="Matrícula" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                        <asp:BoundField DataField="CostCenter" HeaderText="Centro de Custo" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />                        
                        <asp:TemplateField>
                             <HeaderTemplate>
                                <asp:Label runat="server" ID="lblValorHead" Text='Valor'></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <span style="display: none">
                                    <asp:Label runat="server" ID="lblVincIdEm" Text='<%#DataBinder.Eval(Container.DataItem, "IdEm") %>'></asp:Label>
                                </span>     
                                <asp:TextBox runat="server" id="txtValor" MaxLength="20"></asp:TextBox>                           
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>

1 answer

1

You can leave the property in Textbox onkeypress="return isNumberKey(event)" and in your javascript file (or even on screen) the following function:

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode != 46 && charCode > 31 
        && (charCode < 48 || charCode > 57))
         return false;

    return true;
}

With this, regardless of the mask, whenever a key is pressed in the textbox the event will be called and if it is not number will be interrupted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.