Critical Developers

Programmers Knowledge Base

Confirm OnTextChanged of TextBox with javascript(if Cancel Restore the previous value, if Ok Call Server Side Script) - ASP.Net

Lets the TextBox is in ListView or any MultiDimensional DataBound Control so that the TextBox have some default value first time its load.

TextBox HTMLization :- (Recommend to use UpdatePanel to ajaxify)

<asp:TextBox ID="txtProdAmount" runat="server" Width="80" Text='<%#Eval("product_amt", "{0:0.00}")%>' AutoPostBack="true"
onchange="if (!confirm('Are U Sure to Change the Amount  ?')) { this.value=this.defaultValue; return false; }" EnableViewState="true">

AutoPostBack="true"  // To Call the ServerSide || CodeBehind TextChanged Event.

ontextchanged="txtProdAmount_TextChanged"  // Call ServerSide TextChanged Event once confirm from client.

onchange=""   // Call ClientSide TextChanged Event


if (!confirm('Are U Sure to Change the Amount  ?'))  //  on Confirm if action is "Cancel"

{ this.value=this.defaultValue; return false; }   //  then restore its previous value in textbox

Code Behind:-

  protected void txtProdAmount_TextChanged(object sender, EventArgs e)
        TextBox txtProdAmount= (TextBox)sender;
        RepeaterItem rptItem = (RepeaterItem)txtProdAmount.NamingContainer;
        HiddenField hdnProdID = (HiddenField)rptItem.FindControl("hdnProdID");
        if (!string.IsNullOrEmpty(txtProdAmount.Text.Trim()) && txtProdAmount.Text.Trim() != "")
            int liSuccess = 0;
            decimal? ldProdID = null;
            decimal? ldProdAmount = null;
            ldProdID = Convert.ToDecimal(hdnProdID.Value);
            ldProdAmount = Convert.ToDecimal(txtProdAmount.Text);
            Product loProductObj = new Product();
            liSuccess = loProductObj.UpdateProductAmount(ldProdID , ldProdAmount ); // Call Update Procedure
            if (liSuccess > 0)
                txtProdAmount.Text = Convert.ToDecimal(txtProdAmount.Text).ToString("0.00");  // To re-Format the TextBox
                Common.ShowAlert("Product Amount Updated.", this); // Alert if Updated


===[ END ]=========