How to get value in a textbox that is readonly - asp.net

I have a textbox.In its onclick iam calling a javascript fn to call calendar.Texbox is readonly. Clicking on textbox calendar is coming and value is showing in textbox. But on clicking submit button where I have written code to save, there i am not getting value in textbox. What may be the reason for that?

I suspect that your text box has the disabled attribute instead of readonly which prevents it from posting its value to the server.

I had this problem too, there is a really simple workaround
Instead of making the textbox ReadOnly using properties option. Do it thru the code behind adding this code:
YourTextBox.Attributes.Add("readonly", "readonly");

You can get the value by using
Request.Form[YourTextBox.UniqueID]

I'm guessing the textbox is disabled so that people have to use the calendar control to enter a date? The textbox is just for showing the selected date?
If you want the textbox to stay readonly (i.e. disabled client-side), have a hidden input that has the value you want to handle on the server is the way to go probably. So add an additional input for to the page. Use
<asp:HiddenField ... runat="server" />
So the client-side code that updates your readonly textbox will also update your hidden input.

if readonly property is true, its not break your .cs call. you can use this as always:
here are your inputs:
<asp:TextBox ID="txtBraid" runat="server" Text="Im sooo readonly" ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblBraid" runat="server" Text="Im gonna change, i promise"></asp:Label>
on .cs page put these to onClick function or something:
lblBraid.Text = txtBraid.Text;

Related

Disable html required tag in vb.net

i need to disable the required tag of this textbox when a submit button is click
<asp:Textbox runat="server" id="username" required name="username" type="text" placeholder="myusername"/>
i tried to write the following code but it did not work
Dim tbUserName As TextBox =Page.FindControl("username")
tbUserName.required = False
can you please help me !
Since required is not part of the ASP.NET TextBox control's properties, thus there is no server-side property equivalent.
You can use the following to remove it:
username.Attributes.Remove("required")
Try this:
tbUserName.Enabled = False
Also, "Required" isn't a valid propert for the Textbox control. ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.aspx
required is client-side attribute which does not have direct equivalent on server-side.
You can try
username.Attributes("required") = "false"
this works when you're already on the server. If you need to do this on client - handle form's onsubmit event and do something like
$get('username').setAttribute('required', 'false');
Of course the easiest way would be simple remove that attribute from textbox markup.

Disabling an ASP.net textbox without actually disabling it?

Within an ASP.Net application I have, there is a textbox that gets a date from a CalendarExtender. When the textbox is populated it checks that date with another date on the form and displays a modalpopupextender popup if the dates are wrong. However, I DO NOT want to allow user input into this textbox, so when I set the ReadOnly field to false and tried Enabled to false, it doesn't allow manual entry however it ALSO disabled the postback and will not call the TextChanged event to fire the modalpopupextender.
So is there a way to disable manual entry and not set it to ReadOnly?
I figured it out, simply enter onkeypress="return false;" within the HTML tag
Try this
<asp:textbox id="txt1" onfocus="blur()" runat="server"/>
this worked for me.
Add the below properties in the tag of textbox
onkeydown="return false" onpaste="return false"
ex:
<asp:TextBox ID="TillDate_TextBox" runat="server" onkeydown="return false" onpaste="return false"></asp:TextBox>
the first property block typing in textbox and the second property block pasting in it
I'm not familiar with the exact components you are using, however the usual way to accomplish things like this is the following. Have selecting the date on the calendar modify the value of a hidden form field. This will restrict the user from editing the value directly. Then create another element like a div or a span, and use javascript to update the span/div to the value selected on the calendar.

Can't change an asp page because of a required validator

On one of my asp.net pages I have a few textboxes and a required validator attached to them. Everything is ok with validation, but when I want to change page to another required validator doesn't allow it because I don't fill in a textbox :/
How to allowed change page without fill a validate textbox? I must create a condition in "exit page" (how is it named ?) event disabled required validation?
Please help ;)
If you're using some control to move to the next page, set it's CausesValidation property to False.
For example, if you're clicking a button that moves you to the next page, it would be like:
<asp:LinkButton id="myButton" runat="server" CausesValidation="False" .... />

AJAX Calendar extender on modalpopup returns null

I'm having some trouble getting a datetime from calextender on a modalpopup I have.
<asp:TextBox ID="txtPopEndDate" runat="server" Enabled="false"></asp:TextBox>
<img id="calButton" alt="" title="Show Calendar" src="~/App_Themes/Main/img/calendar.png"
runat="server" height="20" style="cursor: hand;" />
<ajax:CalendarExtender Animated="true" TargetControlID="txtPopEndDate"
runat="server" PopupButtonID="calButton"
Enabled="true" ID="calExtender" Format="dd/MM/yyyy"/>
are the controls I have to select the date, now when I press the ok button I handle the data including the date. However, if I try to fetch the date using calExtender.selectedDate property I get nothing. the same goes for manually getting the string from the textbox and parsin that.
I have to mention that the other data from textboxes and dropdowns on that popup work without a glitch. any help here would be greatly appreciated.
Your textbox has enabled = false. I think this spits out a disabled attribute set to true in the html, therefore the post of the form would not send the value. Why dont you want the textbox enabled?
Im also assuming your trying to grab the value after a postback ? Is that true?
yep,
in the meanwhile I found a solution to the problem. as you said, the textbox apparently needs to be enabled. this means the option for erroneous dates.
I got the value from the textbox and parsed it that way with a DateTime.TryParse to secure a correct date.
if anyone has a better option, let me know
We used Enabled="false" on TextBoxes linked to the CalendarExtender all through our application without a problem. Then I added a date picker to a page which refused to co-operate. The TextBox value was never available on PostBack.
I found the solution here:
http://www.west-wind.com/weblog/posts/2005/Dec/20/ASPNET-20-ReadOnly-behavior-change-when-EnableViewState-is-false
What I found was:
I could set ReadOnly = true, then retrieve the value from the Request on PostBack with TextBox1.text = Request[TextBox1.UniqueID].
I could use TextBox1.Attribute.Add("readonly", "readonly"), then the Text property was set as I expected.
I could not retrieve the value with Enabled = false; or TextBox1.Attribute.Add("disabled", "disabled")
We didn't have ViewState disabled on this page and we didn't get this behaviour on other pages, so I guess this behaviour can be triggered by something else too.

Adding javascript to the OnBlur property of an ASP.NET text box control

Is there a way to specify some JavaScript to execute on the OnBlur event of an ASP.NET text box? It seems to me like if I add any event handlers to the TextBox object they will just cause postbacks to the server isntead of doing what I want. Basically, I just want to be able to have the textbox be rendered in this HTML:
<INPUT type="text" onblur="alert('1234')" />
Thanks!
Could also go for:
<asp:TextBox runat="server" onblur="Javascript:alert('1234');" />
if you dont feel like setting it up in the codebehind.
Im guessing the reason why you end up with postbacks, must be because you have set AutoPostBack on the textbox to true. That makes the textbox postback when the client-side onchange event is triggered. Switch it to false, and it will act as a normal input-element.
In your codebehind, add this:
myTextBox.Attributes.Add("onblur","alert('1234');");

Resources