Click to See Complete Forum and Search --> : cloneNode
Evilernie
08-09-2007, 06:34 PM
Hi,
Is there a cloneNode method in .NET?
If not, do you know of any code to share?
If still no, I guess I'll have to code it myself. I promise to post it when I'm done.
Thanks,
Evilernie
08-30-2007, 03:06 PM
Hi,
I got that to work, I even have a neat reusable class.
But my problem now is that everything dynamically created disappears on postback. It's irrelevant to display only screens, I actually prefer it that ways so that I don't have to worry about removing the extra controls created previously.
I used it for a User Entry form, but sadly they were no longer available on postback. Since the nodes are available on the client page, I can make javascript do it, but I'm looking for a scriptless client-side solution.
.NET have a few "register" methods. Are those the key to solving my problem?
Please help. For now, I'll try MSDN.
Thanks,
lmf232s
08-30-2007, 03:34 PM
When do you create your dynamically created content?
Does this include textboxs and controls of this nature?
Evilernie
08-30-2007, 07:39 PM
Server-side and yes, input, textarea etc.
As mentioned, they were available during creation, but disappeared on postback. I am guessing that it's probably as simple as it is not being registered into the event variables. But haven't figured out how to do that.
lmf232s
08-31-2007, 09:26 AM
When you dynamically create controls from server side you need to recreate them on each post back.
Also when you dynamically create a control from server side you must create the control on the Page_Init sub so that the controls will maintain view state.
If you create a control in the page_Load sub, say a textbox and enter some text into that textbox click a button and try to write the value of the textbox to the screen, it will either throw an error or it will return a null (Not exaclty sure on that but I know its one or both).
But by creating the control in the page_init sub you are creating the control before view state is applied so that when viewstate is applied it can find the control and you can now access the value of that control somewhere else in code.
Again I think this is your problem. Here is an example.
This code shows the control being created in the page_init sub and then a button fires a postback. In both cases we get the value of the textbox and write it to the screen.
'ASPX CODE
<asp:Panel ID="pnlContent" runat="server">
</asp:Panel>
<asp:Button ID="cmdPostBack" runat="server" Text="Click Me" />
'CODE BEIND CODE
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'Create TextBox
Dim tb As New TextBox
tb.ID = "txtFirstName"
'Add Textbox to panel control
Me.pnlContent.Controls.Add(tb)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Response.Write("Page Load Value : " & CType(Me.pnlContent.FindControl("txtFirstName"), TextBox).Text & "<BR>")
Else
Response.Write("Post Back Value : " & CType(Me.pnlContent.FindControl("txtFirstName"), TextBox).Text & "<BR>")
End If
End Sub
Protected Sub cmdPostBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPostBack.Click
Response.Write("Button Click Event <BR>")
End Sub
To see what happens when you create the control on page load take the code from the page_init sub and place it in the page_load. Notice that this will no longer have viewstate. But notice it will always be there because we create the control in the page load regardless of if its a post back or page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tb As New TextBox
tb.ID = "txtFirstName"
Me.pnlContent.Controls.Add(tb)
If Not Page.IsPostBack Then
Response.Write("Page Load Value : " & CType(Me.pnlContent.FindControl("txtFirstName"), TextBox).Text & "<BR>")
Else
Response.Write("Post Back Value : " & CType(Me.pnlContent.FindControl("txtFirstName"), TextBox).Text & "<BR>")
End If
End Sub
Now add the same code to create the textbox and add it in the first part of the if statement. This will run fine on page load but on a post back will cause an error when we try to write the value of the control because the control does not exists. If you remove the code to write the value of the control on the post back you wont get an error but youll notice the control is gone.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim tb As New TextBox
tb.ID = "txtFirstName"
Me.pnlContent.Controls.Add(tb)
Response.Write("Page Load Value : " & CType(Me.pnlContent.FindControl("txtFirstName"), TextBox).Text & "<BR>")
Else
Response.Write("Post Back Value : " & CType(Me.pnlContent.FindControl("txtFirstName"), TextBox).Text & "<BR>")
End If
End Sub
Man I hope I was not just rambling to ramble. I hope this is the problem you are having or at least sheds some light on it.
Let me know.
Evilernie
08-31-2007, 05:55 PM
It certainly is and thanks for the info.