Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
populating dropdown list based on the checkbox... checked is true populating data in a one table,is checked is false populating data in a another table

thnks
Posted

in the checkbox selection changed event

C#
if (chkbox.Checked==true)
    {
           ddlCity.DataSource=datatable1;
           ddlCity.DisplayMember="Name";
           ddlCity.DataBind();
    }
else
    {
          ddlCity.DataSource=alternatedatatable;
          ddlCity.DisplayMember="Name";
          ddlCity.DataBind();
    }
 
Share this answer
 
v2
You can done this in following ways:


webform1.aspx

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="popddl.aspx.cs" Inherits="Ajax_sample.popddl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!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>
    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1"
  runat="server"></ajaxToolkit:ToolkitScriptManager>

        <asp:UpdatePanel ID="updatepanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"


                    style="top: 152px; left: 354px; position: absolute; height: 22px; width: 128px"></asp:TextBox><asp:Button ID ="Button1" runat ="server" Text ="Save" />


              <ajaxToolkit:PopupControlExtender ID="TextBox1_PopupControlExtender" runat="server" DynamicServicePath=""
                    Enabled="True" ExtenderControlID="" TargetControlID="TextBox1" PopupControlID="Panel1"
                    OffsetY="22"></ajaxToolkit: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" AutoPostBack="True" DataTextField ="Name"  OnSelectedIndexChanged  = "CheckBoxList1_SelectedIndexChanged"  >
                  </asp:CheckBoxList>

                                 </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:scriptConnectionString %>"
        SelectCommand="SELECT [Name] FROM [Table1]"></asp:SqlDataSource>

    </div>

    <p>

    <asp:Button ID ="b1" runat ="server" Text ="Save" onclick="b1_Click"
            style="top: 151px; left: 514px; position: absolute; height: 26px; width: 42px" />
    </p>

    </form>

    </body>
</html>



webform1.aspx.cs


C#
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;

namespace Ajax_sample
{
    public partial class popddl : System.Web.UI.Page
    {
        SqlConnection sqlcon = new SqlConnection("User ID=sa;Password=Mobius@123;Data Source=MOB-PERLZ-WS192;Initial Catalog=script");
        protected void Page_Load(object sender, EventArgs e)
        {

        }
       

        protected void b1_Click(object sender, EventArgs e)
        {
            sqlcon.Open();
            // string name = "";
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {

                    //Create the insert query
                    SqlCommand sqlcmd = new SqlCommand("insert into Checked_Items values('" + CheckBoxList1.Items[i].Text + "')", sqlcon);
                    sqlcmd.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand sqlcmd1 = new SqlCommand("insert into UnChecked_Items values('" + CheckBoxList1.Items[i].Text + "')", sqlcon);
                    sqlcmd1.ExecuteNonQuery();
                }
            }
            sqlcon.Close();
            Response.Write("Saved Successfully");

        }
        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;
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900