I have a page where I have four dropdown controls. Based on the selection, I show a grid of data with a text box on each row of the grid. I take input from user on each control and are saved to database upon clicking Save.
Problem:
I'm struggling to alert users when there is an unsaved change on the page and they try to change dropdown value, which results in showing different set of data on the grid.
I have a hidden field which captures when there is a change on the page
<asp:HiddenField ID="hfSaveFlag" runat="server" Value="false" ClientIDMode="Static" />
I have enabled textbox change function so every time there is a change, I enable this hidden field flag to true
.
<asp:TextBox ID="txtRating" runat="server" Text='<%# Eval("rating") %>' Width="25"
ValidationGroup="rating" onchange="setPendingSaveFlag()" />
function setPendingSaveFlag(ctrl) {
$("#hfSaveFlag").val('true');
}
This works fine so far and the value is set to true
upon change of value in text box.
Now, my dropdown list is like below:
<asp:DropDownList ID="ddlRFP" Width="250" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlRFP_SelectedIndexChanged" />
I added onchange event in code behind
ddlRFP.Attributes.Add("onchange", "verifyPendingSave(this);");
My javascript for this onchange
function verifyPendingSave(ctrl) {
if ($("#hfSaveFlag").val() == 'true') {
alert('You have pending changes on the page.');
return false;
} else {
__doPostback(ctrl, '');
return true;
}
}
My intention is if the value of the hidden flag is true, I show an alert, suppress postback and select whatever the value previously selected. Execute postback when it is false.
I see the alert but it is not stopping postback though I return false
.
My else part never does postback
. I see control id and the actual controls in this part but I guess since it is inside a gridview, it might not be able to find the even to postback
. I am sure somehow I am missing something very trivial.
else {
alert(ctrl.id);
var control = document.getElementById(ctrl.id);
__doPostback(control, '');
return true;
}