积极答复者
动态产生dropdownlist asp.net C#

问题
答案
-
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:45
全部回复
-
用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 -
<%@ 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??加不第二个?? -
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;
}
}- 已编辑 ThankfulHeartModerator 2013年7月31日 9:25
- 已标记为答案 Jason Dot WangModerator 2013年8月1日 1:38
- 取消答案标记 Jason Dot WangModerator 2013年8月1日 1:40
-
大侠,为什么我这样写,按了按钮之后还会增加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 }
-
那当然——因为按钮和下拉列表都会引发页面回发事件。
代码如下:
Public Class WebForm1 Inherits System.Web.UI.Page '静态变量,保存下拉列表对象 Shared dropDownLists As New List(Of DropDownList) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles 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 Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles 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 -
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:45