Can't get text with text box when it is readonly? - c#

I have a text box
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>
The text in the text box is inputted through a Jquery DatePicker. In some code behind I am getting the text from this text box like so.
string x=checkIn.Text;
Why can I not pull the inputted date from the text box? I am guessing it is to do with the fact that it is readonly as when I remove this it works?
Can anyone help me?

In ASP.Net, if the readonly value is changed, it will revert to the original value on the postback.
You can use a wrokaround however, instead of specifying readonly declaratively, assign it as an attribute in code-behind. i.e.
instead of
<asp:textbox runat="server" id="checkIn" ReadOnly="true"...
apply this is code-behind
checkIn.Attributes.Add("readonly", "readonly");
But, the viewstate still may not work with this.
More info:
There is a subtle difference between readonly and disabled controls in HTML. The disabled ones will not be submitted with the form, however the readonly ones will. Literally, readonly is just readonly, but disabled is actually disabled.
From W3C: http://www.w3.org/TR/html401/interact/forms.html#h-17.12
(Get down to section 17.13.2 Successful controls under 17.13 Form submission)
ASP.Net however, reverts to the original value on postback if a control is declared like that i.e. if the attribute is set during init. Which is why setting the attribute later (in page load) will not affect this behavior.

Use HTML input tag instead of asp:textbox
Use this:-
<input type="text" readonly="readonly" runat="server" id="checkIn" clientidmode="Static"/>
Instead of this:-
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true"></asp:textbox>

Write the following code in your code behind file, for asp.net .cs or class file and for vb in .vb file.
You will have to write it on page load event, depending on if you want it before postback or after or irrespective of both...
textbox.Attributes.Add("readonly", "readonly");

Try to make readonly to false before posting the values through jQuery or Java-Script. It should work.
$('#tbox').removeAttr('readonly');
Suppose I have a asp:button then on client click I need to call a function which remove the read-only attribute from the text-box.
<asp:Button ID="MessageButton" runat="server" Text="Hellow"
OnClientClick="return changeAttribute()" />
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>
and change attribute function in javascript as follows
function changeAttribute()
{
$('#checkIn').removeAttr('readonly');
return true;
}

You can also leave the readOnly attribute off in the asp:TextBox control and use jquery $('#textbox').attr('readOnly','readOnly').

You can actually get the value of a readonly textbox if it is within the form.
var text = Page.Request.Form[TextBox.UniqueID];
Sorry this response is a few years late!

Apply in asp.cs Page : AttrName.Attributes.Add("readonly", "readonly");

Please use:
Request.Form[txtText.UniqueID]

I had the same issue with jQuery, however fixed with a trick:
$('#txtBoxID').removeAttr('readonly');
/// Do your stuff here..
$('#txtBoxID').add('readonly');
;)

Related

Disabling autocomplete for textbox within ModalpopupExtender

I have a modalpopupextender to add new users in an application. The extender has username and password textboxes. I do not want these textboxes to be autopopulated by values as these are suppose to be new values not the old used ones. I tried setting:
<asp:TextBox ID="userNameTextBox" runat="server" AutoCompleteType="None">
and also Disable but nothing works.
What am I missing? How can I do it?
Try this as your code :
asp:TextBox Runat="server" ID="Textbox1" autocomplete="off">
you can also check this link autocomplete off link

How to reference code behind variable in ASPX?

I'm not sure why when referencing a code behind variable in an asp.net control, I get the text of the reference:
<%=this.Person.Contact.Emails[0].EmailAddress%>
This outputs the literal reference text:
<asp:TextBox ID="EmailAddress" runat="server" Text="<%=this.Person.Contact.Emails[0].EmailAddress%>"></asp:TextBox>
This renders the variable value:
<input id="testfield" type="text" value="<%=this.Person.Contact.Emails[0].EmailAddress%>" />
Any ideas how I can get the variable value in the asp.net control?
You could say:
EmailAddress.Text = this.Person.Contact.Emails[0].EmailAddress
in your code behind
I prefer the solution in Code Behind in hunter's solution but another option would be to use data binding with #:
<asp:TextBox ID="EmailAddress" runat="server" Text="<%# this.Person.Contact.Emails[0].EmailAddress%>" />
But then you have to bind the server control in code-behind:
EmailAdress.DataBind();
The = sign is like a call to Response.Write() at this place and just outputs whatever follows as text.

Unable to get value Textbox by id in javascript

