根据复选框填充下拉列表... [英] populating dropdown list based on the checkbox...

查看:143
本文介绍了根据复选框填充下拉列表...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据复选框填充下拉列表...选中的是在一个表中填充数据,是否选中的是在另一个表中填充数据

thnks

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

推荐答案

在复选框选择中已更改事件

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();
    }


您可以通过以下方式完成此操作:


webform1.aspx

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 %> ; " span> SelectCommand =" 从[Table1]中选择[名称]" ">< /asp:SqlDataSource > < /div > < p > < asp:Button ID =" runat =" 文本 =" onclick =" b1_Click" span> =" 顶部:151像素;左侧:514像素;位置:绝对;高度:26像素;宽度:42像素" / > < /p > < /form > < /body > < /html >
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





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


这篇关于根据复选框填充下拉列表...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