ASP.net 在按下按钮时动态添加控件。回发问题
我有一个包含多个按钮的用户控件,根据按下的按钮,不同的控件会添加到页面中(假设按钮 1 添加一个文本框,按钮 2 添加一个标签)。
我的代码大致如下:
protected void but1_click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.ID = "tb1";
paramsCtrlDiv.Controls.Add(tb);
}
protected void but2_click(object sender, EventArgs e)
{
Label lb = new Label();
lb.ID = "lb1";
paramsCtrlDiv.Controls.Add(lb);
}
然后我有第三个按钮(button3)来获取页面上的所有控件及其值。 (假设在此示例中每个按钮仅单击一次)。
我的问题是当按下button3时,paramsCtrlDiv.controls数组不包含已添加的控件。我知道我需要在每次回发的 Page_Load 时间添加这些控件。我的问题是,因为我不确切知道用户添加了哪些控件,所以我不知道要添加什么 Page_Load (可能有一个文本框,然后是标签,只是一个标签或只是一个 tb),我可以不控制用户按下的内容。
我知道我可以存储会话中的所有内容,但我不确定这是一个优雅的解决方案。不同选项卡上还可以有此控件的多个实例,因此每个实例都必须正确维护其自己的控件集合
I have a user control which contains several buttons, depending on the button pressed a different control is added to the page (lets say button 1 adds a TextBox, button2 adds a label).
I have code along the lines of:
protected void but1_click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.ID = "tb1";
paramsCtrlDiv.Controls.Add(tb);
}
protected void but2_click(object sender, EventArgs e)
{
Label lb = new Label();
lb.ID = "lb1";
paramsCtrlDiv.Controls.Add(lb);
}
I then have a third button (button3) to get all controls on the page and their values. (Assume each button is only clicked once for this example).
My problem is when button3 is pressed the paramsCtrlDiv.controls array doesn't contain the controls that have been added. I know I need to add these controls at Page_Load time on each postback. My issue is as I don't know exactly what controls the user has added I don't know what I want to add a Page_Load (there could be a text box, then label, just a label or just a tb), I can't control what the user presses.
I know I could store everything in the session but I'm not sure this is an elegant solution. There can also be several instances of this control on different tabs so each one has to correctly maintain it's own control collection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您是动态执行此操作,所以您需要一种存储您正在执行的操作的方法,以便服务器可以使用每个
PostBack
重新创建它。如果您不想使用Session
,请将数据存储在ViewState
中(无论时间如何,该数据都会保留在页面中)。创建一个List
并确保它是Serialized
,然后将其存储在ViewState
中。您可能想要存储控件类型、位置等,以便每次发生PostBack
时都可以在Page_Load
上重建它。问题归结为您需要为这些动态创建的控件维护自己的状态。这只是一个建议,但您可以通过多种不同的方式来做到这一点。
Because you are doing this dynamically, you need a way of storing what your are doing so the server can recreate it with each
PostBack
. If you don't want to useSession
, store the data in theViewState
(which will persist with the page regardless of time). Create aList<YourControlObjects>
and make sure it isSerializable
and then store it in theViewState
. You probably want to store the control type, location, etc. so that you can rebuild it onPage_Load
each time there is aPostBack
.The problem comes down to you needing to maintain your own state for these dynamically created controls. This is just one suggestion but you could do this many different ways.
我个人会使用
ListView
来处理这个问题。使用Visible=false
将您需要的所有控件包含在ItemTemplate
中,并将它们绑定到存储在视图状态中的小列表。以编程方式设置行数据绑定上可见的正确控件。请注意,在重新绑定之前,您必须在控件中收集数据并将其保存在列表中。
I personally would handle this using a
ListView
. Include all of the controls you need in theItemTemplate
withVisible=false
and bind them to a small list stored in the viewstate. Programatically set the correct control visible on row databind.Note you will have to collect your data in the controls and save it in your list before you rebind it.