C#如何动态添加控件
在页面上有两个控件,一个是Button,用于触发添加事件.一个是Panel,用于装载新控件的容器.我想每点击一次button,就在button事件中,动态地往Panel添...
在页面上有两个控件,一个是Button,用于触发添加事件.一个是Panel,用于装载新控件的容器.
我想每点击一次button,就在button事件中,动态地往Panel添加一个LinkButton控件.
不知如何实现...
看了一篇文章,但还不能解决问题.
http://hi.baidu.com/snyl/blog/item/7b8f80d68a0e0c2e07088b1c.html 展开
我想每点击一次button,就在button事件中,动态地往Panel添加一个LinkButton控件.
不知如何实现...
看了一篇文章,但还不能解决问题.
http://hi.baidu.com/snyl/blog/item/7b8f80d68a0e0c2e07088b1c.html 展开
3个回答
展开全部
举个最简单的例子
比如我要不定量的上传图片
可以这样在前台添加控件
<script>
function AddAttachments()
{document.getElementById('attach').innerText = "继续添加附件";
tb = document.getElementById('attAchments'); newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'> <input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
}
function delFile(index)
{
document.getElementById('attAchments').deleteRow(index);
tb.rows.length > 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件"; }
</script>
<a id="attach" onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</a>
比如我要不定量的上传图片
可以这样在前台添加控件
<script>
function AddAttachments()
{document.getElementById('attach').innerText = "继续添加附件";
tb = document.getElementById('attAchments'); newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'> <input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
}
function delFile(index)
{
document.getElementById('attAchments').deleteRow(index);
tb.rows.length > 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件"; }
</script>
<a id="attach" onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</a>
展开全部
这是windows程序,动态添加Label控件的,你参考下。方法都是一样的。
..
public int i = 0;
public int h = 0;
..
private void button2_Click(object sender, EventArgs e)
{
//添加起始页面项
lblBeginPage = new Label[10];
lblBeginPage[i] = new Label();
lblBeginPage[i].Name = "lblBeginPage" + i.ToString();
lblBeginPage[i].Text = i + ".aspx";
lblBeginPage[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
lblBeginPage[i].Location = new Point(20, 20 + 16 * h);
lblBeginPage[i].Size = new Size(100, 16);
this.panel1.Controls.Add(lblBeginPage[i]);
h++;
i++;
}
..
public int i = 0;
public int h = 0;
..
private void button2_Click(object sender, EventArgs e)
{
//添加起始页面项
lblBeginPage = new Label[10];
lblBeginPage[i] = new Label();
lblBeginPage[i].Name = "lblBeginPage" + i.ToString();
lblBeginPage[i].Text = i + ".aspx";
lblBeginPage[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
lblBeginPage[i].Location = new Point(20, 20 + 16 * h);
lblBeginPage[i].Size = new Size(100, 16);
this.panel1.Controls.Add(lblBeginPage[i]);
h++;
i++;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个是例子,希望对你有帮助!
public partial class stock_Default : System.Web.UI.Page
{
int aaa = 1;
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
aaa = ViewState["aaa"]==null?1:int.Parse(ViewState["aaa"].ToString());
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["aaa"] = ++aaa;
for (int i = 0; i < aaa; i++)
{
LinkButton lk = new LinkButton();
lk.Text = (i).ToString();
lk.ID = lk.Text;
lk.Visible = true;
this.Label1.Controls.Add(lk);
}
}
}
public partial class stock_Default : System.Web.UI.Page
{
int aaa = 1;
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
aaa = ViewState["aaa"]==null?1:int.Parse(ViewState["aaa"].ToString());
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["aaa"] = ++aaa;
for (int i = 0; i < aaa; i++)
{
LinkButton lk = new LinkButton();
lk.Text = (i).ToString();
lk.ID = lk.Text;
lk.Visible = true;
this.Label1.Controls.Add(lk);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询