如何在asp.net中添加控件时动态单击按钮?
我想动态添加控件
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:
下面code是,当我第一次,单击Add按钮它是正确添加控件,但问题如果我关闭浏览器窗口,并再次来到,然后单击添加按钮,我得到了更多的控制。
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
任何想法?在此先感谢
我会分别用例子你可以尝试用自己的方式
一个想法是创建按钮的列表中,你会保存您在 btnCreateDynamic_click
An idea would be to create a list of buttons in which you'd store the buttons you created inbtnCreateDynamic_click
您可以像一个方法:
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;
}
在 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
应该从那里可以每次请求后获取保存在某个地方。
myDinamicButtonsList
should be stored somewhere from where it could be retrieved after each request.
编辑:在页面加载,你可以有这样的事情:
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..
在客户端您的按钮单击事件会导致页面回发将启动ASP.Net页面生命周期。
Your button click event at the client will cause a page postback that will start the ASP.Net Page Life-cycle.
在服务器上的按钮单击事件是 PostBackEvent
,你应该能够用同样的方法调用 CreateMyButton()
您在加载或初始化事件中。
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.