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

0

I need to format TextBox <asp:TextBox runat="server" id="txtValor" MaxLength="20"></asp:TextBox and I am very difficult, first this grid is opened in a modal, that is, when I create the main page I can not get control by javascript and put the mascara, have not been initialized.

I need to do this formatting only when I open this modal and load this grid, how can I perform this formatting for 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>
    
asked by anonymous 11.01.2018 / 21:37

1 answer

1

You can leave in the TextBox the onkeypress="return isNumberKey(event)" property 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 it will be interrupted.

    
12.01.2018 / 15:55