动态创建DropDownList并实现互动和跳转的web用户控件

动态创建DropDownList并实现互动和跳转的web用户控件

2006-05-15 09:03

以下代码主要供参考:怎样动态创建控件,并怎样添加控件的事件以及获得动态创建的控件的值。不足之处还请大家指出!

=================typeUc.ascx 代码部分======================

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="typeUC.ascx.cs" Inherits="include_typeUC"%>
<script language="javascript" type="text/javascript">
   
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="4" id="TABLE1" runat="server">
  <tr>
    <td id="TD1" runat="server">
        &nbsp;<asp:DropDownList ID="lstProvince" runat="server" AutoPostBack="True" OnSelectedIndexChanged="lstProvince_SelectedIndexChanged" Width="70px">
            <asp:ListItem Value="0">请选择</asp:ListItem>
            <asp:ListItem Value="1">广东</asp:ListItem>
            <asp:ListItem Value="2">北京</asp:ListItem>
            <asp:ListItem Value="3">浙江</asp:ListItem>
            <asp:ListItem Value="4">上海</asp:ListItem>
            <asp:ListItem Value="5">香港</asp:ListItem>
            <asp:ListItem Value="6">天津</asp:ListItem>
            <asp:ListItem Value="7">江苏</asp:ListItem>
            <asp:ListItem Value="8">重庆</asp:ListItem>
            <asp:ListItem Value="9">河北</asp:ListItem>
            <asp:ListItem Value="10">福建</asp:ListItem>
            <asp:ListItem Value="11">辽宁</asp:ListItem>
            <asp:ListItem Value="12">黑龙江</asp:ListItem>
        </asp:DropDownList>
        &nbsp; &nbsp; &nbsp;
      <input type="submit" name="Submit" value="确定" id="Submit1" runat="server" onserverclick="Submit1_ServerClick"/></td>
  </tr>
</table>

===================typeUc.ascx.cs 代码部分======================

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class include_typeUC : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write("totalDListNum:"+totalDListNum+"<br>"); //测试时使用
    }

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
        //
        InitializeComponent();
        base.OnInit(e);
        this.initA();
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.lstProvince.SelectedIndexChanged += new System.EventHandler(this.lstProvince_SelectedIndexChanged);
        this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    //创建数据库连接
    SqlConnection conn = DBconn.CreateConn();
    DataSet DS;//多级互动下拉列表框的数据源
    SqlDataAdapter sda;
    int totalDListNum;//储存应该动态创建的DropDownList的数目

    public void initA()
    {
        //计算应该动态创建的DropDownList级数
        string sql = "select max(l_ord) from types";
        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        totalDListNum = (int)cmd.ExecuteScalar();
        conn.Close();

        //开始创建并绑定数据
        DS = new DataSet();
        sda = new SqlDataAdapter();
        for (int i = 1; i <= totalDListNum; i++)
        {
            sda.SelectCommand = new SqlCommand("select id,name,p_id,l_ord,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12 from types where l_ord=" + i+" or l_ord="+(i+1), conn);
            sda.Fill(DS, "type" + i);

            DropDownList DD = new DropDownList();
            if (i < totalDListNum)
            {
                DD.AutoPostBack = true;
            }
            DD.ID = "type"+i;
            DD.DataSource = DS.Tables["type" + i];
            DD.DataTextField = "name";
            DD.DataValueField = "id";
            DD.DataBind();
            DD.Items.Clear();
            DD.SelectedIndexChanged += new EventHandler(this.DD_SelectedIndexChanged);
            DD.Items.Add(new ListItem("-请选择-","-1"));
            this.TD1.Controls.AddAt(i+1,DD);
        }

    }

    protected void DD_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList iDD=(DropDownList)sender;
        EventArgs arg=(EventArgs)e;
        for (int i = 1; i < totalDListNum; i++)
        {
            if (iDD.ID == "type" + i)
            {
                DropDownList iiDD = (DropDownList)this.TD1.FindControl("type" + (i + 1));
                if (Convert.ToInt32(iDD.SelectedValue) > -1)
                {
                    DS.Tables["type" + (i + 1)].DefaultView.RowFilter = "p_id=" + iDD.SelectedValue;
                    iiDD.DataBind();
                    iiDD.Items.Insert(0, new ListItem("-请选择-", "-1"));
                }
                else
                {
                    iiDD.Items.Clear();
                    iiDD.Items.Add(new ListItem("-请选择-","-1"));
                }
                for (int j = i; j < (totalDListNum-1); j++)
                {
                    //Response.Write("j"+j+"<br>"); //测试时使用
                    DropDownList iiiDD = (DropDownList)this.TD1.FindControl("type" + (j+2));
                    iiiDD.Items.Clear();
                    iiiDD.Items.Add(new ListItem("-请选择-","-1"));
                }

            }
        }
    }

    protected void lstProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Response.Write(this.lstProvince.SelectedValue); //测试时使用
        if (this.lstProvince.SelectedValue != "0")
        {
            DropDownList iDD = (DropDownList)this.TD1.FindControl("type1");
            DS.Tables["type1"].DefaultView.RowFilter = "p_id=0 and v" + this.lstProvince.SelectedValue + ">0";
            iDD.DataBind();
            for (int i = 1; i <= totalDListNum; i++)
            {
                DropDownList iiDD = (DropDownList)this.TD1.FindControl("type" + (i + 1));
                if (iiDD != null)
                {
                    iiDD.Items.Clear();
                    iiDD.Items.Add(new ListItem("-请选择-", "-1"));
                }
            }
            iDD.Items.Insert(0, new ListItem("-请选择-", "-1"));
        }
        else
        {
            for (int i = 1; i <= totalDListNum; i++)
            {
                DropDownList iDD = (DropDownList)this.TD1.FindControl("type" + i);
                if (iDD != null)
                {
                    iDD.Items.Clear();
                    iDD.Items.Add(new ListItem("-请选择-", "-1"));
                }
            }
        }
    }

    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("csdata/auto/index.aspx?id="+GetUrl());
    }

    private string GetUrl()
    {
        string path = "172";
        for (int i = 2; i <= totalDListNum; i++)
        {
            DropDownList iDD = (DropDownList)this.TD1.FindControl("type" + i);
            if (iDD.SelectedValue == "-1")
            {
                i = totalDListNum; //跳出循环
            }
            else
            {
                //path = path +","+ iDD.SelectedValue;
                path = iDD.SelectedValue;
            }
        }
        return path;
    }
}

转载于:https://www.cnblogs.com/heartstill/archive/2011/05/31/2064928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值