Is there any way to get the original text from a Asp:Label on a page, after the text has been altered?
with the orginal text i mean the text that is hard coded into the asp.net markup.
There is no standard way to get it back after some chages, but you could do that in some your ways, for example add custom attributes to label
textLabel.Attributes.Add("data", textLabel.Text);
and then use it on your page.
Or cache label value using js code on page startup or statically.
Create a property that wraps the text assignment. Before the assignment take the current value and assign it to a hidden input or stick it in the session or viewstate.
Create a property that retrieves the previous value from the hidden input or session or viewstate.
Could get fancy and extend the label to add a PreviousValue property. Not sure how this would work in practice though.
Related
I am using jQuery and ASP.net
I have a User Control and wanted to Set the values of this User control to the database, for that I need the user control's value. In the same way, I want to show the data from the database in the user control, for that I need to Get the values by jquery.
In my user control I have 4 TextBoxes and 2 Buttons (SET/UPDATE)
AutoCompleteSearch_New is ascx user control
Here is my tried code:
var ID = $('#<%= ((HiddenField)AutoCompleteSearch_New.FindControl("hdnvalue")).ClientID %>').val();
But I dont wan't to use hidden fields.
Can I directly find the control's value without using hidden fields?
It is similar to use value from a webform.
Here is the code for the same.
Var TextBoxValue = $('#YourTextBoxID').val();
Inspect element and get the Textbox ID and replace it with YourTextBoxID.
Or
Var TextBoxValue = $('#<%= YourTextBoxID.ClientID').val();
where YourTextBoxID is your asp:Textbox ID.
When ever you load user control in your aspx page, jquery consider it as a whole page which is combined of user control and rest of the aspx controls exist in the form. So you can directly get textbox value in your jquery
you can do that by adding ClientIDMode="Static" to the Control
and then use
var txtvalue= $("#TextBoxId").val();
Provided storing the values in Session is not an option, other options I see are :
javascript variable declaration in the user control markup
client-side decryption of the view state
custom data attributes on html elements
I'm afraid HiddenField is your best option. That's somewhat the same idea behind ViewState.
If you don't want the client to tamper with the value, you may go for a combination of the value, and a hash of the value concatenated to a secret key ( value+separator+Hash(value+secretkey) )
If you don't want the client to access the value, you may rely on encrypting it in the HiddenField
hey u can directly find control of textbox or hiddenfield without mention its type
var value=$('AutoCompleteSearch_New_hdnvalue').val();
here AutoCompleteSearch_New is the name of user control u load on page and
hdnvalue is the id of hidden field in user control AutoCompleteSearch_New
enjoy.. :-)
I have an ASP.NET web app with a dropdown list and a number of ASP.NET textboxes/labels. when the user chooses a particular item from the dropdownlist the labels change text and some of them are hidden etc using jquery. However after clicking on a button the controls are being changed back as when the page is first loaded.
Please suggest
How i Can preserve the new labels name etc?
Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
Should i use hidden fields to store the value?
1: How i Can preserve the new labels name etc?
Yes,
You can use hidden field to store.
2: Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
No,
On page_load access hidden field, extract and use values.
3: Should i use hidden fields to store the value?
Yes,
You can use hidden field to store changes you made in GUI control using jQuery. Make that hidden field server accessible by making it runat server. On server end when you get postback access the hidden field, extract and assign values to respective controls. You can see how it would work in this post.
As you might be knowing , after clicking the button if it is submit type it will do the postback to the server , as HTTP is stateless , it is your responsbility to generate the label text and value again. Generally ASP.NET viewstate take care of doing so for input texts, but for labels you have to do by yourself. I can suggest the solution like this
1) Have a javascript method which will have the logic to change the value of label and hidden field based on the selected value of the dropdown
function MyFunction ( ddlSelectedValue)
{
if(ddlSelectedValue == "1")
{
$("#LABELID").html('YOUR VALUE');
// rest other logics
}
}
2) ASP.NET viewstate will help you to preserve the dropdown state after button click . So on add this jquery script on the page top
$(document).ready(function(){
var ddlSelectedValue = $("#DDLID").val();
MyFunction(ddlSelectedValue);
});
3) attach the the point 1 method to onchange of the dropdown also.
4) Using the hidden field is Ok , because hidden fields will serve your requirement in using in the server side as they will also be posted back.
I'm creating a web application using C#.net
My webpage contains a Hidden Field Control. I need to use this control's value in another page. I declared a method and inside this method i passed the entire page as a parameter. In another classfile i defined this method. Inside the method i tried to access the Controls that was in the page created. I tried but i can't get the control.
Code:
HiddenField hdnTotal = page.FindControl("hdnTotal") as HiddenField;
Is anyother option to find this control in other class file.
Thanks In Advance!
Instead of passing a page as parameter did you try using PreviousPage property to find a control? On the page where you want the control from previous page you could find hidden file like this:
HiddenField hf = (HiddenField)PreviousPage.FindControl("hdnTotal");
The normal way to get data from one page to another is to POST the first page to the second page in hidden/form fields.
Is there a reason you're trying to pass the entire control instead of having the control (for example) just set the value of one or more hidden form fields?
You have several options: to use cross-page posting or to pass the value of that hidden field as a GET parameter to another page when redirecting. Also you can save the value in session or in cookie and access it later.
Hy,
I have created some dynamic textboxes with standard content.
Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?
Thanks a lot.
Jeff
Update
This is how I am creating the textboxes:
foreach (string name in listOfNames)
{
TextBox tb = new TextBox();
tb.Text = name;
tb.BorderStyle = BorderStyle.None;
tb.BorderWidth = 0;
tb.Font.Name = "Arial";
tb.Font.Size = 8;
}
The specific will vary depending on the technology you are using. However the concept would remain very similar, though for ASP.NET it will be a little more interesting.
WinForms/WPF/Silverlight
Maintain a list of the dynamically created textboxes and when the button is pressed you can run through the list of textboxes and read the Text property to get the user input.
ASP.NET - After the tag update it seems this section is most appropriate to your requirement.
For ASP.NET you will need to create the textboxes in an override of the OnInit method, this should happen on each postback. Then in the Button.Click event you can read the user input from the textboxes that you created in the OnInit function. You need to ensure that the controls are created with the same ID on each post back.
You need to ensure that the text boxes are recreated on every postback.
If you do not recreate them, you will not be able to access their properties or events.
The best place to create dynamic controls is the page Init event handler.
I suggest reading up on the ASP.NET page life cycle.
Update (following updated question)
Make sure to set an ID (and a different one, at that) for the text boxes, so you can refer to them later on.
I can't see where you are adding these controls to the page either.
Request.Form is a collection of Key-Value pairs that represents all of the data coming back from the ASP.NET request. If you access that you can get any value whether its control is specified in the ASPX code or dynamically created in the code behind file.
You can also get their values placed back in them automatically if you recreate them during the init part of the page lifecycle. If you do this, their values will be set when ASP.NET recreates the state of the page and applies the values from the form.
You should be able to access them as you would a normal control, you just need to get a reference to the control.
Remember to re-create all controls on each postback.
this is a useful article on dynamic controls in asp.net
I have a label on a page and I'm updating the text property of the label (with a calculated value) when text is changed in a textbox.
I'm updating the label like so:
$myLabel.text("123");
The text is being displayed correctly on the screen butwhen I try to save the text value to an object in the code behind (When I press a button) the text property of the label is "" and not "123".
Code behind:
var myLabel = myLabel.Text;
//the var myLabel is "" when it should be "123"
Any ideas as to why this would be?
Thanks in advance,
Zaps
Not sure if this is the correct way to do it but I got around the problem by using a hidden field.
I updated the label text as above:
$myLabel.text("hello");
but then I updated the hidden field value:
$('#<%= hiddenField.ClientID %>').val("hello");
I was then able to use the hidden field in the code behind:
var myLabel = hiddenField.Value.ToString();
This seems to work fine.
Why don't you check the value that was entered in the textbox. based on your description, that should be the same and it will be available. Otherwise, I think you need to post some more code to clarify what you are doing.
The value of the label text needs to be stored in ViewState, otherwise it will be overwritten on the postback triggered by the button click.
One option would be to also change the value of a hidden control. Any changes to this value will be available in the code behind on postback.
<asp:Hidden id="hiddenLabel" runat="server" />
Html controls like labels, spans, divs don't post their values to the server - while inputs do. ASP.NET maintains changes in controls by using ViewState.
When you change the value of a server control, it's state is often persisted there. If you change the value on the client side via JavaScript, the ViewState isn't changed, and this is why on PostBack you get the original Empty value.
In what function are you putting var myLabel = myLabel.Text;?
It won't work in the init function -- you need to give the page time to load from the viewstate. Best in the button push event handler.
Update:
You need to use an form input control (eg TextBox) not label. Labels are readonly.