7

I've creating one ASP web application, in that application one form have to update date value in the textbox field i put calender button near that textbox. it can update the date in that textbox field but its editable . i want update date value through only using calender button , i used read-only property but return empty value so not works .

0

6 Answers 6

12

Try client side html readonly attribute instead of ASP.NET server side readonly.

myTextBox.Attributes.Add("readonly", "readonly");

From MSDN,

The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.

This is why textbox with server side readonly attribute has null value in postback.

Sign up to request clarification or add additional context in comments.

Comments

5

You can use either TextBox1.Enabled = false; OR

TextBox1.Attributes.Add("readonly","readonly");

Difference is that if you make enabled= false then you cant pass the value of the textbox. If you need to pass the value of the textbox then you should use read-only property of textbox.

Comments

1

On page load event of your code behind file just write following code:

yourTextBoxName.Attributes.Add("readonly","readonly");

you can also do same by using property

Enabled="false"

on your aspx page but then you will not be able access its value in code behind page.

1 Comment

"yourTextBoxName.Attributes.Add("readonly","readonly");" is better becaue it allows your textbox to be resizable and "Enabled="false"" not.
1

You can make textBox non-editable in asp.net

1 Comment

<asp:TextBox ID="txtBox1" runat="server" ReadOnly="true"></asp:TextBox>
0

By using read only it will give problems on post back just set this java script property

onkeydown="javascript:return false"

by using this u can have properties like readonly and have absolutely no problem will araise

Comments

0

You might want to use this:

TextBox1.Enabled = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.