ASP.net 在按下按钮时动态添加控件。回发问题

发布于 2024-11-04 16:16:05 字数 758 浏览 1 评论 0原文

我有一个包含多个按钮的用户控件,根据按下的按钮,不同的控件会添加到页面中(假设按钮 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-11-11 16:16:05

因为您是动态执行此操作,所以您需要一种存储您正在执行的操作的方法,以便服务器可以使用每个 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 use Session, store the data in the ViewState (which will persist with the page regardless of time). Create a List<YourControlObjects> and make sure it is Serializable and then store it in the ViewState. You probably want to store the control type, location, etc. so that you can rebuild it on Page_Load each time there is a PostBack.

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.

乜一 2024-11-11 16:16:05

我个人会使用 ListView 来处理这个问题。使用 Visible=false 将您需要的所有控件包含在 ItemTemplate 中,并将它们绑定到存储在视图状态中的小列表。以编程方式设置行数据绑定上可见的正确控件。
请注意,在重新绑定之前,您必须在控件中收集数据并将其保存在列表中。

I personally would handle this using a ListView. Include all of the controls you need in the ItemTemplate with Visible=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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文