How to make textbox as not editable in asp.net(c#) - c#

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 .

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.

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.

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.

You can make textBox non-editable in asp.net

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

You might want to use this:
TextBox1.Enabled = false;

Related

Hiddenfield and label in the same itemtemplate of a gridview

I have a template field in which I had a label and a hiddenfield. But when I tried to access the hiddenfield value using findcontrol I got a NULL Exception error. But when I put the seperate template field and use the hiddenfield then I could get the value.
I dont want to show the template at the same time want the value from hiddenfield. How am I supposed to proceed?
Just use a label with style="display:none;" inside any of the template fields. Doesn't matter if there is any other control along with it. As long as you don't mind users using developer tools to see it, you are good to go. If you don't want it to be there in the page at all, just set visible="false".
Depending on when you want to retrieve the hidden field the code should be something along the lines of this:
var hf = GridView1.Rows[e.RowIndex].FindControl("hiddenFieldId") as HiddenField;
If you post more code I can add more information.

Disabled textbox losses viewstate

I am dynamically generating the form based on the drop down selected.
The form consists of fields (data entry for decimal values + few text fields). Have to add all the decimal values at the end and update the Total TextBox with that value. Total Textbox is disabled.
When I click Save button on the form after the user have entered their values, whole form is persisted in viewstate except the disabled textbox. When I enable the textbox, everything works fine. Mind you, I am dynamically generating the form and updating the value of the total textbox using javascript to calculate (adding all decimal fields).
P.S. I am doing everything right for persisting the viewstate.
So what has the enabled/disabled got bearing on the viewstate
Basically, I added two statements to get it working.
txtBox.Attributes.Add("readonly", "readonly");
txtBox.Style.Add("color","gray");
When I used txtBox.Enabled = false, it didn't persist viewstate but did it alternatively using above two statements in my code-behind page
Yes, disabled form element will not send it's value to server side, you can look request header. disabled element not appeared at "get" or "post" collection.
If you want set user can't edit it, you can set it as readonly.
Add javascript on the page:
function enableTextBoxes() {
$("input[type='text'][disabled='disabled']").removeAttr("disabled");
}
And add to server code (in Page_Load, PreRender or some else method)
ClientScript.RegisterOnSubmitStatement(typeof(Page), "enableTextBoxes", "enableTextBoxes();");
If you use UpdatePanels then utilize the ScriptManager.RegisterOnSubmitStatement method
Please create custom text box rather than using actual textbox instance.
inherit textbox in your custom textbox and add this textbox in your dynamic form.

TextBox is not showing new value in code-behind

I have created a asp:TextBox. its disabled. but its values is changed via javascript. When i click a button to get the updated value in that textbox it always show me empty ("").
In my Page_Load i am doing everything in if(!Page.IsPostBack) so there is no chance of updating TextBox value on postback in Page_Load.
Where i could be wrong? how can i get updated value?
Disabled textboxes are not posted back.
See: http://www.velocityreviews.com/forums/t360866-textbox-no-value-if-disabled.html
Soultion: use a hidden or readonly field.
When you disable a control it's value is not send back in the postback. You should change the textbox to be readonly.
Solution 1: Set your textbox readonly instead of making it disabled.
Solution 2: If you really have to disable the textbox, you can add an asp:hiddenfield to record the text in the textbox, you can get the value at server side then.

Get values of a textbox changed by javascript

strange bug:
i have an ajax datepicker added to a text box of my form.
i submit the form.. and I could receive all values excepting those of the datepicker checkboxes.
why is the .Text property empty of this elements?
Thanks
Is your TextBox disabled for user input, so that it might only be changed by javascript, by the datepicker?
The problem in that case, is that .NET "knows" that the control is disabled, and just assumes that the value cannot, then, have changed since it was rendered. So .NET will use the ViewStated value immediately, without checking the POST data.
There are two solutions to this:
Don't render the TextBox as disabled, but disable it with the datepicker script
Instead of relying on the TextBox's Text property, check Request.Form[myTextBox.ClientID]
Try accessing SelectedDate instead of Text.

Retrieving the value of a asp:TextBox

I have a disabled TextBox that I am editing the value of on the client side with JavaScript. When I try to retrieve the value on the server side it does not reflect the change made on the client side. If I set the TextBox's enabled attribute to true I can retrieve the value, but the user is able to put focus and edit the TextBox.
Is there a sane way to keep the user from giving focus and editing to the TextBox?
Use the textbox's ReadOnly property.
Edit: Based on OP's comment, this will probably not do the trick then.
Edit 2: From DotNetSlackers:
So what's the difference between these
two properties and why do both exist?
There are two differences between
these two properties, a trivial
difference and a subtle, profound one:
The two properties emit different markup. When you set Enabled
to False, the TextBox injects the
attribute disabled="disabled" intoits
rendered HTML. When you set the
ReadOnly property to True, the
attribute readonly="readonly" is
injected.
According to the W3C spec on HTML forms, disabled controls areNOT
"successful," while read-only controls
MAY BE "successful." A "successful"
control is one whose name/value pair
is sent back to the browser through
the POST headers or querystring.
Therefore, disabled controls are NOT
sent back to the ASP.NET page, while
read-only controls may be, depending
on the User Agent. (In my tests,both
IE 6and FireFox 1.5 send along the
read-only TextBox input.)
......
If you encountered this problem in
ASP.NET version 1.x you might have
found the TextBox's ReadOnly property
and used that instead of setting
Enabled to False. You could still have
a page's ViewState disabled and set a
read-only TextBox Web control's Text
property programmatically because the
TextBox value is sent back through the
form submission for read-only
controls. However, in ASP.NET version
2.0, things change just a bit, as noted by Rick Strahlin his blog entry
ASP.NET 2.0 ReadOnly behavior change
when EnableViewState is false. With
2.0, the TextBox control'sReadOnly property's behavior has changed
slightly. From the technical docs:
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.
What happens is that the client sends
along the value of the read-only
TextBox through the form values, but
the ASP.NET 2.0 engine does not take
that value and assign it to the Text
property of the TextBox on postback to
help protect against a malicious user
changing the read-only TextBox value
themselves. But this brings us back to
our earlier problem - if the value
isn't specified in the postback (or is
ignored, in this case) and ViewState
is disabled, the value will be lost.
Eep.
Rick'sworkaround was to just manually
read the value from the request
headers (this .TextBox1.Text =
Request[this.TextBox1.UniqueID];),
which poses a security risk and
introduces the problem that 2.0
addresses. The optimal approach is to
requery the value from the database
(or wherever you initially got the
programmatically-set value for the
read-only TextBox).
The moral of this blog post is that if
you have read-only data you can use
either disabled or read-only form
fields, it really doesn't matter
whether or not you receive back the
value of the form field in the form's
submissions. It shouldn't matter
because you shouldn't be
trusting/using that data to begin
with! If you have read-only data,
don't re-read it from a data stream
that the end user can tinker with!
Source
Browsers don't post values back in disabled input controls, as you've discovered. Probably the easiest way to work around this is to hook onto form submission, and re-enable the input as the form is being submitted; the user won't have a chance to edit the value, and it should get posted with the rest of the request.
An alternative might be to inject a hidden element into the form; this could either be maintained by your script, mirroring the displayed value, or added at the end, in a similar fashion to the above.

Categories

Resources