Click here to Skip to main content
12,945,066 members (67,704 online)
Rate this:
 
Please Sign up or sign in to vote.
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 25-Sep-12 22:50pm
rosren579
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

in the checkbox selection changed event

if (chkbox.Checked==true)
    {
           ddlCity.DataSource=datatable1;
           ddlCity.DisplayMember="Name";
           ddlCity.DataBind();
    }
else
    {
          ddlCity.DataSource=alternatedatatable;
          ddlCity.DisplayMember="Name";
          ddlCity.DataBind();
    }
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You can done this in following ways:


webform1.aspx

<%@ 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


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;
        }
    }
}
  Permalink  
v2

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,781
CHill60 3,135
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100