Cara Disable/Turn off browser autocomplete feature in TextBox in asp.net

Cara Disable/Turn off browser autocomplete feature in TextBox in asp.net

Disable auto complete in asp.net textbox controls

ada 3 cara untuk melakukan hal ini,silakan pilih yang menurut anda mudah :

 1) Setting the autocomplete="off" in the Form tag to disable autocomplete on entire form as:

<form id="form1" method="post" runat="server" autocomplete="off">

2) Setting the autocomplete="off" in all the Textbox controls where you want to disable autocomplete as:
<asp:TextBox ID="txtName" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="txtContact" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="txtBankAccountNo" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="txtCreditCardNo" runat="server" autocomplete="off"></asp:TextBox>

3) At run time turning this feature off via the attributes of the controls in the code behind as:
        txtName.Attributes.Add("autocomplete""off");
        txtContact.Attributes.Add("autocomplete""off");
        txtBankAccountNo.Attributes.Add("autocomplete""off");       
        txtCreditCardNo.Attributes.Add("autocomplete""off");

Implementation: let's create an asp.net example web application to see it in action. 
  • In the design page (.aspx) design the form as: 
<form id="form1" method="post" runat="server" autocomplete="off">
    <div>
    <fieldset style="width:300px;">
    <legend>Disable Autocomplete in TextBox example</legend>
        <table>
    <tr>
    <td>Name: </td>
    <td>
        <asp:TextBox ID="txtName" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Contact No.: </td>
    <td>
        <asp:TextBox ID="txtContact" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Bank Acccount No.: </td>
    <td>
        <asp:TextBox ID="txtBankAccountNo" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Credit Card No.: </td>
    <td>
        <asp:TextBox ID="txtCreditCardNo" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
    </tr> 
    </table>
    </fieldset>
    </div>
    </form>
  
Note: I have demonstrated all the 3 methods to turn off the auto complete feature in the example below. You just need to use any one of these methods.


C#.Net Code to disable Auto complete featurein asp.net textbox
  • In the code behind file (.aspx.cs) write the code on page load event as:
protected void Page_Load(object sender, EventArgs e)
    {
        txtName.Attributes.Add("autocomplete""off");
        txtContact.Attributes.Add("autocomplete""off");
        txtBankAccountNo.Attributes.Add("autocomplete""off");       
        txtCreditCardNo.Attributes.Add("autocomplete""off");
    }


VB.Net Code to disable Auto complete feature in asp.net textbox
  • In the code behind file (.aspx.vb) write the code on page load event as:
Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load
        txtName.Attributes.Add("autocomplete""off")
        txtContact.Attributes.Add("autocomplete""off")
        txtBankAccountNo.Attributes.Add("autocomplete""off")
        txtCreditCardNo.Attributes.Add("autocomplete""off")
    End Sub 
  • Now run the application and enter any record and click on submit. Refresh the browser by pressing F5 and again try to enter the same record and you will see previously filled data is not showing while entering.

jadi begitulah caranya,ni berlaku untuk C# dan vb.net .

selamat bercoding teman.

0 KOMENTAR