Tuesday, 10 January 2012

Image Example in asp.net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="~/images/barney.jpeg" Width="200" HotSpotMode="Navigate">
        <asp:RectangleHotSpot  Top="0" Bottom="448" Left="0" Right="250" AlternateText="hi i am teddy"  NavigateUrl="~/imageauto.aspx" />
        </asp:ImageMap>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}


How to save The RadioButtonlist values in asp.net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <h2 style=" color:blue; font-style:normal ">  
            How to save RadioButtonlist 
            <br />values in asp.net  
        </h2>  
    <div>
       <asp:RadioButtonList ID="RadioButtonList1" runat="server">
       <asp:ListItem>Male</asp:ListItem>
       <asp:ListItem>Female</asp:ListItem>
    </asp:RadioButtonList>
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label><br />
    
        <asp:Button ID="Button1" runat="server" Text="SAVE" OnClick="btnsave" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class rbdlistsave : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsave(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue != "")
        {

            string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into [Gender]values(@gender)";
            cmd.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedValue);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = RadioButtonList1.SelectedValue +" was saved sucessesfully";
            sqlcon.Close();
        }
        else
        {
            Label1.Text = "please check gender";
        }


    }

}




Tuesday, 3 January 2012

ListBox Clint Side Validations Using Javascript in ASP.Net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function projectselect()
{
var BaseControl = null;
BaseControl = document.getElementById('<%= this.form1.ClientID %>');
if (BaseControl == null)
return false;           
var ChildControl = "ListBox1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'ListBox' && Inputs[n].id.indexOf(ChildControl, 0) >= 0 && Inputs[n].checked)
  var valid= true;
if(!valid)
{
alert('Select at least one ..!');
 return false;
}
}
    </script>   
</head>
<body>
    <form id="form1" runat="server">  
    <h2 style=" color:blue; font-style:normal ">  
           ListBox Javascript Validation
            <br /> in asp.net  
        </h2> 
    <div>
        <asp:ListBox ID="ListBox1" runat="server">
        <asp:ListItem>HI</asp:ListItem>
        <asp:ListItem>HOW</asp:ListItem>
        <asp:ListItem>ARE</asp:ListItem>
        <asp:ListItem>YOU</asp:ListItem>
        </asp:ListBox>
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor="Blue" ></asp:Label><br /><br />       
        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"  OnClientClick="projectselect()"
         Font-Bold="true"  
         ForeColor="blue" />
        <asp:Button
         ID="Button2" runat="server" Text="Clear" onclick="Button2_Click"  Height="30"  
         Font-Bold="true"  
         ForeColor="blue"/>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class listboxsave : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
        var sSkill = "";
        ListBox project = FindControl("ListBox1") as ListBox;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;

            }
        }
        if (sSkill != "")
        {
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [checklist] values(@skill)";
            cmd.Parameters.AddWithValue("@skill", sSkill);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = sSkill + " was saved sucessesfully";
            sqlcon.Close();
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox1.ClearSelection();
        Label1.Text = "";
        
    }
}




ListBox Values Saved in Database in asp.net

Create a database name trail and then execute the following script

CREATE TABLE [dbo].[checklist](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [skill] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listboxsave.aspx.cs" Inherits="listboxsave" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
       <script type="text/javascript" language="javascript">
    function projectselect()
{

var BaseControl = null;
BaseControl = document.getElementById('<%= this.form1.ClientID %>');
if (BaseControl == null)
return false;           
var ChildControl = "ListBox1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'ListBox' && Inputs[n].id.indexOf(ChildControl, 0) >= 0 && Inputs[n].checked)
  var valid= true;
if(!valid)
{
alert('Select at least one ..!');
 return false;
}
}
    </script> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2 style=" color:blue; font-style:normal ">  
            How to use ListBox  and
            <br /> ClearSelection method in asp.net  
        </h2>  
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Font-Size="Medium" Font-Italic="true">
        <asp:ListItem Value="HI">HI</asp:ListItem>
        <asp:ListItem Value="HOW">HOW</asp:ListItem>
        <asp:ListItem Value="ARE">ARE</asp:ListItem>
        <asp:ListItem Value="YOU">YOU</asp:ListItem>    
        </asp:ListBox><br />
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor=" blue" ></asp:Label>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Select atleast one" ControlToValidate="ListBox1"></asp:RequiredFieldValidator><br /><br />
    <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"   Font-Bold="true"  ForeColor="blue" OnClientClick="projectselect()"; />
        <asp:Button ID="Button2" runat="server" Text="Clear" onclick="Button2_Click" Height="30"  
            Font-Bold="true"  
            ForeColor="blue"/>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class listboxsave : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
        var sSkill = "";
        ListBox project = FindControl("ListBox1") as ListBox;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;

            }
        }
        if (sSkill != "")
        {
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [checklist] values(@skill)";
            cmd.Parameters.AddWithValue("@skill", sSkill);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = sSkill + " was saved sucessesfully";
            sqlcon.Close();
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox1.ClearSelection();
        Label1.Text = "";
        
    }
}



