动态创建ASP.NET表单控件 [英] Dynamically creating ASP.NET form controls

查看:84
本文介绍了动态创建ASP.NET表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其基于在先前页中给出的答案,可以在字段的组合约10种不同的变体(大部分是相同的,但几个变化)。我决定,而不是让10单独的页面,我会尽量做到动态。最终,这将拉动从数据库的形式设置,但现在我只是想获得有活力的部分工作。下面code有点儿工作,但它给了我一个奇怪的结果。

I have a form which, based on the answers given in the prior page, can have about 10 different variations in the combination of fields (most are the same, but several change). I decided rather than making 10 separate pages, I would try to make it dynamic. Eventually this will pull the form setup from a database, but for now I'm just trying to get the dynamic part to work. The following code kinda works, but it's giving me a weird result.

private void AddTestControls()
{
    var newbox = new TextBox();
    newbox.ID = "FirstBox";
    newbox.Text = "This is dynamic";
    newbox.CssClass = "stepHeader";
    DynamicDiv1.Controls.Add(newbox);

    var newlit = new Literal();
    newlit.ID = "FirstLit";
    newlit.Text = ".<br/>.";
    DynamicDiv1.Controls.Add(newlit);

    newbox.ID = "SecondBox";
    newbox.Text = "This is also dynamic";
    newbox.CssClass = "step";
    DynamicDiv1.Controls.Add(newbox);

}

我已经通过它加强与所有属性越来越设置正确,但当页面呈现最后,只有 SecondBox 控件是可见的。这里没有 FirstBox 的痕迹。如果我改变它,这样 SecondBox 是自己的对象( newebox2 为例),那么无论是可见的,但如何我在想,我最终会做的形式从数据库中,这可能使事情变得复杂。我不明白为什么文本框对象,以便将它添加到div的集合控件重新创建。我要对这个都错了,或者只是缺少一个步骤的地方?

I've stepped through it and all the properties are getting set correctly, but when the page finally renders, only the SecondBox control is visible. There is no trace of the FirstBox. If I change it so that SecondBox is its own object (newebox2 for example) then both are visible, but with how I was thinking that I would ultimately do the form from the database, this could complicate things. I don't understand why the textbox object has to be recreated in order to add it to the Div's collection of controls. Am I going about this all wrong, or just missing a step somewhere?

推荐答案

您SecondBox被覆盖FirstBox newbox ,因为它仍持有对它的引用。创建一个新的文本框第二框:

Your "SecondBox" are overwriting the "FirstBox" newbox since it's still holding a reference to it. Create a new TextBox for the second box:

var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);

var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);

// Create a new TextBox
var secondBox = new TextBox();
secondBox.ID = "SecondBox";
secondBox.Text = "This is also dynamic";
secondBox.CssClass = "step";
DynamicDiv1.Controls.Add(secondBox);

我不明白为什么这可能复杂的事情,但你可以做的是创建一个方法来创建一个文本框,如果这是更简单:

I'm not quite sure why this could complicate things, but what you could do is create a method for creating a textbox, if that's easier:

TextBox CreateTextBox(string id, string text, string cssClass)
{
    var box = new TextBox();
    box.ID = id;
    box.Text = text;
    box.CssClass = cssClass;
    return box;
}

然后

var newBox = CreateTextBox("FirstBox", "This is dynamic", "stepHeader");
DynamicDiv1.Controls.Add(newBox);

这篇关于动态创建ASP.NET表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