[英]How to add controls dynamically when click button in asp.net?
I'm trying to add controls dynamically 我正在尝试动态添加控件
Code: 码:
AddVisaControl.ascx AddVisaControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %>
<%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
<tr>
<td class="style8"> Visa Number:</td>
<td class="style20"><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td class="style22"> Country Name:</td>
<td class="style23">
<asp:DropDownList ID="dropCountry" Width="165px" runat="server">
</asp:DropDownList></td>
</tr>
<tr>
<td class="style22"> Type of Visa:</td>
<td class="style23">
<asp:DropDownList ID="dropVisa" Width="165px" runat="server"> </asp:DropDownList></td>
<td class="style22"> Type of Entry:</td>
<td class="style23">
<asp:DropDownList ID="dropEntry" Width="165px" runat="server"> </asp:DropDownList></td>
</tr>
<tr>
<td class="style8"> Expiry Date</td>
<td class="style20">
</td>
</tr>
</table>
.cs code: .cs代码:
Below code is the problem when the first time I click add button it is adding controls properly but If I close the browser window and comes again and click the add button I got more controls 下面的代码是第一次点击添加按钮时它正在添加控件但是如果我关闭浏览器窗口并再次出现并单击添加按钮我得到更多控件的问题
static int i = 0;
protected void addnewtext_Click(object sender, EventArgs e)
{
i++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
PlaceHolder1.Controls.Add(ac);
PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));
}
}
In the below image If I click add more visa button I want to get another visa details 在下面的图像如果我点击添加更多签证按钮我想获得另一个签证详细信息
Any ideas? 有任何想法吗? Thanks in advance
提前致谢
I will show one examples you can try it your own way 我将展示一个你可以按照自己的方式尝试的例子
An idea would be to create a list of buttons in which you'd store the buttons you created in btnCreateDynamic_click
一个想法是创建一个按钮列表,您可以在其中存储您在
btnCreateDynamic_click
创建的按钮
you could have a method like: 你可以有一个像这样的方法:
private Button CreateButton(string id, string name)
{
Button b = new Button();
b.Text = name;
b.ID = id;
b.Click += new EventHandler(Button_Click);
b.OnClientClick = "ButtonClick('" + b.ClientID + "')";
return b;
}
in btnCreateDynamic_click
you could have something like: 在
btnCreateDynamic_click
你可以有类似的东西:
Button b = CreateButton("dinamicBtn"+myDinamicButtonsList.Count.ToString(),"dinamicBtn"+myDinamicButtonsList.Count.ToString());
myDinamicButtonsList.add(b);
and in the pageLoad for example you could do something like
foreach(button btn in myDinamicButtonsList){
form1.Controls.Add(btn));
}
List<Button> myDinamicButtonsList = new List<Button>();
myDinamicButtonsList
should be stored somewhere from where it could be retrieved after each request. myDinamicButtonsList
应存储在每个请求后可以检索到的位置。
EDIT: In page load you could have something like this: 编辑:在页面加载中你可以有这样的东西:
if(Session["myDinamicButtons"] == null){
List<Button> myDinamicButtonsList = new List<Button>();
Session["myDinamicButtons"] = myDinamicButtonsList;
}
foreach(Button btn in Session["myDinamicButtons"] as List<Button>){
form1.Controls.Add(btn));
}
i didn't tested it but it should work. 我没有测试它,但它应该工作。
also put on some information following may more help.. 也可以提供以下一些信息可能更有帮助..
Your button click event at the client will cause a page postback that will start the ASP.Net Page Life-cycle. 您在客户端的按钮单击事件将导致页面回发,这将启动ASP.Net页面生命周期。
Your button click event on the server is a PostBackEvent
and you should be able to use the same method call CreateMyButton()
that you used in the Load or Init events. 您在服务器上的按钮单击事件是
PostBackEvent
,您应该能够使用在Load或Init事件中使用的相同方法调用CreateMyButton()
。
if you remove the static 'i' variable and use a hidenInput to maintain the no of created controls (or a session value) you will be fine. 如果删除静态'i'变量并使用hidenInput来维护创建的控件(或会话值),则可以。
But I suggest you read the following article to find a better way of creating dynamic controls : 但我建议您阅读以下文章,以找到创建动态控件的更好方法:
Remove static int i = 0;
删除
static int i = 0;
Change the method as below 更改方法如下
protected void addnewtext_Click(object sender, EventArgs e)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
PlaceHolder1.Controls.Add(ac);
PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.