jQuery - Lose labels text on Postback - c#

I'm filling some labels using jQuery AJAX and it's working properly (i'm getting the values through webservices). But after that, i'm firing and Button click to get that values but in debugging the labels are empty.
I think it's because the values are losing in PostBack.
Is there anyway to keep those labels values on Postback?

Since the labels are just text in the HTML, there is no data being sent back in the Post that tells ASP.NET that the labels have changed.
You may need to include a Hidden field to track the text of the labels.
That being said (and since you are already using jQuery), I would recommend using a call-back rather than a post-back for this particular scenario.

Related

preserve UI state of asp.net controls after button click

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.

Yet another ASP:Textbox vs <input type="text">

I have read through some articles on this topic but I am still cautious about this. I am all along using ASP:Textbox but I would like to know what are the things an input textbox cannot possibly perform without using a ASP:Textbox or takes much more effort to pull off?
I have a Jquery tooltip sample which uses HTML input textbox and I am not sure if I should change all my ASP:Textboxes to HTML textboxes, the things which I need to perform on this textboxes are RequiredFieldValidation as well as storing their values into the database.
Anyone can advice me on this rookie question. Thanks.
The asp.net textbox IS a html input box from jQuerys point of view. Everything special about it is done on the server (including viewstate validation). The question you need to ask, is what value does giving it a server side reference bring to your app? Generally the answer is easy server side reference, but does that apply in your case?
As far as i know there is no visible server side programming difference between a HTML input box and an ASP Textbox. ASP.NET Validators would work perfectly both ways.
As for your tooltip concern. Jquery doesn't care if your using an ASP Textbox since your ASP textbox will end up as an HTML textbox anyways.
And for the advice - i don't recommend you changing all your ASP Textbox to HTML input boxes, its just a waste of time. You should use the CSSClass / class property to display the tooltip instead. That way it would work on your regular HTML input box and ASP Textboxes + other page elements.

I need to change Multiview or place holder whenever I select different values in dropdown list

I am creating online booking page.
I got some example page you can see in this link
http://www.parkercorporate.co.uk/webbookercc/OneForm.asp
I need the same behaviour in my site whenever user selects value in pickup dropdown list in the right side I need to change the text values or view.
To do this behaviour how should I write code in ASP.NET and c#.
Thanks
Ranam
First, use an UpdatePanel so you don't see any visual postback.
Second, set the AutoPostBack property on your DropDownList to True.
Third, handle the DropDownList's SelectedIndexChanged event. This will now fire after every change in the DropDownList, because you enabled AutoPostBack.
In the event handler, you can now change the ActiveViewIndex of the MultiView, or show/hide controls, change textboxes, etc.
First, create different blocks (i.e divs) and give ID's to them.
Then, for each value in the dropbox, fill the divs w.r.t it.
Assign "OnChange" event to the dropdown, and in the handler, set visible or invisible to corresponding block.
On the other hand, If you want to do this on client side, I strongly suggest you to use JQuery, to prevent a lot of requests going to server.
Check this out with JQuery:
make visible div on dropdown select in jquery
If you won't fetch values from database, there is nothing complex about this page. Because it just writes hospital if you choose hospital etc. and changes control's visible state.
Even if you think you need db queries when dropdown's selected change event, actually at this page there is no need. Just store db values in hidden controls (like Hospital- for Hospital) and when dropdown selection changes, write hidden's value completely in javascript

Validating textbox content on blur by calling server-side method without affecting page behaviour

I have a textbox in one grid-view column where upon entering a particular value and losing focus of the textbox, should post to the server to get the text validated through a server-side method. If the entry is valid, a result set to fill rest of row cells would be returned, else the bgcolor of the textbox needs to be changed to Red.
I tried posting back through the obvious way, i.e. making the textbox's autopostback as true and wiring up a server-side OnTextChanged event handler to validate the entered value.
It is working with this setup, but is also affecting the remaining page controls behaviour. For example, if I click a button in some other grid after entering some text in the textbox, the OnTextChanged handler gets called thus preventing the button's click event, which I also wish to call to execute its functionality.
Kindly suggest what alternatives/corrections I should carry out to enable textbox content server-side validation plus making the other controls/updatepanels work as expected.
Me dumb. I tried everything from creating PageMethods, UpdatePanels to jQuery as hinted in lincolnk's reply. But the thing which finally worked was removing the Autopostback attribute from the textbox control.
After removing it the OnTextChanged event executed each time any server postback was initiated after changing the text. Thereby, executing both the OnTextChanged method and the other control's method. :)
I can think of a couple general approaches.
Create a web service with your validation routine and manually make the call (jQuery or whatever) when the text changes. Manually update the client display when you get a result.
Convert your gridview column to a templated field. Add a CustomValidator and wrap the textbox and validator in an UpdatePanel. Set the textbox to auto-postback and the UpdatePanel to conditional update so only the one you are using is refreshed.
Option 1 is kind of an end-around the typical asp.net process, and you would still want to validate everything on the server-side when the page is posted back.
Option 2 might have performance issues, since you're hitting the page again every time you do a validation.

ImageUrl lost on postback with dynamic controls, but textboxes keep their value

I have a button that adds an already defined usercontrol programtically. I am rebuilding each control on Postback and they show up fine. If I put text in the textbox it shows up fine, however my images are losing their url. Any idea why or how I can fix this?
I tried adding AJAX updatepanel to see if that would help, but it does not.
To further explain - I have a button that after clicks set the url of the image - I also put this value in the textbox just to see if the same thing happens - after postback, my textbox still has the value, but the image does not - and all of my other ASP.Net images lose their image and they are defined right on the controls (ie: ImageUrl)
--- update
OK, I have found some more insight to my issue - some solutions but still one problem remains.
The reason the images were loosing their url is because they were and not ASP.Net images - ASP.Net remembers the values when they are recreated on postback - however the Main image that im changing via javascript looses its value - I'm very certain this is because of using javascript to change it, on post back it reverts back to the previous value... so for a solution I will try to stuff it in a hidden value, then use that value on postback to define the ImageUrl of the image...
You are correct <img> is an html control, while <asp:Image> is a server control. As long as server controls have viewstate enabled they will keep their values during postbacks.
You are correct that changing an image's URL via JavaScript will not get it returned during the postback as this is not a form value. Therefore, it will be lost forever if you do not save it into a form field, such as the hidden field that you suggested.
Image is not form field. That is why it's value is never posted back to server.
The reason your asp:Image controls retain value during postback is because they are stored in viewstate (which is stored in hidden field). When you click button this viewstate is posted back to server, and asp.net sets the ImageUrl property of image control from viewstate in early events of page lifecycle.
In short the server side form controls values are available after postback. Same way if you have normal html form fields (https://www.w3schools.com/html/html_form_elements.asp), you will get it's value using Request.Form collection.
You are setting your Image's src using javascript (and which is obviously not in original viewstate), that is why you are not getting it's value in postback. You should use Hidden Field to set value using javascript, which can be available after postback.

Categories