How To Checkboxlist Values Saved in Database in ASP.Net

Create a Database name trail and Then Execute The Following Script

CREATE TABLE [dbo].[checklist](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [skill] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="checksave.aspx.cs" Inherits="checksave" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function projectselect()
{
var BaseControl = null;
BaseControl = document.getElementById('<%= this.form1.ClientID %>');
if (BaseControl == null)
return false;           
var ChildControl = "CheckBoxList1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(ChildControl, 0) >= 0 && Inputs[n].checked)
  var valid= true;
if(!valid)
{
alert('Select at least one ..!');
 return false;
}
}
    </script>   
</head>
<body>
    <form id="form1" runat="server">  
    <h2 style=" color:blue; font-style:normal ">  
           CheckBoxList values save in Database 
            <br /> in asp.net  
        </h2> 
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem>HI</asp:ListItem>
        <asp:ListItem>HOW</asp:ListItem>
        <asp:ListItem>ARE</asp:ListItem>
        <asp:ListItem>YOU</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor="Blue" ></asp:Label><br /><br />       
        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"  OnClientClick="projectselect()"
         Font-Bold="true"  
         ForeColor="blue" />
        <asp:Button
         ID="Button2" runat="server" Text="Clear" onclick="Button2_Click"  Height="30"  
         Font-Bold="true"  
         ForeColor="blue"/>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class checksave : System.Web.UI.Page
{  
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
        var sSkill = "";
        CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;
            }
        }
        if (sSkill != "")
        {
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [checklist] values(@skill)";
            cmd.Parameters.AddWithValue("@skill", sSkill);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = sSkill + " was saved sucessesfully";
            sqlcon.Close();
        }       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {      
        CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected = false)
            {
            }
        }
        Label1.Text = "";
   }
}

RadioButtonList Bind Using sqldatasource in asp.net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
             sqldatasource For RadioButtonList 
            <br />  in asp.net  
        </h2>
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
            DataSourceID="Sqldatasource1" DataTextField="department" 
            DataValueField="department" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true">   
        </asp:RadioButtonList>
    
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label>
        <asp:sqldatasource ID="Sqldatasource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:empConnectionString %>" 
            SelectCommand="SELECT * FROM [department]"></asp:sqldatasource>

    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class radiodatabind : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "your selection is ="  +  RadioButtonList1.Text;
    }
}

ADD RadioButtonList Values By Using Code in ASP.Net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
            How to ADD RadioButtonList Values 
            <br /> By Using Code in asp.net  
        </h2> 
    <div>
    <div>
    <asp:RadioButtonList 
    id="languageRadioButtonList"
    runat="server" 
    OnSelectedIndexChanged="DropDownListSelectionChanged" >
</asp:RadioButtonList><br/>
 <b><asp:label runat="server" id="favoriteLanguage" style="color:blue" /></b><br />
