Highlight dot net form text box onfocus using jquery

Highlight asp.net text box when it gets focus. When your cursor will focus on text box it will be surrounded by a red rectangle when cursor leaves text it becomes normal. If coursor goes to  button or dropdown list they dont get any style the behave as normal. This code is ony tested in IE7. Do not forget to reference Jquery script in your document. you can get jquery script file from jquery.com

 

CSS Style

<style type=”text/css”>

        .required

        {

            border: solid 1px #ff0000;

            padding: 1px 1px 1px 1px;

        }

</style>

ASP.net Controls

    <div>

      <asp:Label ID=”Label1″ runat=”server”>First Name</asp:Label>

            <asp:TextBox ID=”txtFirstName” unat=”server”></asp:TextBox>

            <asp:Label ID=”Label2″ runat=”server”>Last Name</asp:Label>

            <asp:TextBox ID=”txtLastName” runat=”server”></asp:TextBox>

        </div>

        <asp:Button ID=”Button1″ runat=”server” Text=”Button” />

        <asp:DropDownList ID=”DropDownList1″ runat=”server”>

        <asp:ListItem >1</asp:ListItem>

        <asp:ListItem >1</asp:ListItem>

        <asp:ListItem >1</asp:ListItem>

       

        </asp:DropDownList>

Jquery Code

$(document).ready(function () {

        $(“:text”).focus(function (){

                $(this).addClass(“required”);

            });

               $(“:text”).blur(function () {

                $(this).removeClass(“required”);

               });

            });

OR

$(document).ready(function () {

        $(“input[type=text]“).focus(function (){

                $(this).addClass(“required”);

            });

           

            $(“input[type=text]“).blur(function () {

                $(this).removeClass(“required”);

               });

  });

Following Code give error in IE 7 I haven’t tested in any other browser.  Can any one tell me please why following code is giving error though this code is available on Internet:  $(“input[@type=text]“).

$(document).ready(function () {

        $(“input[@type=text]“).focus(function (){

                $(this).addClass(“required”);

            });

           

            $(“input[@type=text]“).blur(function () {

                $(this).removeClass(“required”);

               });       

 });

I Wan to share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • kick.ie
  • Live
  • MyShare
  • IndianPad
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb
  • DotNetKicks
  • DZone
  • TwitThis

How to populate asp.net dropdown list from database by passing radio button list’s selected value as parameter with JQuery?

How to populate asp.net drop down list from database by passing radio button list’s selected value as parameter with J Query, J Son?

In my project I am taking off asp.net update panel where ever possible and replacing it with J Query Ajax. J Query Ajax is far better then asp.net Ajax and asp.net update panel.  I am populating 259 countries list on the basis of parameter passed from asp.net radio button list’s selected text. Using asp.net radio button list and drop down list inside asp.net update panel keeping Radio button list’s auto post back property to true. First time running application from visual studio and clicking on the radio button list item populated drop down list in “176ms” and when chose same option second time it took “76ms” while same Ajax request using J query  took “79ms” for first time and  when chose same option second time it took “32ms”.

J Query code to decode j son result and populate asp.net drop down list

<script type=”text/javascript”>

function getCountries()

{

var elementId = ‘<%=rblUKInternational.ClientID%>’;

var elementRef = document.getElementById(elementId);

var checkBoxArray = elementRef.getElementsByTagName(‘input’);

var checkedValue =“”;

var isChecked = false;

for (var i = 0; i < checkBoxArray.length; i++) {

if (document.getElementById(elementId + “_” + i.toString()).checked == true) {

var checkBoxRef = checkBoxArray[i];

checkedValue=checkBoxRef.value;

Chekced = false;

break;

}

}

$.ajax({

type:“POST”,

url:“WebService/WsCountry.asmx/getCountryList”,

data:“{optionvalue:’”+checkedValue+“‘}”,

contentType:“application/json; charset=utf-8″,

dataType:“json”,

success:function(response)

{

if (response.length<=4)

{

alert (response);

}

else

{

var myObject= eval(“(“+ eval(response)+ “)”);

$(‘#<%=DropDownListCountry.ClientID %>>option’).remove();

$(“#<%=DropDownListCountry.ClientID %>”).append($(‘<option></option>’).val().html(“Please select…”));

for (var i=0;i<=myObject.length-1;i++) {

$(“#<%=DropDownListCountry.ClientID %>”).append($(‘<option></option>’).val(myObject[i].countrycode).html(myObject[i].name));

}

}

},

error: function (response)

{

if (response.length!=0)

alert(response.status + ‘ ‘ + response.statusText);

}

});

}</script>

Web service code to get data from SQL server 2005 and encode into J Son format

using System;

using System.Data;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.ComponentModel;

using System.Web.Script.Services;

using System.Web.Script.Serialization;

using System.Text;

using System.Collections.Generic;

{

/// <summary>

/// Summary description for WsCountry

/// </summary>

[ScriptService]

public class WsCountry : System.Web.Services.WebService

{

[WebMethod]

public string getCountryList(string optionvalue)

{

try

{

StringBuilder sbcountriesList = new StringBuilder();

if ((optionvalue != “GB”) &&

(optionvalue != “INT”))

throw new ApplicationException(string.Format(“Country code GB or INT expected, {0} received”, optionvalue));

CountryService cs = new CountryService();

TList<Country> countries = cs.GetAll(optionvalue);

List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();

foreach (Country country in countries)

{

Dictionary<string, string> Dictionary = new Dictionary<string, string>();

Dictionary.Add(“name”, country.DisplayName);

Dictionary.Add(“countrycode”, country.CountryCode);

rows.Add(Dictionary);

}

JavaScriptSerializer js = new JavaScriptSerializer();

js.Serialize(rows, sbcountriesList);

return sbcountriesList.ToString();

}

catch (Exception ex)

{

return ex.Message;

}

}

}

}

C# code to call JavaScript function on the radio button list selected item

this.rblUKInternational.Attributes.Add(“onclick”,“getCountries()”);

I Wan to share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • kick.ie
  • Live
  • MyShare
  • IndianPad
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb
  • DotNetKicks
  • DZone
  • TwitThis