none
动态产生dropdownlist asp.net C# RRS feed

  • 问题

  • 我想做这样一个效果,在一个dropdownlist(服务器控件)选中好之后,就会在后面继续新增一个dropdownlsit让用户继续选中,一直选到用户停为止。

    我的做法是在panel中不断new dropdownlist 但是由于不断刷新,看起来很糟糕。

    有什么办法可以不断新增又不会刷新??

    本人新手,javascript很差劲

    2013年7月31日 7:18

答案

  • Public Class WebForm1
        Inherits System.Web.UI.Page
     
        '静态变量,保存下拉列表对象
        Shared dropDownLists As  List(Of DropDownList)
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Not IsPostBack) Then
              dropDownlists = New List(Of DropDownList)
            Else
            If (HiddenField1.Value = "") Then
                Dim ddr As New DropDownList
                ddr.Items.Add("1")
                ddr.Items.Add("2")
                ddr.Items.Add("3")
                ddr.AutoPostBack = True
                AddHandler ddr.SelectedIndexChanged, AddressOf DropDownList1_SelectedIndexChanged
                dropDownLists.Add(ddr)
            End If
            For Each ddr In dropDownLists
                UpdatePanel1.ContentTemplateContainer.Controls.Add(ddr)
            Next
            HiddenField1.Value = ""
        End Sub
     

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年8月2日 2:16
    版主

全部回复

  • 用AJAX技术——把整个Panel扔到UpdatePanel中看看。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年7月31日 8:03
    版主
  • 能不能举个例子啊?不太会
    2013年7月31日 8:08
  • <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="aspnet页面的生命周期演示.WebForm5" %>
    
    <!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>
        <script runat="server">
            void AddControl(object sender, EventArgs e)
            {
                DropDownList d = new DropDownList();
                d.Items.Add("1");
                d.Items.Add("2");
                d.Items.Add("3");
                d.SelectedIndexChanged += new EventHandler(AddControl);
                this.UpdatePanel1.ContentTemplateContainer.Controls.Add(d);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="AddControl">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                    </asp:DropDownList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>
    为什么我这样写了,总是只加一个dropdownlist??加不第二个??
    2013年7月31日 8:34
  • using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    public class WebForm1 : System.Web.UI.Page
    {

     //静态变量,保存下拉列表对象
     static List<DropDownList> dropDownLists = new List<DropDownList>();

     private void ShowAllDropdownlists()
     {
      foreach (object ddr_loopVariable in dropDownLists) {
       ddr = ddr_loopVariable;
       UpdatePanel1.ContentTemplateContainer.Controls.Add(ddr);
      }
     }

     protected void Page_Load(object sender, System.EventArgs e)
     {
      if ((IsPostBack)) {
       DropDownList ddr = new DropDownList();
       ddr.Items.Add("1");
       ddr.Items.Add("2");
       ddr.Items.Add("3");
       ddr.AutoPostBack = true;
       ddr.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;
       dropDownLists.Add(ddr);
       foreach ( ddr in dropDownLists) {
        UpdatePanel1.ContentTemplateContainer.Controls.Add(ddr);
       }
      }
     }

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
     {

     }
     public WebForm1()
     {
      Load += Page_Load;
     }
    }
    2013年7月31日 9:22
    版主
  • 大侠,为什么我这样写,按了按钮之后还会增加dropdownlist

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="动态生成3.WebForm2" %>
    
    <!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:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Button ID="Button1" runat="server" Text="read text" OnClick="Button1_Click" />
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace 动态生成3
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
                    DropDownList ddl = new DropDownList();
                    ddl.Items.Add("1");
                    ddl.Items.Add("2");
                    ddl.Items.Add("3");
                    ddl.AutoPostBack = true;
                    ddl.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
                    list.Add(ddl);
                    foreach (DropDownList temp in list)
                    {
                        UpdatePanel1.ContentTemplateContainer.Controls.Add(temp);
                    }
            }//end  page  load
    
    
            /// <summary>
            /// 存储dropdownlist的数组
            /// </summary>
            static List<DropDownList> list = new List<DropDownList>();
           
    
    
    
    
            /// <summary>
            /// 选择之后就新增
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = new DropDownList();
                ddl.Items.Add("1");
                ddl.Items.Add("2");
                ddl.Items.Add("3");
                ddl.AutoPostBack = true;
                ddl.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
                list.Add(ddl);
            }
    
    
            /// <summary>
            /// 读出所有文本
            /// </summary>
            /// <returns></returns>
            private string ReadText()
            {
                string text = "";
                foreach (DropDownList temp in list)
                {
                    text += temp.SelectedValue.ToString();
                }
                return text;
            }
    
    
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                this.TextBox1.Text = ReadText();
            }
            
    
    
    
    
        }//end class
    }

    2013年8月1日 1:47
  • 那当然——因为按钮和下拉列表都会引发页面回发事件。

    代码如下:

    Public Class WebForm1     Inherits System.Web.UI.Page     '静态变量,保存下拉列表对象     Shared dropDownLists As New List(Of DropDownList)     Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load         If (IsPostBack AndAlso HiddenField1.Value = ""Then             Dim ddr As New DropDownList             ddr.Items.Add("1")             ddr.Items.Add("2")             ddr.Items.Add("3")             ddr.AutoPostBack = True             AddHandler ddr.SelectedIndexChanged, AddressOf DropDownList1_SelectedIndexChanged             dropDownLists.Add(ddr)         End If         For Each ddr In dropDownLists             UpdatePanel1.ContentTemplateContainer.Controls.Add(ddr)         Next         HiddenField1.Value = ""     End Sub     Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As ObjectByVal e As EventArgsHandles DropDownList1.SelectedIndexChanged             End Sub     Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgsHandles Button1.Click     End Sub
    End Class

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
     
    <!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>Test</title>
        <script type="text/javascript">
            function CheckButtonRaised() {
                document.getElementById("HiddenField1").value = "Button";
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
        
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                    </asp:DropDownList>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="CheckButtonRaised()" />
                    <asp:HiddenField ID="HiddenField1" runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>
        
        </div>
        </form>
    </body>
    </html>
    

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年8月1日 3:02
    版主
  • 还剩下最后一个问题:

    因为dropDownLists 是静态变量,

    所有如果不清除解决方案然后重新编译的话,就会保持上一次的数量,

    我要怎么该才使得下次运行的时候,把他的数量清零????


    2013年8月1日 9:29
  • Public Class WebForm1
        Inherits System.Web.UI.Page
     
        '静态变量,保存下拉列表对象
        Shared dropDownLists As  List(Of DropDownList)
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Not IsPostBack) Then
              dropDownlists = New List(Of DropDownList)
            Else
            If (HiddenField1.Value = "") Then
                Dim ddr As New DropDownList
                ddr.Items.Add("1")
                ddr.Items.Add("2")
                ddr.Items.Add("3")
                ddr.AutoPostBack = True
                AddHandler ddr.SelectedIndexChanged, AddressOf DropDownList1_SelectedIndexChanged
                dropDownLists.Add(ddr)
            End If
            For Each ddr In dropDownLists
                UpdatePanel1.ContentTemplateContainer.Controls.Add(ddr)
            Next
            HiddenField1.Value = ""
        End Sub
     

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年8月2日 2:16
    版主
  • 谢谢,你太厉害了
    2013年8月2日 2:45