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

Validation of web site address on server side using c-sharp and Regular expression

Validating web site address at server side in asp.net using c-sharp.

A detail web site address  validation method where Regular expression is being used for a baisc web addrss validation, then web address is being splitted by dot and forward slash to check if it contains http: and if its defined more than once in the web address return false as web addrss is not a vaild web address.

Then I am checking for the dot in web site address if there is no dot in the web site address then its an invalid web address. Next I am checking of first word is http or https if not then its an invalid web address, also there must be atleast two dots in the web address at last we need to check at end there must be at least 2 chracters.

public static bool ValidateWebSiteAddress(string webAdress)
{
bool isValid = true;
Regex weburl = new Regex(@”http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?”);
char[] sepCharacters ={ ‘.’, ‘/’ };
int iCounthttp = 0;
string[] words = webAdress.ToLower().Split(sepCharacters);

foreach (string word in words)
if (word.Equals(“http:”))
iCounthttp++;

if (iCounthttp > 1)
isValid = false;

//check against regular expression
if (!weburl.IsMatch(webAdress))
isValid = false;

//check dots are present
if (!webAdress.Contains(“.”))
isValid = false;
//check for http and https
if (words[0].Length > 6 || words[0].Length < 5)
isValid = false;
//check if there are 2 dots in the adress
if (CharCount(webAdress, “.”) < 2)
isValid = false;
if (words.Length <= 5)
{
if (String.IsNullOrEmpty(words[words.GetUpperBound(0)]))
isValid = false;
if (words[words.GetUpperBound(0)].Length < 2)
isValid = false;
}

return isValid;

}

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