0

I have an asp.net page with C# code behind. In the asp page, I have a few asp Literals. As an example, I have a Literal called "ltrtitle".

<asp:Literal runat="server" ID="ltrtitle" />

In the code behind, I set the Text property of the literal to this:

<asp:TextBox runat="server" ID="title" TextMode="MultiLine" Columns="0" Rows="2" />

This renders a text box on the asp page for the user to enter text. When The user goes to save, I want to be able to get the Text value of the TextBox (Not the Literal). I have tried the following:

TextBox txtbxHiddenUser = (TextBox)FindControl("title");

Didn't Work. Tried:

foreach(Control c in Page.Form.Controls)
        {
            if(c.ClientID == "ltrtitle")
            {
                foreach (TextBox textbox in c.Controls.OfType<TextBox>())
                {
                    TEST3456 = textbox.Text;
                }
            }
        }

Still doesn't work. I would have thought FindControl would have worked. Any idea what I am doing wrong?

pmcs
  • 51
  • 1
  • 10
  • You can't just set the Text property of a literal to have ASPX markup and expect it to work. If you want to add additional controls, then a [PlaceHolder](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.placeholder) would be more appropriate. – mason Oct 30 '20 at 18:23
  • Thank You. I added a PlaceHolder to the aspx page and populated it in the code behind. However, In the save code, I am pulling the Text property of the TextBox, which causes the error, "The name title doesn't exist in this context." Since the TextBox isn't built till runtime, Visual Studios doesn't know it exists. How do I pull the Text Value from the TextBox loaded to the PlaceHolder? – pmcs Oct 30 '20 at 18:55
  • Why are you dynamically adding the textbox? What's your justification? Would setting the .Visible property be just as effective? Doing dynamically added controls is tricky, and best avoided if possible. – mason Oct 30 '20 at 18:57
  • The same controls might be a TextBox when viewed and a Dropdown when in Edit mode (The Legacy code is done this way). I am considering just putting the edit section in one panel and the display section in another. – pmcs Oct 30 '20 at 19:08
  • 1
    Yep - then show/hide the panels depending on which mode you are in. That will be a lot easier to code. – mason Oct 30 '20 at 19:42
  • Thank You, mason, that is what I ended up doing. – pmcs Nov 23 '22 at 19:33

1 Answers1

0

Thanks to mason answer, I ended up putting the edit section in one panel and the display section in another. I then show/hide the panels depending on which mode the page is in.

pmcs
  • 51
  • 1
  • 10