Dropdown list onchange function - update value - c#

I have a Y/N dropdown list that I would like to update from the view (instead of updating it on the Edit page). For example, if I select "Yes" I will be redirected to the index page which will display "Yes". If I change it to "No" it will show "No". The result will be saved to the database. Basically, I want to call the edit method for the Y/N property on my index page.
What is the best way to go about this?

Here is one way to do it :
Wrap the dropdown list in a form that will POST to the server.
Not necessarily the Edit action... it could be some more specific UpdateYesNo action.
Listen for change events on the dropdown list.
Submit the form programmatically from within the change event.
Handle the request and redirect the client to the appropriate page.
Since this is a full POST request and not an $.ajax call we let AspNet Mvc handle the redirection.

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.

Validate data when textbox loses focus

In my ASP.Net web application, there is a textbox to receive ProductId. When the user enters data into the ProductId textbox and moves to the next textbox, I want to validate the data against a table and if matching record is found, I want to display Product details in different controls (readonly and normal textboxes) else I want to display a message about "New Product" and continue. I do not want to do the verification AFTER the user enters all data and press the Save button, but it should be done immediately. I was hoping that the Web Form textbox would have a LostFocus event, but do not know how to handle the requirement.
ASP.NET Validators don't have an ajax style immediate check.
Attach a custom validator to your textbox that hooks up to a validation method server side.
Using javascript, you'll need to hook up an eventhandler to the focusout event and make an ajax call. That call will send the value back to the server and get a response of valid or not.
This site : http://brian.dobberteen.com/code/jquery_ajax_custom_validator/ will help you with a lot of it, but one thing is this will fire only when the page is submitted. You'll want to hook up an additional event handler to your textbox to call the validation method on focusout
It's easy solution, read this article:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.servervalidate.aspx
You just need to preform javascript call to trigger server validation, use event onlostfocus.

To provide confirmation message in web page only if any change is done on the page

I want to show a confirmation message on click of cancel button and then redirect to another page. But the confirmation message should come only if any change has been done on the page.
Please can anyone help on this. Is it possible to achieve this at client side or at server side without comparing data ?
Thanks in advance.
I'm guessing you could have a scenario where:
A form is populated with values and each form item that you are tracking for changes also has a corresponding hidden field.
If the user clicks submit = great, but if the user clicks cancel you want to loop through each form item, comparing it to it's original value stored in the hidden field. If a change is detected you want to show a pop-up window informing the user that if they cancel their changes will lost - or something like that.
You can do all this on the client. JQuery, or Knockout (which I would use) or Backbone (I don't know this one tbh) should all provide a neat way to achieve this.
If you can't have hidden fields you might want to send the new values to the server in an Ajax post, get the server to pull the original values, compare and send back an appropriate response to the client. I'm sure there are other ways too*.
edit: like pop the original values in an array via Javascript, then compare them. Anyway, I'm sure you get the idea :)
This could be accomplished using jquery you would need to compare data since you want the message to come up when the cancel button is clicked.
jquery reference

How can I access the value of a checkbox without a postback?

I have a radgrid controlling access to an application for users. There are two columns, a 'name' column and a checkbox 'access' column representing their current access permission. the checkboxes are populated from a database. I would like the to change the checkboxes to grant or deny access to the users I specify. Since there are alot of users I would like to make multiple changes and use a submit button to write the changes to the database. I do not want to postback with each checkbox change and wait for the page to "blink". My problem is that with the checkbox postback disabled, when I click submit, the value of the checkboxes are not registering the changes I make. I need a way to access the current client-side state of the checkbox or some workaround to accomplish this.
Thanks in advance!
You could use javascript to send an ajax request that saves the change to the database without reloading the page. Then your updates happen right away and there's no "blink".
Thank you for the support. It turns out that the issue wasnt with client/server-side visibility, but with the page lifecycle. I was refreshing the radgrid in the PageLoad method and checking the value of the checkboxes in the Submit method. I didn't realize PageLoad was executed prior to Submit, thus clearing my selections. By removing the RadGrid refresh from PageLoad, the current values of the checkboxs were preserved.
If you set AutoPostBack to false, that will disable the postback, but they should still be posting their current values.
Sounds to me more like an issue with the dynamic generation of the checkboxes.
Not sure why you are not able to access the values of checkbox at server side, but to answer your question "I need a way to access the current client-side state of the checkbox or some workaround to accomplish this"...
You can use server tags and read the values of checkboxes at client side.
For more information about server tags, read this MSDN link.

AJAX CascadingDropdown - Setting the selected index

I have a CascadingDropDown on an ASP.NET page.
Now, the prompt text is "Select State". (list of states).
However, on a different version of this page (ie querystring), i might want to set the selected index to "California" for example.
How can i do this?
The web service used by the ajax control (ie GetStates) gets invoked at the same time the jquery document.ready function is triggered (ie asynchronously).
So when i try and set the selected index in jquery, the items are not yet bound.
Is there a way to attach a handler to the ajax dropdown so that i can set the selected index once the webservice call has completed, and the items are bound?
So instead of trying to get tricky, i ended up replacing the CDDL with regular client-side dropdownlists and extending them with jQuery/AJAX.

Categories