logo

ASP.NET How to client side prompt to save data on textfield change without posting back

Overview: In this code sample I use a custom validator to call a client side javascript that sets a hidden field to the value "changed". When the parameters button is clicked, if the hiddenvalue is "changed" then client side javascript prompt to save the information, if the hiddenvalue is "changed". The server side parameters_click then sees if the user press "Ok" and the hiddenvalue is "saved".

<asp:hiddenfield Visible="true" ID="HiddenfieldSave" runat="server" value="Javascript Message"/>


cmdParameters.Attributes.Add("onclick", "javascript: if (document.getElementById('ctl00_ContentPlaceHolder1_HiddenfieldSave').value='Changed'){ if (confirm('Do you want to save?')==true){document.getElementById('ctl00_ContentPlaceHolder1_HiddenfieldSave').value='Saved';return true;} else{document.getElementById('ctl00_ContentPlaceHolder1_HiddenfieldSave').value='Not Saved'; return true;}};")


ClientScript.RegisterStartupScript( _
    Me.GetType(), _
    "myscript_validator", _
    "function CVValidator(source, args) {alert('reached');document.getElementById('ctl00_ContentPlaceHolder1_HiddenfieldSave').value='Changed'}", True)



    <asp:textbox  ID="txtProjMaterial" ForeColor="black" Font-Size="small" Width="80" CssClass="AlignRight"
                            runat="server" Text='<%# formatnumber(eval("projmat"),2) %>' />
                        <asp:Label ID="lblErrorProjMaterial" runat="server" ForeColor="Red"></asp:Label>
                        <asp:CustomValidator ID="CVProjMaterial" runat="server" ErrorMessage="" Text=""
    ValidationGroup="g2" EnableClientScript="true" ClientValidationFunction="CVValidator" ControlToValidate="txtProjMaterial" ValidateEmptyText="true"></asp:CustomValidator>


    Protected Sub Parameters_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        If HiddenfieldSave.Value = "Saved" Then
            SaveBudgets()
        End If
        lblSaveMessage.Text = HiddenfieldSave.Value

        Page.SetFocus(panelParameters)
    End Sub

s