2

I'm trying to use code behind to read text from HTML provided to me. After researching this topic I found that almost all instances of this involve Web Forms controls(asp:) for the textboxes but the HTML I was given does not, but instead is:

<p>
    <label>Address</label>
    <textarea class="w3-input w3-border" name="addr" cols="30" rows="4"></textarea>
</p>
<div class="w3-half w3-container">
    <p>
        <label>Phone:</label>
        <input type="text" class="w3-input"/>
</div>
<div class="w3-half w3-container">
    <label style="padding-left:10px;">Email:</label>
    <input type="text" class="w3-input"/>
</div>
</p>

Will I still be able to read the user-provided text from these boxes or will I need to alter the HTML?

A couple of my unsuccessful code-behind attempts to extract the address supplied:

string address = ((textarea)Address.FindControl("addr")).Text;
string address = ((TextBox)Address.FindControl("addr")).Text;

Update: Using the server control described in a solution offered, I get an error message stating that "A page can have only one server-side Form tag."

This results from the following markup:

<form runat="server">
        <asp:textbox id="addr" runat="server" textmode="multiline" />
        </form>

followed later by:

<form runat="server">
            <asp:Button ID="Ship" runat="server" Text="Ship" OnClick="Ship_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
            <asp:Button ID="Rate" runat="server" Text="Rate" OnClick="Rate_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />

            </form>

The textarea is located in a different section than the buttons and I'm unclear on how to make both functional either without a form tag or without having them share the same one. Thanks

KMcL
  • 115
  • 1
  • 2
  • 7

2 Answers2

0

You need to use a server control if you're looking to access the value in code behind. Use an ASP TextBox and set the TextMode to MultiLine:

<asp:TextBox ID="textarea1" runat="server" TextMode="MultiLine" />

Then in code behind:

string addr = textarea1.Text;

UPDATE to demonstrate multiple forms on the same page:

<form ID="form1" runat="server">
    <asp:Button ID="Ship" runat="server" Text="Ship" OnClick="Ship_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
    <asp:Button ID="Rate" runat="server" Text="Rate" OnClick="Rate_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
</form>

<form id="form2" action="WebForm1.aspx" method="post">
    <asp:TextBox ID="textarea1" runat="server" TextMode="MultiLine" />
</form>

From here, you can use either method of retrieving the textarea1 value in code behind for posts from form1 or form2...

form1:

string addr = textarea1.Text;

form2:

string addr = Request["textarea1"].ToString();
mjw
  • 1,196
  • 1
  • 12
  • 19
  • The issue I run into with your suggestion is that the code you posted must be placed inside a form tag with runat="server" and I have this form tag for button function in a separate section of the web app. And apparently you can only have one of these form tags. Any idea how I can get around this issue? – KMcL Jun 14 '17 at 18:06
  • You can have more than one form, but you'll need to manually handle the form posting and value extraction if you're posting more than one form to a page. It may be useful for you to post more or all of the markup to show the forms you're talking about. – mjw Jun 14 '17 at 18:08
  • You can achieve multiple forms as shown above by removing the runat="server" from one of them. You are only allowed a single form with the runat="server" designation. – mjw Jun 14 '17 at 18:43
  • This is untrue. You can add `runat='server'` to any plain-vanilla HTML tag and access it. – Xavier J Jun 14 '17 at 18:57
  • Which part are you (incorrectly) claiming is untrue? https://stackoverflow.com/questions/7544454/can-we-use-multiple-forms-in-a-web-page – mjw Jun 14 '17 at 19:02
0

Add runat="server" to your TEXTAREA and INPUT tags. Then you can access them from code-behind. You also need to assign the ID attribute of each one.

<p>
    <label>Address</label>
    <textarea class="w3-input w3-border" name="addr" id="textarea1" runat="server" cols="30" rows="4"></textarea>
</p>
<div class="w3-half w3-container">
    <p>
        <label>Phone:</label>
        <input type="text" class="w3-input" runat="server" id="input1" />
</div>
<div class="w3-half w3-container">
    <label style="padding-left:10px;">Email:</label>
    <input type="text" class="w3-input" runat="server" id="input2" />
</div>
</p>
Xavier J
  • 4,326
  • 1
  • 14
  • 25