开发者

Setting an asptextbox to read only from the code behind

开发者 https://www.devze.com 2022-12-28 21:59 出处:网络
I need to change the readonly property of a asptextbox from the code behind based on certain conditions.

I need to change the readonly property of a asptextbox from the code behind based on certain conditions.

I know how to 开发者_StackOverflow社区run a function when the page loads, but I don't know how to reference the form fields to change the readonly property.

Can anyone point me in the right direction?

Thanks in advance,

Anthony


I think dtryan is wrong. If you use Enabled then you cannot type anything at all int he box. If you want to use read only then use the read only property.

Like this

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

protected void Page_Load(object sender, EventArgs e)
{
    // some text that is put into the textbox.
    TextBox1.Text = "Some Text that you can't edit but can copy out of.";
    // if read only is set to true then you can't change the text but copy it out.
    TextBox1.ReadOnly = true;

    // if enabled is set to false then you can't change the text or copy the text out.
   //TextBox1.Enabled = false;
}

if this is your code

<asp:TextBox ID="Req_DateTextBox" runat="server" Text='<%# Bind("Req_Date", "{0:d}") %>

Then it would be Req_DateTextBox.ReadOnly = true;


The ASP Textbox also has its own ReadOnly property, but none of the other ASP input form elements have it.

You can access the similar disabled html attribute through the Enabled property.

MyTextbox.Enabled = False 'makes control readonly

More info here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号