Qudir Ansari
Qudir Ansari

Reputation: 59

How To Fire An Event /Function From A Code Behind When I Type 6Th Digit In A Textbox?

How To Fire An Event /Function From A Code Behind When I Type 6Th Digit In A Textbox ?

My uestion is very simple I am a programmer of C# how to fire an event /function from a code behind when i type 6th digit in a textbox ??

Upvotes: 0

Views: 664

Answers (3)

Ali Umair
Ali Umair

Reputation: 1424

My solution may not be perfect but put your texbox in UpdatePanel double click it to create an event, now use int.Parse to see that if entered input is integer or not and use txtbx.Text.Length to see if you got the sixth digit or not.

it should be something like this

if(txtbx.Text.Trim().Length > 0 && int.Parse(txtbx.Text.Trim(), out intoutput))
{
    if(txtbx.Text.Trim().Length = 6 )
    {
    // your code here
    }
}

Upvotes: 0

milan m
milan m

Reputation: 2254

After reading your requirements from the comments in the question, I suggest you to use Web Service or Page method for your current problem.

Your problem can be solved in the following way:

  1. Use jQuery in your project.
  2. Write a javascript "onchange" function for your textbox.
  3. Calculate length of the text in textbox in this javascript function.
  4. When the length equals "6" then fire a web service request via jQuery.
  5. The web-service shall return data that you need to capture in your javascript.
  6. Display the data.

A sample code is as below:

Javascript function for calculating length:

      function caculateTextboxLenth() {
            if (document.getElementById("<Textbox Id>").length == 6) {
                var text = document.getElementById("<Textbox Id>").text;
                CallWebService("/getDistrictData",text);
            }
            return false;
        }

Javascript function for calling web page method:

function CallWebService(WebServiceURL,text) {
    $.ajax({
        type: "POST",
        cache: false,
        url: WebServiceURL + "&pincode=" text + "&dt=" + new Date().getTime(), //This will append ?dt={timestamp} to the URL to prevent caching,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: ShowData,
        error: error
    });
}

Your C# page method shall be like the following in your code behind file.

[System.Web.Services.WebMethod]
    public static string getDistrict()
    {
        int pincode = Convert.ToInt32(HttpContext.Current.Request.QueryString["pincode"].ToString().Trim());
        string district = "";
        //code to fetch district
        return district;
    }

P.S: Do check for SQL injections.

Upvotes: 1

Mandar Jogalekar
Mandar Jogalekar

Reputation: 3281

attach two events to textbox 1)Javascript onchange 2)c# OnTextChanged

check validations for characters in onchange and if true then "return true;" which will fire OnTextChanged event on server side.

Also do no forget to put "AutoPostBack" property of textbox to true.

Upvotes: 0

Related Questions