Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP:NET
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 23:50pm
rosren529
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
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
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