Wednesday, July 6, 2011

To Check browser in asp.net application

//For Internet Explorer

Response.Browser.Browser.ToLower().Contains("ie");


or


Response.Write(Request.Browser.ActiveXControls)

//return true if it is IE else false.

Validating the Textbox using Javascript

// Not allowing string to be entered in textbox accept number


<script type="javascript">
function allownumber(e)
{
var key=window.event ? e.keycode:e.which;
var keychar=String.fromCharCode(key);
var reg=new RegExp("[0-9.]");
if(key=="")
{
keychar=String.fromCharCode(key);


}
if(key==13)
{
key=8;
keychar=String.fromCharCode(key);
}
return reg.test(keychar);
}


</script>


//Code Behind 


 on page_load


TextBox1.Attributes.Add("onkeypress","javascript:return allownumber(event);");


//It will not allow user to enter string accept number

To retrieve the value of dynamic generate Checkbox when it get checked in asp.net

//First Create the object of CheckBoxList
CheckBoxList chk=new CheckBoxList();
ck.Item.Add("cricket");
ck.Item.Add("hockey");
ck.Item.Add("badminton");
ck.Item.Add("table tennis");
ck.Item.Add("rugby");
ck.AutoPostBack=true; //postback occur when checkbox is checked
form1.Controls.Add(ck);
ck.selectedIndexChanged +=new EventHandler(chkvalue);

protected void chkvalue(object sender,EventArgs e)
{
int a=ck.Items.Count; // count no of item in checkboxlist
for(int i=0;i<a;i++)
{
it(ck.Item[i].selected)
{
Response.Write(ck.Item[i].Text);
}
}
}

Display the value of checkbox when it get checked

To retrieve the id and Text of dynamic generate button when it get clicked in asp.net

//First create the object of Button
Button b=new Button();
//suppose i want 5 button
for(int i=0;i<5;i++)
{
 b.ID="b"+ i;
b.Text="Btn"+i;
b.Click +=new EventHandler(getvalue);
}
protected void getvalue(object sender,EventArgs e)
{
Button b1=sender as Button;
Response.Write(b1.ID);
Response.Write(b1.Text);
}
 It Will display the id and text of particular button, when it get clicked