天天看點

動态為DropDownList添加Item

先看示範:

動态為DropDownList添加Item

Insus.NET在MasterPage放置了一個DropDownList和一個Button控件,然後在Default.aspx放置一個TextBox。從上面的動畫中可以看到,在TextBox輸入文字,它會動态添加至MasterPage的DropDownList控件中。

實作過程,首先寫一個接口:

動态為DropDownList添加Item
動态為DropDownList添加Item

IGetable

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

/// <summary>

/// Summary description for IGetable

/// </summary>

namespace Insus.NET

{

    public interface IGetable

    {

        string TextBoxValue

        {

            get;

        }

    }

}

接下來,建立MasterPage:

InsusMasterPage.master:

動态為DropDownList添加Item
動态為DropDownList添加Item

InsusMasterPage.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="InsusMasterPage.master.cs" Inherits="InsusMasterPage" %>

<!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>

        <asp:DropDownList ID="DropDownList1" runat="server" Width="100">

        </asp:DropDownList>

        <asp:Button ID="Button1" runat="server" Text="Add Item" OnClick="Button1_Click" />

        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>

    </div>

    </form>

</body>

</html>

InsusMasterPage.master.cs代碼,重點部分已經有注釋,難度應該不大:

動态為DropDownList添加Item
動态為DropDownList添加Item

InsusMasterPage.master.cs

using System.Web.UI;

using System.Web.UI.WebControls;

using Insus.NET;

public partial class InsusMasterPage : System.Web.UI.MasterPage

   //Insus.NET:宣告一個泛型,将存儲輸入的資料

    List<string> list = new List<string>();

    protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)

            Data_Binding();

    private void Data_Binding()

        this.DropDownList1.DataSource = list;

        this.DropDownList1.DataBind();

    protected void Button1_Click(object sender, EventArgs e)

        //Insus.NET:把page轉為接口,并取屬性

        string v = ((IGetable)this.Page).TextBoxValue;

       //Insus.NET:判斷Session是否為空

        if (Session["db"] != null)

           //Insus.NET:如果不為空,直接從Session取出

            list = (List<string>)Session["db"];

        else

            //Insus.NET:如果為空,new一個執行個體

            list = new List<string>();

       //Insus.NET:把輸入值加入泛型

        list.Add(v);

        //Insus.NET:存入Session

        Session["db"] = list;

        Data_Binding();

最後,是Aspx的代碼:

Default.aspx:

動态為DropDownList添加Item
動态為DropDownList添加Item

View Code

<%@ Page Title="" Language="C#" MasterPageFile="~/InsusMasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</asp:Content>

Default.aspx.cs:

動态為DropDownList添加Item
動态為DropDownList添加Item

public partial class _Default : System.Web.UI.Page,IGetable

    //Insus.NET:實作接口

    public string TextBoxValue

        get {

            return this.TextBox1.Text;

好了,非常簡單的例子。

繼續閱讀