Reputation: 59
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
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
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:
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
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