把下面c#翻译成vb.net。测试后答。在线等可加分。实现网页动态panel添加多个控件,不会因页面刷新只一个
<body><formid="form1"runat="server"><div><asp:ButtonID="button1"runat="server"Text="创...
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="button1" runat="server" Text="创建" onclick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="id2" runat="server" Text="获取所有值" onclick="id2_Click" />
</div>
</form>
</body>
后台代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//还原控件
RestoreTextBox();
}
//静态变量存储控件列表
static List<TextBox> txtlist = new List<TextBox>();
//控件id
static int id = 1;
protected void Button1_Click(object sender, EventArgs e)
{
var txt = new TextBox
{
ID=id.ToString(),
Text="Sample textbox " + id.ToString()
};
this.Panel1.Controls.Add(txt);
txtlist.Add(txt);
//id自增1
id++;
}
/// <summary>
/// 还原控件
/// </summary>
void RestoreTextBox()
{
foreach (var item in txtlist)
{
if (item != null)
{
this.Panel1.Controls.Add(item);
}
}
}
/// <summary>
/// 读取textbox的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void id2_Click(object sender, EventArgs e)
{
foreach (Control item in Panel1.Controls)
{
if (item is TextBox)
{
Response.Write((item as TextBox).Text+"</br>");
}
}
}
} 展开
<form id="form1" runat="server">
<div>
<asp:Button ID="button1" runat="server" Text="创建" onclick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="id2" runat="server" Text="获取所有值" onclick="id2_Click" />
</div>
</form>
</body>
后台代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//还原控件
RestoreTextBox();
}
//静态变量存储控件列表
static List<TextBox> txtlist = new List<TextBox>();
//控件id
static int id = 1;
protected void Button1_Click(object sender, EventArgs e)
{
var txt = new TextBox
{
ID=id.ToString(),
Text="Sample textbox " + id.ToString()
};
this.Panel1.Controls.Add(txt);
txtlist.Add(txt);
//id自增1
id++;
}
/// <summary>
/// 还原控件
/// </summary>
void RestoreTextBox()
{
foreach (var item in txtlist)
{
if (item != null)
{
this.Panel1.Controls.Add(item);
}
}
}
/// <summary>
/// 读取textbox的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void id2_Click(object sender, EventArgs e)
{
foreach (Control item in Panel1.Controls)
{
if (item is TextBox)
{
Response.Write((item as TextBox).Text+"</br>");
}
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询