How to maintain a value assigned to a label through javascript after postback?
Problem: I have assigned a label value using a clientside function.
But whenever postback happened, label values are gone.
Solution i found: After searching a lot, all are suggessting to store and retrieve
the value to & from a hidden field.
Note: But i want to achieve this without using hidden field as it may
increase pageload time.
The state of label is not maintained in ViewState by asp.net. The labels are converted in to spans and the html of span is not posted on submitting form, this is why changes made by client are not persisted. You can put the state of label in some hidden field when you change it in javascript and access it on server.
HTML
<input id="hdnLabelState" type="hidden" runat="server" >
Javascript
document.getElementById('<%= hdnLabelState.ClientID %>').value = "changed value of span";
Server side (code behind)
string changedLabelValue = hdnLabelState.Value;
use html hidden field
<input type="hidden" runat="server" id="hiddenlabel">
try an html label, like
<label id="lbl" runat="server">Myv alue</label>
Related
I have an input tag having some text in it. Now I would like that onclick of a button the text will be changed.
For some reason it is not being changed.
This is the input tag:
<input id="network_table" type="text" value="oldValue" runat="server"/>
the following is the way I am trying to change the value of the input tag:
network_table.Value = "newValue";
network_table.Text = "newValue";
Bind the "onclick" event and apply this these methods:
In jQuery :
$('#network_table').val("your val");
http://docs.jquery.com/Val
Javascript
document.getElementById('network_table').value = "your val";
you can do it serverside with "OnClick" event on button, assuming your controls are defined with runat="server" attribute
http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.button.onclick(v=VS.80).aspx
You could try and assign some meaningless class to the input and use that as a reference point to get in hold of the input field.
<input id="network_table" type="text" value="oldValue" runat="server" class="myInputField"/>
$('.myInputField').val('newValue');
Using the id will not work because you are using the 'runat=server' and it makes the id unavailable on client side and you would need to get the unique id first to be able to get in hold of it. This is a lot cleaner way but you need to make sure not to use the class elsewhere to avoid ambiguous results.
Here is a jsfiddle example which does what you want but on load.
http://jsfiddle.net/yX5ze/
I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this?
Javascript:
document.getElementById('hdntxtbxTaksit').value = "";
HTML:
<asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false"> </asp:HiddenField>
error : "Unable to get value of the property \'value\': object is null or undefined"
Prior to ASP.Net 4.0
ClientID
Get the client id generated in the page that uses Master page. As Master page is UserControl type, It will have its own Id and it treats the page as Child control and generates a different id with prefix like ctrl_.
This can be resolved by using <%= ControlName.ClientID %> in a page and can be assigned to any string or a javascript variables that can be referred later.
var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');
Asp.net server control id will be vary if you use Master page.
ASP.Net 4.0 +
ClientIDMode Property
Use this property to control how you want to generate the ID for you. For your case setting ClientIDMode="static" in page level will resolve the problem. The same thing can be applied at control level as well.
asp:HiddenField as:
<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />
js code:
$("#hfProduct").val("test")
and the code behind:
hfProduct.Value.ToString();
First you need to create the Hidden Field properly
<asp:HiddenField ID="hdntxtbxTaksit" runat="server"></asp:HiddenField>
Then you need to set value to the hidden field
If you aren't using Jquery you should use it:
document.getElementById("<%= hdntxtbxTaksit.ClientID %>").value = "test";
If you are using Jquery, this is how it should be:
$("#<%= hdntxtbxTaksit.ClientID %>").val("test");
document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value
The id you set in server is the server id which is different from client id.
try this code:
$('hdntxtbxTaksit').val('test');
I suspect you need to use ClientID rather than the literal ID string in your JavaScript code, since you've marked the field as runat="server".
E.g., if your JavaScript code is in an aspx file (not a separate JavaScript file):
var val = document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value;
If it's in a separate JavaScript file that isn't rendered by the ASP.Net stuff, you'll have to find it another way, such as by class.
My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response. My suggestion to solve your problem is
Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without runat=server. You can always controls the visiblity of that div using css.
If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :
placeholderId.Attributes["style"] = "display:none";
Anyway, as other have stated, your problems occurs because once you set control.visible = false, it doesn't get rendered in the client response.
I will suggest you to use ClientID of HiddenField. first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. as:
.cs file code:
ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);
and then use following code in JS:
document.getElementById(hdntxtbxTaksit).value= "";
Try setting Javascript value as in document.getElementByName('hdntxtbxTaksit').value = '0';
How can I recover a Css class that was add via JQuery in my <asp:TextBox> component ?
Example:
ASPX
<asp:TextBox ID='txtTest' runat='server' CssClass='inputText'></asp:TextBox>
JQUERY
$('#txtTest').addClass('testClass');
Page Renderized
<input type='text' ID='txtTest' CssClass='inputText testClass' />
Code Behind
How can I recover the testClass that was add via Jquery in my <asp:TextBox> component ?
I tryied this.txtTest.CssClass but return just inputText class.
You won't be able to retrieve this because changes to style aren't even submitted in the request. ASP.NET will reconstruct the object from what "it knows" about it, that is, the original markup in this case.
If you must keep track of this, then you are going to have to add the new class to a hidden element and retrieve it on code-behind:
<input type="hidden" id="addedClasses" name="addedClasses" />
Then the jQuery part:
$('#txtTest').addClass('testClass');
$('#addedClasses').val('testClass');
And on code behind:
string addedClasses = Request.Params["addedClasses"];
The only way I can think to do this is to put the TextBox's class into a Hidden field with javascript and have it be sent back to the server on a POST.
How can I retain client side html controls on postback? I have tried setting enableviewstate="true" but that did not work. A workaround I've done was to build a server side function that takes all the posted values and resets them via ClientScript.RegisterStartupScript and call this on every postback method. Is there an easier and more time efficient way of doing this?
You have have html control to keep their values on postback by making them runat="server" e.g.
<input type="text" id="txt1" runat="server" />
You need to create the controls at every postback. If you're looking for something a little easier to implement, take a look at the DynamicControlsPlaceholder control. It's a nifty little control that takes away most of the pain associated with persisting dynamic content.
Can you use HiddenField?
Now on clicking any button at client side, Preserve the data in HiddenField.
Use JQuery document.ready function to set the value again from HiddenField again. JQuery docuemnt.ready will be called on each Postback.
I am using Page.ClientScript.RegisterHiddenField("hf_Name",value) in an ASP.net application, how to override or assign a new value to the same Hidden Field 'hf_Name' in code behind?
RegisterHiddenField doesn't create a server side control, it just
creates a plain-old <input type="hidden" name="myhiddenField">
Page.FindControl("myhiddenField") will never find anything on serverside and even document.getElementById("myhiddenField") will return nothing on clientside since only the name and not the id is assigned.
So if you need to access it on serverside, you should use a HiddenField server control or at least use a html-input type=hidden with runat="server".