"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, March 30, 2011

Dynamically adding HTML controls using javascript

Hi *.*,
yesterday ,I login to my facebook account and doing some timepass there. then i notice there are doing everything with out postback. Then i goto ViewSource option... Holly fuck.. Really shocked full JavaScript from top to bottom.Then my interest change how they... basic is Dynamically creating everything they need in JavaScript!!!!!.I'm not showing how they are, but the basic of that is this.as per some tips from developers.facebook.com.

Adding Elements like textbox, button, radio button etc in a html form using JavaScript is very simple. JavaScript’s document object has a method called createElement() which can be used to create html elements dynamically

source code


<html>
<head>
    <title>Dynamically add Textbox, Radio, Button in html Form using JavaScript</title>

    <script language="javascript">
        function add(type) {

            //Create an input type dynamically.
            var element = document.createElement("input");

            //Assign different attributes to the element.
            element.setAttribute("type", type);
            element.setAttribute("value", type);
            element.setAttribute("name", type);

            var foo = document.getElementById("fooBar");

            //Append the element in page (in span).
            foo.appendChild(element);

        }
    </script>

</head>
<body>
    <form>
    <h2>
        Dynamically add element in form.</h2>
    Select the element and hit Add to add it in form.
    <br />
    <select name="element">
        <option value="button">Button</option>
        <option value="text">Textbox</option>
        <option value="radio">Radio</option>
    </select>
    <input type="button" value="Add" onclick="add(document.forms[0].element.value)" />
    <span id="fooBar">&nbsp;</span>
    </form>
</body>
</html>



hope this helped u. Stay Tune...
Have a nice day... 'N happy Coding :)

Tuesday, March 29, 2011

trim() in javascript and jquery

Hai *.*,
Here i'm presenting a simple tip over trim() on JavaScript and jquery.


using jQuery
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
var _str = “   My string ";
var _String = jQuery.trim(_str);

Trim() in javascript
var _str = "   My string ";
var _String = _str.trim();

hope u understood it.
Stay tune... 
Have a nice day... 'N happy Coding :)

Monday, March 28, 2011

Easy way to Print a web page

Hi *.*,
Now a day's almost all website giving us the option to print web-pages and its very simple to achieve this functionality.window.print() Is used for this.
 

Through Client Side


  <asp:Button ID="btn_Print" runat="server" Text="Print" 
OnClientClick="javascript:window.print();" />
 
Through Server Side 
 Normally v done through client side but some cases, example : only registered users are 
allowed this functionality then it's need a postback isit. 
 
protected void printButton_Click(object sender, EventArgs e)
 {
  check ur condition
   // yes
     btn_Print.Attributes.Add("onclick", "return printing()");
  else
   //no
  }

and on html page add this JavaScript function  

function printing()
        {
            window.print();
           
        }

Hope it helps u.
Stay tune...
Have a nice day... 'N happy Coding :)

hide some part of page while printting using css

Hi *.*,

In my project I need to implement functionality where I have to print Order print receipt. But the real thing is I have to hide some part of page when I print page and I have to use same page for this.
The easy solution for this problem is you need to give one more style sheet for the print purpose by which you can hide those element which are not required. For this you require to use attribute
media = print
of the STYLE element. Following is small code for this issue
<style media="screen">
  .noPrint{ display: block; }
  .yesPrint{ display: block !important; }
</style>
<style media="print">
  .noPrint{ display: none; }
  .yesPrint{ display: block !important; }
</style>
or when you linking style sheet
<link css="" href="ScreenStyleSheet.css" media="screen" rel="stylesheet" type="" text="">
<link css="" href="PrintStyleSheet.css" media="print" rel="stylesheet" type="" text="">

Using media type as print you can print only those element that you want. Even you can print element with the different style

Have a nice day... 'N happy Coding :)

Friday, March 25, 2011

Sample application on SQLITE and ASP.NET C#

Hai *.*,
As i told before here 'm presenting a sample application using SQLITE and C#.i prefer u before moving to an example go through the Introduction to SQLITE.

In a sentence, "SQLite is a in-process library that implements a self-contained, server less, zero-configuration, transactional SQL database engine"

Actually the flow of control is represented here by a diagram so that i can avoid more explanation here.

