Thursday, 30 May 2013

How to access server Checkbox control using javascript in asp.net



How to access server Checkbox control using javascript in asp.net


Step 1 : First create your website in asp.net and create one .aspx page .

Step2: Open .aspx page and drag and drop check box control and onChange event to check box control.

<asp:CheckBox ID="cbValue" runat="server" Text="Select Value" onChange="cbValue_onChange(this);" />

Step 3 : Add javascript function to handle onchange event of check box control.

<script type="text/javascript">
        function cbValue_onChange() {
            var check_box = document.getElementById("<%= cbValue.ClientID %>");
            if (check_box.checked == true) {
                alert("Check box is checked");
            }
            else {
                alert("Check box is UN-checked");
            }
        }
    </script>


Step 5 : Just run your code and check is your code is running perfectly and access all event of checkbox control.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxControl.aspx.cs" Inherits="CheckBoxControl" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function cbValue_onChange() {
            var check_box = document.getElementById("<%= cbValue.ClientID %>");
            if (check_box.checked == true) {
                alert("Check box is checked");
            }
            else {
                alert("Check box is un-checked");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:CheckBox ID="cbValue" runat="server" Text="Select Value" onChange="cbValue_onChange(this);" />

        </div>
    </form>
</body>
</html>

Wednesday, 29 May 2013

Create run time DataTable and add rows,column and data to Datatable using c# code

Create run time DataTable and add rows,column and data to Datatable using c# code



Step 1 : First create your website in asp.net and create one .aspx page .

Step2  : Open .aspx page source code .cs file




Step 3: First add one import class file to access DataTable class

using System.Data;
 
Step 3 : Create one object of DataTable class

DataTable dt = new DataTable("Names");


Step 4 : Add rows and columns and data to data table

 DataTable dt = new DataTable("Names");
        DataColumn dc1 = new DataColumn("Name");
        DataColumn dc2 = new DataColumn("Age");
        dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
        DataRow dr1 = dt.NewRow();
        dr1[0] = "Ahmed";
        dr1[1] = "27";
        DataRow dr2 = dt.NewRow();
        dr2[0] = "Peter";
        dr2[1] = "30";
        DataRow dr3 = dt.NewRow();
        dr3[0] = "John";
        dr3[1] = "20";
        DataRow dr4 = dt.NewRow();
        dr4[0] = "Ali";
        dr4[1] = "30";

        DataRow dr5 = dt.NewRow();
        dr5[0] = "Ali";
        dr5[1] = "30";


        DataRow dr6 = dt.NewRow();
        dr6[0] = "Ali";
        dr6[1] = "30";


        DataRow dr7 = dt.NewRow();
        dr7[0] = "Ali";
        dr7[1] = "30";

        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        dt.Rows.Add(dr3);
        dt.Rows.Add(dr4);
        dt.Rows.Add(dr5);
        dt.Rows.Add(dr6);
        dt.Rows.Add(dr7);



Step 5: Access this table any where else in your code .






How to use StringBuilder class and accessing in different-2 property of that class using c#

How to use StringBuilder object and accessing in different-2 property of that object using c#



Step 1 : First create your website in asp.net and create one .aspx page .

Step2  : Open .aspx page source code .cs file



Step 3: First add one import class file to access StringBuilder class

using System.Text;
 
Step 3 : Create one object of StringBuilder class

StringBuilder str = new StringBuilder();


Step 4 : Access different-2 property of StringBuilder object.

StringBuilder str = new StringBuilder();

// append test in stringbuilder
str.Append("My name is Anuj Rohila");

 // print string builder content
Response.Write("<br/>String : " + str.ToString());

// count the length of string
Response.Write("<br/>Length : " + str.Length);

 // append symbol  str.Append("=-=-=-",1,2)
Response.Write("<br/>String : " + str.Append("=-=-=-", 1, 2));

 // remove symbol  str.Remove(1,5)
Response.Write("<br/>String : " + str.Remove(1, 5));

// insert test  str.Insert(1,"Test",2)
Response.Write("<br/>String : " + str.Insert(1,"Test",2));

 // Replace test  str.Replace("Anuj", "David")
Response.Write("<br/>String : " + str.Replace("Anuj", "David"));



Step 5: Also access more property and do more change as per you.

Output will be:

String : My name is Anuj Rohila
Length : 22
String : My name is Anuj Rohila-=
String : Me is Anuj Rohila-=
String : MTestTeste is Anuj Rohila-=
String : MTestTeste is David Rohila-





Create DataList control in Asp.net with c# code

Create DataList control in Asp.net with c# code
 

Step 1 : Create you asp.net website first and then create on .aspx page in your website.

Step 2 : Open .aspx design page .

Step 3 : Open Toolbox dialog box  View Menu => Toolbox option

Step 4 : Click on Data sub link in Toolbox and select List view control from list and drag and drop to the page where you want to display that control.

<asp:DataList  ID="DataList " runat="server"></asp:DataList >

Step 5 : This is default list view control.We can create Item templete

<asp:DataList ID="DataList1" runat="server">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>


Step 6 : Create one function will create dummy DataTable to bind data in DataList

  public  DataTable getDataForListView()
    {
        DataTable dt = new DataTable("Names");
        DataColumn dc1 = new DataColumn("Name");
        DataColumn dc2 = new DataColumn("Age");
        dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
        DataRow dr1 = dt.NewRow();
        dr1[0] = "Ahmed";
        dr1[1] = "27";
        DataRow dr2 = dt.NewRow();
        dr2[0] = "Peter";
        dr2[1] = "30";
        DataRow dr3 = dt.NewRow();
        dr3[0] = "John";
        dr3[1] = "20";
        DataRow dr4 = dt.NewRow();
        dr4[0] = "Ali";
        dr4[1] = "30";

        DataRow dr5 = dt.NewRow();
        dr5[0] = "Ali";
        dr5[1] = "30";


        DataRow dr6 = dt.NewRow();
        dr6[0] = "Ali";
        dr6[1] = "30";


        DataRow dr7 = dt.NewRow();
        dr7[0] = "Ali";
        dr7[1] = "30";

        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        dt.Rows.Add(dr3);
        dt.Rows.Add(dr4);
        dt.Rows.Add(dr5);
        dt.Rows.Add(dr6);
        dt.Rows.Add(dr7);
        return dt;
    }


Step 7 : Bind Data to DataList control

DataList1.DataSource = getDataForListView();
DataList1.DataBind();


 Step 8 : This way you can access DataList control. We learn in lext post how to format list view control and how to access property of Datalist control.


Create ListView control in Asp.net with c# code

Create ListView control in Asp.net with c# code
 

Step 1 : Create you asp.net website first and then create on .aspx page in your website.

Step 2 : Open .aspx design page .

Step 3 : Open Toolbox dialog box  View Menu => Toolbox option

Step 4 : Click on Data sub link in Toolbox and select List view control from list and drag and drop to the page where you want to display that control.

<asp:ListView ID="ListView1" runat="server"></asp:ListView>

Step 5 : This is default list view control.We can create Item templete

<asp:ListView ID="ListView1" runat="server">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:ListView>


Step 6 : Create one function will create dummy DataTable to bind data in ListView

  public  DataTable getDataForListView()
    {
        DataTable dt = new DataTable("Names");
        DataColumn dc1 = new DataColumn("Name");
        DataColumn dc2 = new DataColumn("Age");
        dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
        DataRow dr1 = dt.NewRow();
        dr1[0] = "Ahmed";
        dr1[1] = "27";
        DataRow dr2 = dt.NewRow();
        dr2[0] = "Peter";
        dr2[1] = "30";
        DataRow dr3 = dt.NewRow();
        dr3[0] = "John";
        dr3[1] = "20";
        DataRow dr4 = dt.NewRow();
        dr4[0] = "Ali";
        dr4[1] = "30";

        DataRow dr5 = dt.NewRow();
        dr5[0] = "Ali";
        dr5[1] = "30";


        DataRow dr6 = dt.NewRow();
        dr6[0] = "Ali";
        dr6[1] = "30";


        DataRow dr7 = dt.NewRow();
        dr7[0] = "Ali";
        dr7[1] = "30";

        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        dt.Rows.Add(dr3);
        dt.Rows.Add(dr4);
        dt.Rows.Add(dr5);
        dt.Rows.Add(dr6);
        dt.Rows.Add(dr7);
        return dt;
    }


Step 7 : Bind Data to ListView control

 ListView1.DataSource = getDataForListView();
ListView1.DataBind();


 Step 8 : This way you can access Listview control. We learn in lext post how to format list view control and how to access property of list view control.


How to use DateTime object and accessing in different-2 property of that datetime object using c#

How to use DateTime object and accessing in different-2 property of that datetime object using c#



Step 1 : First create your website in asp.net and create one .aspx page .

Step2  : Open .aspx page source code .cs file




Step 3 : Access different-2 property of datetime object

  // accessing day property
   Response.Write("<br/>1. " + DateTime.Now.Day);

// accessing month property
Response.Write("<br/>2. " + DateTime.Now.Month);

// accessing year property
Response.Write("<br/>3. " + DateTime.Now.Year);

 // accessing minutes property
   Response.Write("<br/>4. " + DateTime.Now.Minute);

 // accessing hours property
Response.Write("<br/>4. " + DateTime.Now.Hour);

 // accessing second property
Response.Write("<br/>5. " + DateTime.Now.Second);

// accessing adding days in current day
Response.Write("<br/>6. " + DateTime.Now.AddDays(5));

 // accessing adding month in current month
Response.Write("<br/>7. " + DateTime.Now.AddMonths(5));

// accessing adding year in current year
Response.Write("<br/>8. " + DateTime.Now.AddYears(5));

// accessing minus year in current year
Response.Write("<br/>9. " + DateTime.Now.AddYears(-5));

// accessing minus months in current month
 Response.Write("<br/>10. " + DateTime.Now.AddMonths(-11));




Step 4 : This way you can access different-2 property of date time object.

Output will be:

1. 29
2. 1
3. 2012
4. 50
4. 16
5. 16
6. 2012-02-03 04:50:16 PM
7. 2012-06-29 04:50:16 PM
8. 2017-01-29 04:50:16 PM
9. 2007-01-29 04:50:16 PM
10. 2011-02-28 04:50:16 PM 







How to use DateTime object and manupulate property of that object using c#

How to use DateTime object and accessing in different-2 format of that object using c#



Step 1 : First create your website in asp.net and create one .aspx page .

Step2  : Open .aspx page source code .cs file




Step 3 : Access different-2 format of datetime object

 //default fomrat of date  
Response.Write(dt.ToString()); 
 
// Accessing only Day month year
Response.Write("<br/>1. " + DateTime.Now.ToString("dd-MM-yyyy"));

 // Accessing only 24 hours minute second
Response.Write("<br/>2. " + DateTime.Now.ToString("HH:mm:ss"));

 // Accessing only 12 hours minute second
Response.Write("<br/>3. " + DateTime.Now.ToString("hh:mm:ss tt"));

 // Accessing full date
 Response.Write("<br/>4. " + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss tt"));

Response.Write("<br/>5. " + DateTime.Now.ToString("ddd-MMM-yy"));

 Response.Write("<br/>6. " + DateTime.Now.ToString("dddd-MMMM-yyyy"));



Step 4 : This way you can access different-2 format of date time object.

Output will be:

1. 29-05-2013
2. 16:36:10
3. 04:36:10 PM
4. 29-05-2013 04:36:10 PM
5. Wed-May-13
6. Wednesday-May-2013





Handle Asp:Textbox control event using javascript in Asp.net

How to access all property of List box using JavaScript function.





Step 1 : First create your website in asp.net and create one .aspx page .

Step2: Open .aspx page and drag and drop ListBox contol

asp:TextBox ID="txt1" runat="server"  onChange="txt1_onChange()"
                 oncopy="return txt1_oncopy()" onpaste="return txt1_onpaste()" oncut="return txt1_oncut()"></asp:TextBox>


Step 3 : All all script at page header portion to handle text box event.

<script type="text/javascript">
        function txt1_onChange() {
            var txt = document.getElementById("<%= txt1.ClientID %>");
            alert("onFocus value is = " + txt.value);
            return false;
        }

        function txt1_oncopy() {
            //do your transcation
            // return true for allow copy
            return false;
        }

        function txt1_onpaste() {
            //do your transcation
            // return true for allow paste
            return false;
        }

        function txt1_oncut() {
            //do your transcation
            // return true for allow cut
            return false;
        }
    </script>


Step 5 : Just run your code and check is your code is running perfectly and access all event of textbox control.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaTextboxControl.aspx.cs"
    Inherits="JavaTextboxControl" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>List Box Example</title>
    <script type="text/javascript">
        function txt1_onChange() {
            var txt = document.getElementById("<%= txt1.ClientID %>");
            alert("onFocus value is = " + txt.value);
            return false;
        }

        function txt1_oncopy() {
            //do your transcation
            // return true for allow copy
            return false;
        }

        function txt1_onpaste() {
            //do your transcation
            // return true for allow paste
            return false;
        }

        function txt1_oncut() {
            //do your transcation
            // return true for allow cut
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <br />
    <asp:TextBox ID="txt1" runat="server" onChange="txt1_onChange()" oncopy="return txt1_oncopy()"
        onpaste="return txt1_onpaste()" oncut="return txt1_oncut()"></asp:TextBox>
    <br />
</body>
</html>


 


How to access all property of List box using JavaScript function.

How to access all property of List box using JavaScript function.





Step 1 : First create your website in asp.net and create one .aspx page .

Step2: Open .aspx page and drag and drop ListBox contol

<asp:ListBox ID="lbScriptAccess" runat="server" Height="200px" Width="350px"></asp:ListBox>
 
Step 3:Add asp:button on page to access all property

<asp:Button ID="Button1" runat="server" Text="Add Item" OnClientClick="return AddItem();" />
        <asp:Button ID="Button2" runat="server" Text="Delete Selected Item" OnClientClick="return DeleteItem();" />
        <asp:Button ID="Button3" runat="server" Text="Delete All Item" OnClientClick="return DeleteAllItem();" />
        <asp:Button ID="Button4" runat="server" Text="Selected Index Position" OnClientClick="return IndexPosition();" />
        <asp:Button ID="Button5" runat="server" Text="Selected Value" OnClientClick="return SelectedValue();" />

Step 4:  Add javscript function to access all asp:button client script to handle script.

<script type="text/javascript">
        function AddItem() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            var option1 = document.createElement("option");
            option1.text = "Value" + list.length;
            option1.value = list.length;
            list.options.add(option1);
            return false;
        }

        function DeleteItem() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            alert(list.options[list.selectedIndex].text + " is deleted successfully");
            list.remove(list.selectedIndex);
            return false;
        }
        function DeleteAllItem() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            list.length = 0;
            alert("All item Deleted from list");
            return false;
        }

        function IndexPosition() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            alert("Selecetd position : " + list.selectedIndex);
            return false;
        }

        function SelectedValue() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            alert("Selected value is : " + list.options[list.selectedIndex].text);
            return false;
        }
    </script>


