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>

No comments:

Post a Comment

Contact Form

Name

Email *

Message *