I have text boxes and it has values let say. I am calling my javascript method on one of textbox 'onblur'. my function is:
function CalculateExpectedProductPrice() {
alert("hi i called");
var originalPrice = document.getElementById('hdnSelectedProductPrice').value;
var numberOfLicenses = document.getElementById('txtNumberOfLicense').value;
var enteredAmount = document.getElementById('txtAmount').value;
alert(originalPrice);
alert(numberOfLicenses);
alert(enteredAmount);
}
i am getting alert message as ""hi i called". but not further.
But some i am not getting values of these controls.
*Edited:* My HTML is :
<asp:HiddenField ID="hdnSelectedProductPrice" runat="server" />
<asp:TextBox ID="txtAmount" runat="server" Width="250px"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtNumberOfLicense" runat="server" Width="35px" ></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtNumberOfLicense" EventName="" />
</Triggers>
</asp:UpdatePanel>
Will there any impact master-content page . because script is in content page and html also on same content page.Also let me know you, I am using wizard control where as all these controls are resides on second step of wizard. will that make any impact ?
Edited:
I think wizard control making here matter. As i started my firebug and review the generated html it assign the Id dynamically to those controls which are inside the wizard. thats why javascript unable to find the expected control .
eg for txtAmount text box which is inside the wizard control getting name as :
ctl00_ContentPlaceHolder1_Wizard1_txtAmount
but certainly i would not prefer to use this generated Id. So is there any remedy to find control inside the wizard control and get - set values ?
get id of the control as shown below
var enteredAmount = document.getElementById('<%=txtAmount.ClientId%>').value;
It's impossible to say for certain with your not having quoted your HTML (!), but the usual reason for this is confusion between the id and name attributes. document.getElementById works with the id attribute, but people tend to think it works with the name on input fields, which it doesn't (except for on IE, where getElementById is broken).
(The other thing to remember is that id values must be unique on the entire page, but looking at the IDs you quoted, I suspect you're okay on that front.)
Update: It works if you use ids:
HTML:
<form>
<input type='hidden' id='hdnSelectedProductPrice' value='10'>
<input type='text' id='txtNumberOfLicense' value='2'>
<input type='text' id='txtAmount' value='3'>
<br><input type='button' id='theButton' value='Click Me'>
</form>
Live copy
As T.J. mentioned we really need to see your html code, without seeing it it could be that you are looking for an elements attributes.
So lookup the element as you are already with
var element = document.getElementById('product');
Once you have the element you can query its attributes
var price = element.getAttribute('price');
If its "ASP.net server control" then you will have to do it like this:
var originalPrice = document.getElementById('<%=hdnSelectedProductPrice.ClientID %>').value;
if you use Asp.Net 4.0 and your textbox is unique on the entirepage you can add ClientIDMode="Static" in attribute of your textbox
<asp:TextBox ID="txtAmount" runat="server" Width="250px" ClientIDMode="Static"></asp:TextBox>

Show and hide labels with assosicatedControls and their controls

I want to show and hide a label and its control. I can do this in c# in the code behind. But, I can only show/hide the control. Any ideas?
<asp:label AssociatedControlID="thisLabel" runat="server">This:
<asp:label ID="thisLabel" CssClass="ascontrol" runat="server" />
</asp:label>
I want to be able to show and hide that whole thing depending on what user gets to the page. I just need to know how to show/ hide that whole thing in the c# code behind...cannot seem to get the visibility of the wrapper label to go away.
You haven't supplied a server-side Id:
<asp:Label ID="label_MyControl" AssociatedControlID="txt_MyControl" runat="server" />
<asp:TextBox ID="txt_MyControl" runat="server" />
What you've done is nest a asp:Label control within another asp:Label control....
Since I normally hide more than one field contiguously, I tend to wrap the whole thing in an asp:Panel and hide the panel. However, that's just my particular usage. But since it's my usage, I tend to block those sorts of things out into panels even for something as simple as your example.
Just my nickel's worth, your mileage may vary, as always.
It should work if you get you r markup correct, like this:
<asp:Label ID="lblYear" runat="server" Text="Year (yyyy):"
AssociatedControlID="txtYear"></asp:Label>
<asp:TextBox ID="txtYear" runat="server" Columns="30" MaxLength="4"></asp:TextBox>
Then in the code behind you could have:
lblYear.visible = False
txtYear.Visible = False
Now, my understanding of the "AssociatedControlID" property of an asp:label is mainly for accessibility purposes. You don't need to have the AssociatedControlID value set to make things work as I've shown.

How to execute in JavaScript function in ASP.NET using C#

i have a textarea which i want to disable when I check a checkbox. I made a working demo in plain HTML and JavaScript , but When I am migrating to Asp.net, neither the function is being executed nor I can keep the OnSelect event in my check box.
<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" AutoPostBack="True" OnCheckedChanged="ChkOthers_CheckedChanged" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />
i want to create client side validation when check box is checked textbox is enable else disable.
I guess you problem is in the id of the controls so here is my solution for your problem
//Remove the AutoPostBack property and the OnCheckedChanged="ChkOthers_CheckedChanged"
<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" OnClick="javascript:YourFunction()" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />
and put the following javascript to handle the enable and diable of the textbox
<script>
function YourFunction() {
document.getElementById('<%= TxtOtherReason.ClientID %>').disabled = document.getElementById('<%= ChkOthers.ClientID %>').checked == false ? true : false;
}
</script>
It has been a while for me since I used ASP.NET but from memory there is something like an "OnClientClick" event and a validation property in asp.net properties for a control that you can use to call client side JavaScript code..
Sorry for the vague answer - perhaps someone can expand on it.
You are using a server side handling of the event. Use a client side handling with "OnClientClick". You can do something like:
<asp:CheckBox ID="ChkOthers" runat="server"
Text="Others(Please Explain)"
Width="205px"
AutoPostBack="True"
OnClientClick="alert(this.checked);"
OnCheckedChanged="ChkOthers_CheckedChanged" />
It seems this also does not work. I am yet to prove.
Also see a similar post on StackOverflow: OnClick vs OnClientClick for an asp:CheckBox? and there is also an issue in MS site: http://connect.microsoft.com/VisualStudio/feedback/details/103398/checkbox-onclientclick
you can put the code in cs files .after it loaded you can make the onclientclick method to an exist javascript function .
and in the javascript function you can get the text box use the name or the simple id which you can get from the source file the server gives to the browser!

Categories

Resources