I am using javascript in an asp.net page where I am also using html textarea to get the text from users.
I want to store that text in the asp:TextBox below and set the visibility of that textbox to "false"..
The problem arises as I use the hidden textbox to store the value , my javascript is not working, and as I set the visibility to "true" it starts working again. but i didn't want to show textbox..
I included the textbox as:
<asp:TextBox ID="txtboxhead" runat="server" Visible="false"></asp:TextBox>
and i use javascript as:
document.getElementById('txtareahead').readOnly = true;
text = document.getElementById('txtareahead').value;
document.getElementById('<%= txtboxhead.ClientID %>').value = text;
how this problem can be solved..
please help me out..
The reason your javascript can not access the textbox when the visibility is set to false is because it simply doesn't exist.
This is because the server is processing the request and because it is set to false it doesn't render it to the page. What you want to do is to change the style of the textbox so it is hidden.
Eg below,
<div style="display:none">
<asp:TextBox ID="txtboxhead" runat="server"></asp:TextBox>
</div>
This way your script will still run and the users will not be able to see the textbox.
HTH
Sounds like what you really want is an <asp:HiddenField> it won't display on the page, but you'll be able to change its contents using javascript.
Related
I know this might be really baby-question.. but it's confusing me. If I'm looking at a page(which is an SSRS- generated webpage, i.e ReportViewer) in Firebug, like so:
So I want to get the ID of the actual input-box there , and the label coming up says _value - does this mean I'm not getting the input-box ID? How would I obtain that(just the input text-box)?
thanks!
The ID for that input is MainContent_rptCompletes_ctl00_ctl03_txtValue (thanks to ASP.NET webforms).
You can retrieve the TextBox ID using TextBox.ClientID. If you want to ensure that the TextBox has the same ClientID as it's ID, then set the ClientIDMode to Static.
<asp:TextBox ID="BoxID" runat="server" ClientIDMode="Static">
</asp:TextBox>
I have following lable
<asp:Label ID="lblErrorMessage" runat="server" Visible="false" ForeColor="Red" CssClass="ControlWidth"></asp:Label>
Which is visible false. On button Save if there is any error on page I make it visible and show error msg.
On Cancel button I am clearing the text of this lable thorugh Javascript as
var lblErrorMessage = document.getElementById('<%=lblErrorMessage.ClientID%>');
lblErrorMessage.innerHTML = "";
lblErrorMessage.innerText = "";
But when I do some other opration on page that time page get postback and error message
Which I have cleared get visible.
Can anyone tell me how I can make label Visible false through javascript and which should
not get visible on postback.
If you want to avoid this problem altogether, have your cancel button do a postback and clear the label values and set its visibility to false again fron the server side as opposed to doing it from the client side. The reason this is happening is because even though you clear the label innerText and innerHtml with javascript; the ViewState still contains the old values so when you post back; it recovers those values.
If you think you can leave without ViewState being enabled for the wrror label, you could disable it and the problem will go away; for example:
<asp:label id="errorlabel" EnableViewState="false" />
In my ASP.NET applications, I have a server-side HTML select control.
<select id="CompanyDropDown" runat="server" style="width:330px">
</select>
I then have a link on my page that runs a JavaScript function that populates this control, and also selects the "current" item. I can see this is working because the list has all the items and the correct one is selected.
However, when the page posts back in response to a button click.
<asp:Button runat="server" ID="btnSaveCompany" Text="Save"
onclick="btnSaveCompany_Click" />
CompanyDropDown.Value is empty, and it contains no items. Any suggestions on how I can see the selected value on postback?
(Note: I tried using an ASP.NET DropDown control, but that fails postback validation when the control has items that were added after the page was rendered.)
Because items added to the list using JavaScript aren't part of the viewstate, they cannot be retrieved server side. If you are only trying to get the selected value, put a server-side hidden field on your page and set its value using JavaScript each time the selection of the drop-down is changed:
jQuery script:
$('#CompanyDropDown').change(function() {
$('#inpSelectValue').val($(this).val());
});
Hidden field markup:
<input runat="server" id="inpSelectValue" clientidmode="static" type="hidden" />
The value of the hidden field will be carried to the server on post back. Refer to it to retrieve the value of the selected item. Be sure to add clientidmode="static" to your drop down as well to keep the rendered id of your controls set to the id you assign in the ASPX page (i.e., CompanyDropDown rather than parentContainer_CompanyDropDown):
<select id="CompanyDropDown" runat="server" style="width:330px"
clientidmode="static" />
Alternatively, you could use AJAX to tell the server you are adding each dynamically generated list item or changing the selection. I would only do this if you needed to maintain a list of items in addition to which one is selected.
The values manipulated in JS can't be retrieved on the server-side and can't be retained on postback. You can put the SelectedValue in a hidden field. On page postback you have to set the SelectedValue again that you save in the hidden field.
You can use Request.Form[CompanyDropDown.UniqueID]. This will work without hidden field.
I have a set of server side combo boxes in a table. Based on client events, I need them to appear or disappear. I've tried the following with no success:
document.getElementById("cboToothNumber").style.visibility = "hidden";
$("#cboToothNumber").hide()
Any ideas? Also, this will need to work from a js file
You don't want the ASP.NET ID, you want the client id, so try
$("#<%=cboToothNumber.ClientID%>").hide()
From a JS file, this won't work. You can use the same code to get the ClientID and set it as a variable or pass it into a function in the Javascript file though.
Have you checked the ID of the combo boxes once the page has rendered? The Id of the element might be changing due to it being serverside.
If you are using .net 4 - there is a way to disable the ClientId by setting the ClientIdMode.
<asp:Label ID="Label1" runat="server" ClientIDMode="[Mode Type]" />
Check out these links:
http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
A required field validator seems to always to fire when the associated textbox is disabled (whether the textbox contains text or not).
When the textbox is enabled the validator behaves correctly.
Can anybody tell me why?
I've tried disabling the required field validator with ValidatorEnable but that seems to make no difference.
Here's the relevant HTML from the page (cut down):
<tr id="trBrokerNetID" runat="server">
<td>
<cc1:mitextbox id="txtBrokerNetID" runat="server" cssclass="bodytext" width="220px" maxlength="20" onBlur="JavaScript:CheckBrokerBranch(false);"></cc1:mitextbox>
<asp:requiredfieldvalidator id="rfvBrokerNetID" runat="server" width="1px" errormessage="BrokerNetID - Please supply a value" controltovalidate="txtBrokerNetID">*</asp:requiredfieldvalidator>
</td>
</tr>
Any ideas gratefully received.
Now what I didn't know was that when a control is disabled on the clientside it doesn't get included in the postback.
Which is why the serverside validation was firing. As far as it was concerned the control was empty.
The soolution is to use the readOnly property rather than the disabled property.
Now to figure out how to style the control to make it have the same look as if it was disabled.
ASP.NET's validators work in mysterious ways :D
First of all it is dangerous to use the id of an ASP.NET control to access it in jQuery.
If you place the control in a repeater or wrap the page in a master page, then the id of the html element will be something different than the id you specified. Use class to access the element instead.
If ASP.NET validators want the field to be enabled then you must try another approach.
My suggestion would be this:
1.
Add a class to the textbox, that makes it looks disabled:
$("#txtBrokerNetID").addClass("thisClassMakesItLookDisabled");
2.
Add an event that checks on focus to the textbox and blurs it if there is focus:
$("#txtBrokerNetID").focus(function() {
$(this).blur();
});
Now the field behaves as if it is disabled and the validator works.
One option you can choose, is to set a ValidationGroup than the one the form uses, an then, when validating the form call Page_ClientValidate('text_validation_group') if needed. That way client validation won't get in the way.