Four C # TextBox editor of prohibited methods

Foreword

In general, Textbox there are two attributes can be set to prevent the editing, which is the most basic knowledge, the first two methods also I want to raise. As a practical method for the latter two, but may be applied to different environments.

A, ReadOnly property

This setting, Textbox controls the restriction can not be entered, but you can read existing text, style is also consistent with Textbox normal use.

1, the front end Readonly set to true, the following pattern:

 <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Text="测试"></asp:TextBox>  

2, in the background Readonly set to true, the following pattern:

TextBox1.ReadOnly = true;

Two, Enabled property

Setting this property will be turned into anti-gray style Textbox control, non-clickable.

1, the front end Enabled set to false, the following pattern:

 <asp:TextBox ID="TextBox1" runat="server" Enabled="false" Text="测试"></asp:TextBox>  

2, in the background Readonly set to true, the following pattern:

TextBox1.Enabled= false;

Three, onfocus method

Set JS events, onfocus event means that the cursor over this control, this.blur () is the cursor away, so they can achieve the purpose can not be edited, as follows.

<asp:TextBox ID="TextBox1" runat="server" Text="测试" onfocus="this.blur();"></asp:TextBox>  

Fourth, set TextModel style

Use CSS code, set TextModel property . Combined with the third, the benefits of this set up is that in some browsers can achieve the same purpose can not be edited, but can guarantee the original "X" number exists, it can also achieve clear content control purposes. After testing, this feature is not supported in IE browser, but Google browser.

Front-end code as follows:

<asp:TextBox ID="TextBox1" runat="server" Text="测试"  TextModel="search" onfocus="this.blur();"></asp:TextBox>  

Css style as follows:

<style>
    Input[type=search]::-webkit-search-cancel-button
    {
    -webkita-appearance: searchfield-cancel-button;
    }
</style>

Guess you like

Origin www.cnblogs.com/HymanWesteros/p/12118990.html