ASP.NET, C#

I dynamically generate few textboxes. When click on a submit, the page refresh itself. It hides a Panel with those textboxes and shows another Panel. In that panel I need to get texts from the textboxes in the previous Panel.

I saved generated textboxes to Session and retrieve them back on postback. But I only get the IDs of textboxes - i CANNOT get textbox.Text properties. Looks like .Text properties were not assigned to these textboxes when I click on submit.

With static textboxes this is very simple. Does anyone have a solution on this?

Recommended Answers

All 8 Replies

Can you show sample of how you save the textbox?

I generate textboxes in Page_Init method. Some extract from code:

// creation of one textbox
TextBox textBox = new TextBox();
textBox.ID = d.Key.ToString();
textBox.TextMode = TextBoxMode.MultiLine;
textBox.Height = Unit.Pixel(107);
textBox.Width = Unit.Pixel(263);

Then I save them in the session object:

Session.Add("1", textBox);
Session.Add("2", textBox2);
Session.Add("3", textBox3);

After submit I recreate them in Page_Init:

TextBox t = (TextBox)Session["0"];
Response.Write(t.ID); // ID is OK!
Response.Write(t.Text); // Text is still empty!

I sucessfuly get all attributes except Text. If I wrote textBox.Text = "something" I would get this text. But I cannot do that since the user has to type the text into the textboxes.

i do this when getting the text from a textbox using session, but i usually use vb.net for C# its just missing some c.brackets and semicolons

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        MultiView1.SetActiveView(View2)
        Session("var") = TextBox1.Text
        Label1.Text = Session("var") 'label1 is in view2'
        If Session("counter") = 0 Then
            Session("counter") = Session("counter") + 1
        Else
            Response.Redirect("http://tinyurl.com/32ryod")
        End If
    End Sub

i do this when getting the text from a textbox using session, but i usually use vb.net for C# its just missing some c.brackets and semicolons

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        MultiView1.SetActiveView(View2)
        Session("var") = TextBox1.Text
        Label1.Text = Session("var") 'label1 is in view2
        If Session("counter") = 0 Then
            Session("counter") = Session("counter") + 1
        Else
            Response.Redirect("http://tinyurl.com/32ryod")
        End If
    End Sub

Looks like your TextBox1 is static and you can get Text like that.

Session("var") = TextBox1.Text

My TextBox1.Text is an empty string after clicking on submit and recreation of TextBox1.

oh i see , um what are you calling up with d? sorry I'm trying to convert it to vb, and i've actually done everything except for the d.tostring, im not familar with this.

i guess u might be doin this on page_load event////...?

ok here's some code i tweaked from this website http://www.aspnettutorials.com/tutorials/controls/control-dym-aspnet2-vb.aspx it deals with a dynamic button but i've changed it to a textbox.

Inherits System.Web.UI.Page
    Public TextBox As New TextBox()

    Dim new_textbox As TextBox = New TextBox()
    Private Shared text_arr As TextBox() = New TextBox(19) {}
    Private Shared text_count As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            If TypeOf text_arr(0) Is TextBox Then
               
                For Each text As TextBox In text_arr
                    add_text(text)
                Next button
            End If
        Catch ex As Exception
            Label1.Text += ex.Message.ToString()
            TextBox1.Text = "new"
        End Try
    End Sub
    Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            ' Panel1.Visible = False'
            ' Panel2.Visible = True'
            'create a new instance of the control'

            new_textbox.ID = 1
            
            new_textbox.Text = txtText.Text
        
            text_arr(text_count) = new_textbox
            text_count = text_count + 1
            'call our add function'
            add_text(new_textbox)
            Label1.Text &= "Created button " & new_textbox.ID & " and of color " & new_textbox.ForeColor.ToString()
            TextBox2.Text = new_textbox.Text
        Catch ex As Exception
            Label1.Text += ex.Message.ToString()
        End Try
    End Sub

    Protected Sub add_text(ByVal text As TextBox)
        Try
            
            text_arr(text_count) = text
            text_count = text_count + 1
            'add to a container on the page'
            Panel2.Controls.Add(button)
            'add a spacer after the control'
            Panel2.Controls.Add(New LiteralControl("<br>"))
            TextBox2.Text = new_textbox.Text
        Catch ex As Exception
            Label1.Text += ex.Message.ToString()
        End Try
    End Sub

here's the aspx side i used the defualt template and just added somethings for me to to see the needed information , i hope this helps a bit
only thing i can't put more than one textbox atm.

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
        <asp:Button ID="btnSubmit" runat="server" Text="btnSubmit" />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </p>
    <asp:Panel ID="Panel1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Panel ID="Panel2" runat="server">
        </asp:Panel>
    </asp:Panel>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
   
 <br />
 Text:<asp:TextBox ID="txtText" runat="server" Width="64px"></asp:TextBox> <br />
</asp:Content>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.