Get values of a textbox changed by javascript - c#

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.

Related

Can not obtain correct Text field input in onLoad() method, when text was entered by JQuery

I am trying to pass a value entered by JQuery in a text field to text INPUT towards my ASP.NET backend handler that looks at respective ASP.NET control's Text property.
It turns out that
$("#inputid").val(mynewvalue);
call does update visible text in my text field, but nevertheless I do not receive it when I examine control's Text function in asp.net in onLoad() method that is called after postback.
When onLoad method finishes and further postback work is happening, that is, other routines are called, only then textbox's Text property returns what was entered there by jquery.
Maybe I just have no right to examine these fields in onLoad() method?
I however need to get that Text property already on onLoad stage. I was thinking if there is some way to modify VALUE
Do you have runat="server" on your textbox control. My guess is that you don't (if I understand the question!)

How to make textbox as not editable in asp.net(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;

control in control losing textBox value - probably because of repeater

I have a problem: I have a user control in which there is a nested second user control. In that second control I have a textBox. During postback the value of the textbox is always the same (default one), even if I change text in that textBox.
Is this a common problem?
I just noticed that it might me caused bye repeater cause textBox is inside repater. Do you know how to foce textBox to keep value beetween postbacks if its in repeater ?
No, this is not a common issue. You should check where the control is being added and were the default value is being set. Either one could be causing you troubles.
This is not the problem, it is in your code. I guess your value is repopulating the old one in the Page_load event of the usercontrol.

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.

Categories

Resources