Insert/Update on single button - asp.net

I have modalpopup with textboxes in it.
Before the user gets to the modal popup, he either clicks ADD or EDIT.
If he clicks ADD, all the textboxes are empty and he gets to add a new address.
If he clicks EDIT, the textboxes already contain his address and he gets to edit his address. Now I have a single SAVE button on which I have to decide whether the data in the textboxes needs to be INSERTED (ADDED) or UPDATED (EDITED). How do I achieve this? I mean how do I find out if the user has clicked on ADD or EDIT??

keep the action in the viestate and check what value it is.
If user click edit then on editclick
ViewStat["Action"]="Edit";
On save button check for the view state value
if(ViewStat["Action"]!=null)
{
if(ViewStat["Action"].ToString()=="Edit")
{
//update statement
}
else
{
//insert statement
}
}
Also you can also set it to empty or null on cancel click
ViewState["Action"]=null;

Take an hidden field in your page. On add button click set its value to "0" and on edit button click set value to the id of the record that has to be updated. You can check value of your hidden field on server side to decide what to do.

Related

View data save -without any button click

I have a view which has some data entered by user. Once user moves out of this View, data in it should get automatically saved. We don't have any save button to get any click event. Please advise, how to capture View event once we dont have any save button
Thanks
There's a saveonshow attribute that you can set to true to automatically save when you leave that view and return to the previous view.

check any data in the webform changed or not asp.net

I have a webform with a lot of controls in it mainly Textbox and dropdowns. When the user clicks on the Update button I am supposed to update the values in Database. But I don't want to run the Update code on every update button click. I just want to do it if the user have made any change in the data inside any of my control. Is there any way to understand theuser have have changed the data or not in the server side?
Create a hidden field with value=0 and set a class name for all controls then using .Change function in JQuery and every time a change value occur in any control, set the value of hidden control to 1.
Then in codebehind check the hidden field value on every submit and if the value is 1, run your update code.
$(".ClassNameForAllInputs").change(function() {
$("#<% = HiddenFieldID.ClientID %>").val(1);
});

Required Field Validator gets clear on postback

I have a clear button and save button ,when i press clear button the required field icon gets clear as the button gets postback,i dont want this to happen i want to show the icon even after clear button is clicked.
You have to specify validation group attribute name for the save button, and set this name on required field validation control
ValidationGroup="validation_name"

Populate jQuery modal dialog form

I have a application that contains a gridview. When a user selects one of the items from the gridview, I want to be able to show a jQuery dialog box that contains a form with some pre-filled information from the selected item from the gridview. Right now I have a div with some html text boxes that is set to be a modal dialog box. The user clicks on it, and the selected item's ID is easily to retrieve. The problem comes when I want to go out to the database, get some of the details, then pre-fill in some of the textboxes and other elements before displaying the dialog box to the user.
An easy comparison is having a user click on an item so that they can submit a form to be processed with some of the items from the selection already filled in. I was wondering what the easiest way to go about this is. Right now I have it so that selecting an item from the gridview uses jQuery to do a Ajax post with the ID to a [WebMethod] in the code behind where it can then go off the database and get all the details of the record. The problem is that for obvious reasons the WebMethod can't magically fill in the fields of the form. I could send back all of the information to the ajax query to fill in, but that also feels pretty messy because there is no real strong connection from the random data I send back and which field they belong to.
A lot of this is pondering out loud, but I am really interested in better understanding how to use jQuery and Ajax within ASP.NET.
Thank you for your help and your input.
add an attribute on the gridview button:
btn.Attributes.Add("onclick","ShowDialog();");
client script
function ShowDialog(){
//setup the dialog
$('#dialog).setup(function(){
......,
buttons: "OK", function(){
//save by calling btn2.click() where btn2 is a hidden button that handles save logic in server side
});
});
$('#dialog').show();
return false;
});

DropDownList Setting Default Value

How should I get a value displayed in the DropDownList when redirected through a cancel buttton. The value in the page where cancel button is there in a TextBox should be caught in the DropDownList.
This DropDownList has SelectedIndexChanged event also fired for which on selection of a country in the list we get a ListView displayed in the same page. In that ListView we have an add button which will redirect us to another page called addcountry in which we have few controls. In those controls one TextBox I am getting value through QueryString in an enabled False state. Now again I need that value displayed in the DropDownList when I click cancel button.
How can I solve this problem?
Using $_POST and $_GET Variables you should be able to retreive almost any value from a form and put it in the right place on a new page, this right place can be one of your drop down list element...
To make absolutly sure your Cancel Button will send the good value to the right page put it in an independant form that has your new page (where you have yout dropdown list) as its action, and in this seperate form put an hidden field that holds the specific value you wanna see in this dd list ...
Hope I am clear but starting with your very "fuzzy" and blur question It is hard to help you.

Resources