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

Monday, February 28, 2011

To fire two event when button is click

protected void Button1_Click(object sender,EventArgs e)
{
     respose.write("<script type='javascript'">
response.write("window.open('default.aspx');");
response.write("</script>");
sqlconnection con=new sqlconnection("connection string.");
sqlcommand cmd=new sqlcommand("insert into sub value('a','b')",con);
con.open();
cmd.ExecuteNonQuery();
con.close();
}

it will fire two event at a time, it will popup the default.aspx page and also insert data into sub table...

Monday, February 21, 2011

Two way to refresh the page using javascript

1. Using link.
<a href="javascript:location.reload(true)">Refresh </a>
it will show link "Refresh" on page by clicking, it will refresh the page




2. Automatic refreshing  of page using javascript
<html>

<head>
<script type="text/JavaScript">
function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
</body>
</html>

it will refresh page every 5 sec when page is loaded

Sunday, February 20, 2011

Dynamically generate button or Textbox according to the data in database in asp.net

 For generating dynamic button and textbox according to data present in database
if(!IsPostBack)
       {string[] nm=new string[100];
            SqlConnection con = new SqlConnection("connection string....");
            SqlCommand cmd = new SqlCommand("sql query.....", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            int cnt = 0;
            int c=0;
            while (dr.Read())
            {
                cnt++;
                nm[c]=dr[0].ToString();
                c=c+1;
              }
            con.Close();
                
                for (int i = 0; i < cnt; i++)
                {
                    Button btn = new Button();                        TextBox txt = new TextBox();
                    btn.Text = nm[i];
                     btn.ID = i + "id";                                       txt.ID = i + "txt";
                     form1.Controls.Add(btn);                        txt.Width = 16;
                                                                                     txt.Height = 16;
                                                                                     txt.BorderStyle = BorderStyle.None;
                                                                                  //   txt.Enabled = false;
                                                                                        form1.Controls.Add(txt);
                }
                      
       }
it will generate the dynamic button and textbox according to data present in table