1

I have asked this question many time at different intervals at different forums but nothing helped me out but in hope I am posting it again. Please help.

I have applied JqueryMasking to a textbox i.e. NIC masking. It works when Page loads initially but stop working when postback occurs i.e. no mask is applied then. Why ?

jQuery(function ($) {

     $("#txtBoxComplainantCNIC").mask("99999-9999999-9", { placeholder: "" });
        });

textbox:

 <asp:TextBox ID="txtBoxComplainantCNIC" runat="server" onkeypress="return isNumberKeyCNIC(this)" ClientIDMode="Static" placeholder="XXXXX-XXXXXXX-X" MaxLength="15" CssClass="form-control"></asp:TextBox>

Textbox resides in UpdatePanel.

Stacky Flowy
  • 103
  • 1
  • 10

2 Answers2

2

It looks like your problem is that you have registered a function with JQuery to execute when the page loads. That is what

jQuery(function ($) {
  $("#txtBoxComplainantCNIC").mask("99999-9999999-9", { placeholder: "" });
});

does. However that means that code will only execute when the page loads. Your problem is that in an UpdatePanel, your postback happens asynchronously (no page load), so your function doesn't fire a 2nd time and your TextBox loses it's mask.

What you probably want to do is register this part of your script

$("#txtBoxComplainantCNIC").mask("99999-9999999-9", { placeholder: "" });

in code behind so that ASP.NET knows to execute it when your UpdatePanel posts back. It will look something like this:

void Page_Load(object sender, EventArgs e)
{
    var setMaskScript =   "$('#txtBoxComplainantCNIC').mask('99999-9999999-9', { placeholder: '' });";
    ScriptManager.RegisterStartupScript(updatePanelID, this.GetType(), "SetMask", setMaskScript, true);
}

where updatePanelID is the ID of your UpdatePanel.

Here is a link that explains RegisterStartupScript and it's interaction with UpdatePanel.

Additionally, several methods for executing code on UpdatePanel postback are discussed here. One of which is simply adding a pageLoad function to your client side code.

pageLoad(){
  $("#txtBoxComplainantCNIC").mask("99999-9999999-9", { placeholder: "" });
}
Community
  • 1
  • 1
Bert
  • 80,741
  • 17
  • 199
  • 164
  • let me try sir and will let u know – Stacky Flowy Jan 11 '16 at 20:53
  • @StackyFlowy Forgive me for asking possibly dumb questions, but do you know what code behind is and how to modify it? Specifically do you know where your Page_Load function is (if you have one already). – Bert Jan 11 '16 at 20:57
  • @StackyFlowy I added an additional link that might help. I have to go for now. Good luck. – Bert Jan 11 '16 at 21:19
0

On the control that is posting back the page use the below code:

ScriptManager.RegisterStartupScript(Page, Page.GetType, "temp", "<script type='text/javascript'>$('#txtBoxComplainantCNIC').mask('99999-9999999-9');</script>", False)
  • What is this? People can barely tell what this is about. Please edit your answer so that people can understand what you are trying explain. And elaborate the answer please so that people know why the answer will work. – UmarZaii Jul 28 '17 at 21:04