English 中文(简体)
Dynamic creation of ASP.NET Form Elements
原标题:

I m trying to build a form which generates itself as it is used. I have created a really simplistic example, loosely related to what I m trying to do below, but which demonstrates the problem.

The user types a word in the text box, clicks the Button and a new TextBox is loaded into a Panel, with the value in the original TextBox that the user has entered. The user should then be able to type something else/the same and create another text box with that in it by clicking the button, basically permitting 0,1,..,n textboxes appearing above the "txtFeeder" TextBox on the form.

The problem is that everytime you click the button, it doesn t add a new control, it seems to just update the one that has already been created with the new (incremental) ID. I m not sure if I m doing something wrong, or if what I m trying to do can t be done (which I find hard to believe)?

Here s the .aspx...

<form id="frmMain" runat="server">
    <asp:Panel ID="pnlAdded" runat="server"></asp:Panel>
    <asp:TextBox ID="txtFeeder" runat="server"></asp:TextBox>
    <asp:Button ID="btnFeedPanel" runat="server" Text="Button" />
</form>

...and here s the .aspx.vb...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ViewState.Add("elementCount", 0)
    End If
End Sub
Protected Sub btnFeedPanel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFeedPanel.Click
    ViewState("elementCount") += 1
    Dim txtNew = New TextBox
    txtNew.ID = "txtElement" & ViewState("elementCount")
    txtNew.Text = txtFeeder.Text
    pnlAdded.Controls.Add(txtNew)
    txtNew = Nothing
End Sub

Thanks

最佳回答

On PostBack you need to explicitly regenerate the buttons from ViewState (you check the added counter you have in the viewstate and regenerate the added buttons) - otherwise they ll be gone (and only the original one will appear, as you re experiencing).

Have a look at this question, the guy is trying to achieve smt extremely similar to what you re looking for (maintaining a bunch of dynamic buttons and regenerating them on postback).

问题回答

Controls that are added to a page dynamically are not automatically retained between form posts. The control itself isn t preserved in the page s view state. I think you ll need to rebuild all of the previously added fields every time a postback occurs.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签