<asp:button ID="Button1" runat="server" Text="Submit" ForeColor="Blue"/>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class radiolist : System.Web.UI.Page
{
    
    protected void Page_Load(object o, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListItem item;
            item = new ListItem("1", "HI");
            languageRadioButtonList.Items.Add(item);
            item = new ListItem("2", "HOW");
            languageRadioButtonList.Items.Add(item);
            item = new ListItem("3", "ARE");
            languageRadioButtonList.Items.Add(item);
        }
    }

    protected void DropDownListSelectionChanged(object o, EventArgs e)
    {
        favoriteLanguage.Text = "Selected Value:"+languageRadioButtonList.SelectedItem.Value;
    }
}



RadioButton OnCheckedChanged Example in ASP.Net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
            How to use RadioButton OnCheckedChanged
            <br />  in asp.net  
        </h2> 
    <div>
        <asp:RadioButton ID="rdbMale" GroupName="Gender" Text="Male" runat="server" OnCheckedChanged="RadioButton_CheckedChanged" AutoPostBack="true" /><br />
        <asp:RadioButton ID="rdbFemale" GroupName="Gender" Text="Female" runat="server" OnCheckedChanged="RadioButton_CheckedChanged" AutoPostBack="true"/><br />
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label><br />
    </div>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    </asp:RadioButtonList>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class radio2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton selectedRadioButton = (RadioButton)sender;
        Label1.Text = "You choose: " + selectedRadioButton.Text;
    }
}


Monday, 2 January 2012

Radio Button Example Male and Female

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
            How to use RadioButton  
            <br />  in asp.net  
        </h2> 
    <div> 
        <asp:RadioButton ID="rdbMale" GroupName="Gender" Text="Male" runat="server" /><br />
        <asp:RadioButton ID="rdbFemale" GroupName="Gender" Text="Female" runat="server" /><br />
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label><br />
        <asp:Button ID="btnSave" Text="Save " runat="server" OnClick="btn_Click" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class radio : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Click(object sender, EventArgs e)
    {
       
        if (rdbMale.Checked)
            Label1.Text = "You choose: "+ rdbMale.Text;
        else
            Label1.Text ="You choose: "+ rdbFemale.Text;
        
    }
}


CheckBoxList Clint side validations Using Javascript in ASP.Net

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function projectselect()
{

var BaseControl = null;
BaseControl = document.getElementById('<%= this.form1.ClientID %>');
if (BaseControl == null)
return false;           
var ChildControl = "CheckBoxList1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(ChildControl, 0) >= 0 && Inputs[n].checked)
  var valid= true;
if(!valid)
{
alert('Select at least one ..!');
 return false;
 
}
}
    </script>
    
    
</head>
<body>
    <form id="form1" runat="server">
    
    <h2 style=" color:blue; font-style:normal ">  
            How to use CheckBoxList  
            <br />  in asp.net  
        </h2> 
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem>HI</asp:ListItem>
        <asp:ListItem>HOW</asp:ListItem>
        <asp:ListItem>ARE</asp:ListItem>
        <asp:ListItem>YOU</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor=" blue" ></asp:Label><br /><br />
       
        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"  OnClientClick="projectselect()"
            Font-Bold="true"  
            ForeColor="blue" />
        <asp:Button
            ID="Button2" runat="server" Text="Clear" onclick="Button2_Click"  Height="30"  
            Font-Bold="true"  
            ForeColor="blue"/>
    </div>
    </form>
</body>
</html>





using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class checklist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    var sSkill = "";
    CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
    
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;

            }
        }
      
        if (sSkill != "")
        {
            Label1.Text = sSkill;
        }
        else
        {
            Label1.Text = "select one";
        }

}


       
protected void Button2_Click(object sender, EventArgs e)
{

    var sSkill = "";
    CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
    foreach (ListItem oItem in project.Items)
   {
       if (oItem.Selected=false)
       {
        
       }
   }

Label1.Text = "";
}
}






Check Box Terms And Conditions

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function ValidateForm(form){
ErrorText= "";
if ( form.CheckBox1.checked == false )
 {
 debugger; 
 alert ( "Please check the Terms & Conditions box." );
  return false;
   }
   if (ErrorText= "") 
   { 
   alert ( "Submitted sucessfully" );
}
}
  </script>  
</head>
<body>
    <form id="form1" runat="server">
    
    <h2 style=" color:blue; font-style:normal "> Check Box in asp.net</h2>
    <div style="margin-left:40">
        <asp:CheckBox ID="CheckBox1" runat="server"  Text="Terms and Condistions" AutoPostBack="true"/><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
        <asp:Button ID="Button1" runat="server" Text="Submit"  OnClientClick="ValidateForm(this.form)" value="Submit" ForeColor="blue"/>
       
    </div>
    
    </form>
</body>
</html>




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class check : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  }



Sunday, 1 January 2012

How to use ListBox in ASP.NET

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head runat="server">
    <title>Untitled Page</title>
</head>

<body>

    <form id="Form1" runat="server">
     <h2 style=" color:blue; font-style:normal ">Moving items between lists 
     <br />with Add and Remove</h2> 

    <table width="300" border="0" cellspacing="1" cellpadding="1">
        <tr>
            <td >Skill:</td>
            <td ></td>
            <td >Skill set:</td>
        </tr>
        <tr>
            <td >        
                <asp:ListBox id="UserList" runat="server" Rows="4" selectionmode="Multiple">
                    <asp:ListItem>ASP</asp:ListItem>
                    <asp:ListItem>C#</asp:ListItem>
                    <asp:ListItem>VB</asp:ListItem>
                    <asp:ListItem>ADO</asp:ListItem>
                </asp:ListBox>
            </td>
            <td>
            <asp:button text="->" runat="server" ID="AddButton" onclick="AddButton_Click"/>
            <br/>
            <asp:button text="<-" runat="server" ID="RemoveButton" onclick="RemoveButton_Click"/>
            </td>
            <td >        
                <asp:ListBox id="GroupList" runat="server" Rows="4" selectionmode="Multiple">
                </asp:ListBox>
            </td>
        </tr>
    </table>

    </form>

</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class listbox2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void AddButton_Click(Object sender, EventArgs e)
    {
        ListItemCollection Users = new ListItemCollection();
        foreach (ListItem User in UserList.Items)
        {
            if (User.Selected)
                Users.Add(User);
        }
        foreach (ListItem User in Users)
        {
            UserList.Items.Remove(User);
            GroupList.Items.Add(User);
        }
    }
    protected void RemoveButton_Click(Object sender, EventArgs e)
    {
        ListItemCollection Users = new ListItemCollection();
        foreach (ListItem User in GroupList.Items)
        {
            if (User.Selected)
                Users.Add(User);
        }
        foreach (ListItem User in Users)
        {
            UserList.Items.Add(User);
            GroupList.Items.Remove(User);
        }
    }
}





ListBox example in ASP.NET

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2 style=" color:blue; font-style:normal ">  
            How to use ListBox  and
            <br /> ClearSelection method in asp.net  
        </h2>  
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Font-Size="Medium" Font-Italic="true">
        <asp:ListItem Value="HI">HI</asp:ListItem>
        <asp:ListItem Value="HOW">HOW</asp:ListItem>
        <asp:ListItem Value="ARE">ARE</asp:ListItem>
        <asp:ListItem Value="YOU">YOU</asp:ListItem>    
        </asp:ListBox><br />
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor=" blue" ></asp:Label>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Select atleast one" ControlToValidate="ListBox1"></asp:RequiredFieldValidator><br /><br />
        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"  
            Font-Bold="true"  
            ForeColor="blue" />
        <asp:Button
            ID="Button2" runat="server" Text="Clear" onclick="Button2_Click" Height="30"  
            Font-Bold="true"  
            ForeColor="blue"/>
    </div>
    </form>
</body>
</html>


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var sSkill = "";
        ListBox project = FindControl("ListBox1") as ListBox;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;

            }
        }
        Label1.Text = sSkill;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox1.ClearSelection();
        Label1.Text = "";
        RequiredFieldValidator1.Enabled = false;
    }
}





connection string in web.config

<connectionStrings>
<add name="ConString" connectionString="Data Source=ServerName;Initial Catalog=dbname;Integrated Security=True;" providerName="System.Data.SqlClient"/>
connectionStrings>


string conect = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(conect);