Flow Of Control

it's a simple application so i don't think much more explanation needed, the above pix is enough.

First a login box will appear with an option create new user
To create a new user, a new window will appear
Here on intilization i'm checking where database exist or not if not create a new one at C:/WINDOWS/
code for that

        # region Checking DB exist or not
        public void _Cfile()
        {

            string curFile = @"C:/WINDOWS/WinApp.db";
            if (File.Exists(curFile) == true)
            {
                //if present
            }
            else
            {
                SQLiteConnection.CreateFile("C:\\WINDOWS\\WinApp.db");
                sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
                _CreateTable();
// if not present
            }
        }

        # endregion

look on here i'm creating new database and connection also i'm calling a function to create a table also, here is the code for that.here i'm creating two tables one for storing the login details another for storing the data,'n here its go...
        # region Create New Table
        private void _CreateTable()
        {
            try
            {
                //// User table
                string _query="Create Table Mst_User( pk_id INTEGER PRIMARY KEY, str_User TEXT NOT NULL, Str_Password TEXT NOT NULL)";
                SQLiteCommand cmd;
                sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
                sqlite.Open();
                cmd = sqlite.CreateCommand();
                cmd.CommandText = _query ;
                cmd.ExecuteScalar();
                /// user data table
                string _Query = "CREATE TABLE Mst_UserData( pk_Data INTEGER PRIMARY KEY,str_Data TEXT NOT NULL)";
                SQLiteCommand cmde;
                sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
                sqlite.Open();
                cmde = sqlite.CreateCommand();
                cmde.CommandText = _Query;
                cmde.ExecuteScalar();
            }
            catch (Exception ex)
            {
                _Error(ex);
            }
           
           
        }
        # endregion

After user entering login data i'm storing it and exit user.

        # region Submitting data
        public void _SubmitData(string _Uname, string _Pass)
        {
            try
            {
                string _Query = "INSERT INTO Mst_User Values (null,'" + _Uname + "','" + _Pass + "')";
                SQLiteCommand cmd;
                sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
                sqlite.Open();
                cmd = sqlite.CreateCommand();
                cmd.CommandText = _Query;
                cmd.ExecuteNonQuery();
                txt_Password.Text = "";
                txt_UserName.Text = "";
                lblErr.Text = "User created Succefully.";
            }
            catch (Exception ex)
            {
                _Error(ex);
            }
        }
        # endregion
 
After the user login, i'm showing data in the database on a grid on form load, 

 and the code of that is,

        # region Bind

        private void _bind()
        {
            SQLiteDataAdapter ad;
            DataTable dt = new DataTable();
            BindingSource Bsource = new BindingSource();
            DataSet ds = new DataSet();
            try
            {
                SQLiteCommand cmd;
                sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
                sqlite.Open();
                cmd = sqlite.CreateCommand();
                cmd.CommandText = "SELECT pk_Data as SlNo,str_Data as Data FROM Mst_UserData";
                ad = new SQLiteDataAdapter(cmd);
                ad.Fill(ds, "Mst_UserData");
                Bsource.DataSource = ds.Tables["Mst_UserData"];
                dataGridView1.DataSource = Bsource;
            }
            catch (Exception ex)
            {
               
                throw;
            }
            sqlite.Close();
        }

        # endregion

and 1 more option 'm providing is to enter data, here is the code.


        # region Submit data
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string _data = txt_Data.Text.Trim();
                string _query = "INSERT INTO Mst_UserData VALUES( null,'" + _data + "')";
                SQLiteCommand cmd;
                sqlite.Open();
                cmd = sqlite.CreateCommand();
                cmd.CommandText = _query;
                cmd.ExecuteNonQuery();
                txt_Data.Text="";
                lblMsg.Text = "Data Added Succefully";
                _bind();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message.ToString();
            }


        }
        # endregion

 and finally 






i prefer u once more before doing an example go through the Introduction to SQLITE. I heard abt SQLITE and i plan to create one 'n i did it that. This is on of my hobby creating some thing i never did.On that crazy i did it so use this code as a reference only because i didn't had much R&D over this.I take 1 hr and 23 min  to create this app after hearing abt this.So a sea is infront of me too...

so stay tune...
Have a nice day... 'N happy Coding :)