这是我在stackoverflow中的第一个问题,如果有任何错误,缺少信息等,请致歉。如果有,请通知我。
我有一个asp.net页面,当点击LiteralControl
(名为addMoreButton)时,需要动态添加一组TextBox
,DropDownList
和Button
。我使用ViewState来保存所需的值。
protected void addMoreButton_Click(object sender, EventArgs e)
{
//The rowAmount is already initialized outside the scope of this function. It is incremented by 1 and will be used to determine the Textbox and DropDownList IDs
rowAmount++;
//The viewState ArrayList would be added to the ViewState
//For each row of webcontrols (literalcontrols, textbox and dropdownlist) added, an ArrayList newState will be added to viewState
ArrayList viewState;
ArrayList newState = new ArrayList();
//If the ViewState is empty, an empty ArrayList will be initialized to be added later
if (ViewState["pageViewState"] == null)
{
viewState = new ArrayList();
}
//If it isn't, the ArrayList from the existing viewState will be retrieved. Additional values will be added.
else
{
viewState = (ArrayList)ViewState["pageViewState"];
}
//Before displaying, the necessary texts for the LiteralControl are stored into a variable as they also need to be stored into a viewstate
string text1 = "Unit of item sold:  ";
string text2 = "  of amount: ";
string text3 = "  with price per unit of:  ";
string separator = "<br />";
//The texts and the rowAmount are added to the newState ArrayList
//Different datatypes are used, which will be explained in the next code block.
newState.Add(text1);
newState.Add((float)rowAmount);
newState.Add(text2);
newState.Add(rowAmount);
newState.Add(text3);
newState.Add((double)rowAmount);
newState.Add(separator);
//The newState ArrayList is added to the viewState ArrayList
viewState.Add(newState);
//The final viewState ArrayList is added to the ViewState
ViewState.Add("pageViewState",viewState);
}
现在,我已经尝试通过将控件添加到名为rowPanel的Panel
来动态创建控件。
protected void Page_PreRender(object sender, EventArgs e)
{
//Getting the viewState
ArrayList viewState = (ArrayList)ViewState["pageViewState"];
//The page will be populated with the dynamically created controls if the ViewState isn't null.
if (viewState != null)
{
//viewState is an ArrayList containing an ArrayList...
foreach (ArrayList i in viewState)
{
//...and the ArrayList contains items of several data types, different data types create different controls.
foreach (object o in i)
{
//String = LiteralControl
if (o.GetType().ToString() == "System.String")
{
rowPanels.Controls.Add(new LiteralControl(o.ToString()));
}
//Integer = textbox
else if (o.GetType().ToString() == "System.Int32")
{
TextBox newBox = new TextBox();
//Setting the properties
newBox.Text = "amountTextBox" + o.ToString();
newBox.ID = "amountTextBox" + o.ToString();
newBox.Width = 28;
//Adding the textbox into rowPanels
rowPanels.Controls.Add(newBox);
}
//Single = DropDownList
else if (o.GetType().ToString() == "System.Single")
{
DropDownList newDDList = new DropDownList();
newDDList.ID = "unitDropDownList" + o.ToString();
//Adding the textbox into rowPanels
rowPanels.Controls.Add(newDDList);
}
//Double = another TextBox
else if (o.GetType().ToString() == "System.Double")
{
TextBox newBox = new TextBox();
newBox.ID = "priceTextBox" + o.ToString();
newBox.Width = 90;
//Adding the textbox into rowPanels
rowPanels.Controls.Add(newBox);
}
}
}
}
}
为了测试,我添加了一个带有OnClick事件的按钮,如下所示
protected void Button1_Click(object sender, EventArgs e)
{
Debug.Print(rowPanels.Controls.Count.ToString());
}
添加几行控件并执行上面的Button1_Click后,调试器只显示1,尽管页面上显示了动态添加的控件。
另一方面,如果我在Page_Load而不是Page_PreRender中执行动态控件添加,则调试器将返回正确的值。但是,我必须按两次addMoreButton才能显示(这是我切换到Page_PreRender的主要原因)
我知道问题是由页面生命周期引起的,但是我无法弄清楚在生命周期阶段需要进行动态添加以便正确检索控件的值。有人可以帮我解决这个问题吗?提前致谢。
编辑:我在rowPanel中打印控件的数量,仅用于测试。在实际使用中,我想要做的是检索每个动态创建的下拉列表和文本框中的值。
我将再次尝试使用ViewState。我完成后会发布结果。