简体   繁体   中英

How to make ItemTempleField to ReadOnly Field in ASP.Net

I have a text box in a gridview that i would like to make it on ReadOnly but the text-box is disabled now and i want to change to ReadOnly so users can scroll up and down to see all the contents of the field. I basically want to restrict the field so users cannot type anything but at the sametime they should be able to scroll up and down. Here is what i have now:

foreach (GridViewRow gvr in GridView1.Rows)
{
    ((TextBox)gvr.FindControl("myTextBox")).Enabled = false;
}

thanks

Add the readonly attribute to the control instead of disabling it:

((TextBox)gvr.FindControl("myTextBox")).Attributes.Add("readonly", "readonly");

If you set .Enabled = false; or .ReadOnly = true; no values will be posted back (I'm not assuming you need them but just in case you do).

You should make the textbox readonly... you could also do so if the boundfield is converted to a template field..

<EditItemTemplate>
       <asp:TextBox ID="Address" runat="server" Height="74px" ReadOnly="True" 
                        Text='<%# Bind("address") %>' TextMode="MultiLine" Width="182px"></asp:TextBox>
 </EditItemTemplate>

The value for ReadOnly should be True and not ReadOnly ok.. Even if you do it at runtime using findcontrol then use True for ReadOnly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM