Total Pageviews

5 April 2012

How to select multiple selection in textbox in asp.net




Default.aspx=>
//////////////////////////////////////////
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="multidropdown.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager runat="server">
        </asp:ToolkitScriptManager>
        <asp:UpdatePanel ID="updatepanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:PopupControlExtender ID="TextBox1_PopupControlExtender" runat="server" DynamicServicePath=""
                    Enabled="True" ExtenderControlID="" TargetControlID="TextBox1" PopupControlID="Panel1"
                    OffsetY="22">
                </asp:PopupControlExtender>
                <asp:Panel ID="Panel1" runat="server" Height="116px" Width="145px" BorderStyle="Solid"
                    BorderWidth="2px" Direction="LeftToRight" ScrollBars="Auto" BackColor="#CCCCCC"
                    Style="display: none">
                    <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource1"
                        DataTextField="name" DataValueField="name" AutoPostBack="True"
                        OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
                    </asp:CheckBoxList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:con %>"
                        SelectCommand="SELECT [name] FROM [ram]"></asp:SqlDataSource>
                </asp:Panel>
                Name<asp:TextBox ID="t2" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
Default.ascx=>
/////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
       
    string s = ConfigurationManager.ConnectionStrings["con"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
 
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string name = "";
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                name += CheckBoxList1.Items[i].Text + ",";
            }
        }
        TextBox1.Text = name;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(s);
        con.Open();
        cmd = new SqlCommand("insert into demo values(@Name,@Select_name)", con);

        cmd.Parameters.AddWithValue("@Name", t2.Text);
        cmd.Parameters.AddWithValue("@select_name", TextBox1.Text);
        int x =cmd.ExecuteNonQuery();
        if (x > 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('data insert')", true);
        }
        con.Close();
           
    }
}
Webconfig=>
///////////////////////////////////////
 <connectionStrings>
    <add name="con" connectionString="Data Source=AMRIT;Initial Catalog=dct;Integrated Security=True" providerName="System.Data.SqlClient"/>
   
  </connectionStrings>

How to select country wise city in drop down list with help of asp.net and C#


Default. aspx
========================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>Country State City</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Country
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
State
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged1">
</asp:DropDownList>
City
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
</asp:DropDownList>
</div>
</form>
</body>
</html>
==============================================================================
C# code behind
==============================================================================
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;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection Conn =new SqlConnection("Data Source=GNANASUDHAKAR\\SQLEXPRESS;Initial Catalog=Sudha;Integrated Security=True");
DataSet DS = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlCommand Cmd = new SqlCommand("Select * From Country", Conn);
SqlDataAdapter DA = new SqlDataAdapter(Cmd);
DA.Fill(DS, "Country");
DropDownList1.DataSource = DS.Tables["Country"];
DropDownList1.DataTextField = "County";
DropDownList1.DataValueField = "CountryId";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Select");
DropDownList2.Items.Insert(0, "Select");
DropDownList3.Items.Insert(0, "Select");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue != "Select")
{
SqlCommand Cmd1 = new SqlCommand("Select * From State Where CountryId=" + DropDownList1.SelectedValue + "", Conn);
SqlDataAdapter DA1 = new SqlDataAdapter(Cmd1);
DA1.Fill(DS, "State");
DropDownList2.DataSource = DS.Tables["State"];
DropDownList2.DataTextField = "State";
DropDownList2.DataValueField = "StateId";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "Select");
}
}
protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
{
if (DropDownList2.SelectedValue != "Select")
{
SqlCommand Cmd2 = new SqlCommand("Select * From City Where SateId=" + DropDownList2.SelectedValue + "", Conn);
SqlDataAdapter DA2 = new SqlDataAdapter(Cmd2);
DA2.Fill(DS, "City");
DropDownList3.DataSource = DS.Tables["City"];
DropDownList3.DataTextField = "City";
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, "Select");
}
}
}
=====================================================================
SQL Server Quries...
=====================================================================
Create Database Sudha
Use Sudha
Create Table Country
(
CountryId Int Primary Key,
County Varchar(30)
)
Create Table State
(
StateId Int Primary Key,
CountryId Int Foreign Key References Country(CountryId),
State Varchar(30)
)
Create Table City
(
CityId Int,
StateId Int Foreign Key References State(StateId),
City Varchar(30)
)
Insert Into Country Values(101,'India')
Insert Into Country Values(102,'USA')
Insert Into Country Values(103,'Pakistan')
Insert Into State Values(1001,101,'Tamil Nadu')
Insert Into State Values(1002,101,'Kerala')
Insert Into State Values(1003,101,'Kasmir')
Insert Into State Values(2001,102,'WC')
Insert Into State Values(2002,102,'NY')
Insert Into State Values(2003,102,'CA')
Insert Into State Values(3001,103,'Punjap')
Insert Into State Values(3002,103,'WP')
Insert Into State Values(3003,103,'NP')
Insert Into City Values(11,1001,'Ch')
Insert Into City Values(12,1001,'Dg')
Insert Into City Values(21,1002,'Pal')
Insert Into City Values(22,1002,'Tri')
Insert Into City Values(31,1003,'Jammu')
Insert Into City Values(32,1003,'Manali')
Insert Into City Values(41,2001,'DC')
Insert Into City Values(42,2001,'WC Old')
Insert Into City Values(51,2002,'ny1')
Insert Into City Values(52,2002,'ny2')
Insert Into City Values(61,2003,'C1')
Insert Into City Values(62,2003,'C2')
Insert Into City Values(71,3001,'p1')
Insert Into City Values(72,3001,'p2')
Insert Into City Values(81,3002,'w1')
Insert Into City Values(82,3002,'w2')
Insert Into City Values(91,3003,'n1')
Insert Into City Values(92,3003,'n2')
Select * From Country
Select * From State
Select * From City

Contact Form

Name

Email *

Message *