Step 5: Just run your page and check is your code is running perfectly or not and access all transaction of listbox control.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaListControl.aspx.cs"
    Inherits="JavaListControl" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>List Box Example</title>
    <script type="text/javascript">
        function AddItem() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            var option1 = document.createElement("option");
            option1.text = "Value" + list.length;
            option1.value = list.length;
            list.options.add(option1);
            return false;
        }

        function DeleteItem() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            alert(list.options[list.selectedIndex].text + " is deleted successfully");
            list.remove(list.selectedIndex);
            return false;
        }
        function DeleteAllItem() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            list.length = 0;
            alert("All item Deleted from list");
            return false;
        }

        function IndexPosition() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            alert("Selecetd position : " + list.selectedIndex);
            return false;
        }

        function SelectedValue() {
            var list = document.getElementById("<%= lbScriptAccess.ClientID %>");
            alert("Selected value is : " + list.options[list.selectedIndex].text);
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <br />
        <br />
        <asp:ListBox ID="lbScriptAccess" runat="server" Height="200px" Width="350px"></asp:ListBox>
        <br />
        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Add Item" OnClientClick="return AddItem();" />
        <asp:Button ID="Button2" runat="server" Text="Delete Selected Item" OnClientClick="return DeleteItem();" />
        <asp:Button ID="Button3" runat="server" Text="Delete All Item" OnClientClick="return DeleteAllItem();" />
        <asp:Button ID="Button4" runat="server" Text="Selected Index Position" OnClientClick="return IndexPosition();" />
        <asp:Button ID="Button5" runat="server" Text="Selected Value" OnClientClick="return SelectedValue();" />
    </form>
</body>
</html>