我已经为Dropdownlost和Textbox创建了动态控件但它无法正常工作 [英] I have created the Dynamic control for Dropdownlost and Textbox But it is not working properly
问题描述
C#文件如下
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
if(DropDownList1.SelectedValue ==DDL)
{
DropDownList DDL = new DropDownList();
DDL.ID =DDL1;
DDL.Items.Add(Rajkot);
DDL.Items.Add(Amdavad);
DDL.Items.Add(Surat);
DDL.Items.Add(Baroda);
PlaceHolder1.Controls.Add( DDL);
}
else if(DropDownList1.SelectedValue ==TB)
{
TextBox TB = new TextBox();
TB。 ID =TB1;
PlaceHolder1.Controls.Add(TB);
}
}
}
ASPX文件如下:
C# file is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "DDL")
{
DropDownList DDL = new DropDownList();
DDL.ID = "DDL1";
DDL.Items.Add("Rajkot");
DDL.Items.Add("Amdavad");
DDL.Items.Add("Surat");
DDL.Items.Add("Baroda");
PlaceHolder1.Controls.Add(DDL);
}
else if(DropDownList1.SelectedValue=="TB")
{
TextBox TB = new TextBox();
TB.ID = "TB1";
PlaceHolder1.Controls.Add(TB);
}
}
}
ASPX File is as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<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" AutoPostBack="True">
<asp:ListItem Text="Please Select" Value="-1"> </asp:ListItem>
<asp:ListItem Text="Enter city" Value="DDL"></asp:ListItem>
<asp:ListItem Text="Enter Pincode" Value="TB"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
请告诉我我在哪里ong。
Please tell me where I am wrong.
推荐答案
通过它工作不正常你的意思是新的控件没有出现在PlaceHolder中?也许您必须将PlaceHolder放在UpdatePanel中。
By "it is not working properly" you mean that de new control doesn't appear in the PlaceHolder? Maybe you have to place the PlaceHolder in an UpdatePanel.
这篇关于我已经为Dropdownlost和Textbox创建了动态控件但它无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!