動態建立DropDownList並實現互動和跳轉的web使用者控制項

來源:互聯網
上載者:User
文章目錄
  • 動態建立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 Form設計器產生的程式碼
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
        //
        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;
    }
}

分享到搜狐微博

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.