Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The problem I'm having is in ASP.NET I'm assigning data to p-tag innerText from JQuery when the form loads, and that's working fine. But then, in Code Behind, when I try to reference the same innerText of the same p-tags, they all come into the Code Behind as empty strings, even though the data is displaying fine on the form. I am using runat="server" in the p-tags.

Note also that, just for testing, I tried hard-coding sine text into p-tag pPlaceId like this:
<p id="pPlaceId">This is a test.</p>

and if hard-coded this way, it is retrieved successfully in the Code-Behind.

Here is the top line of the HTML/JavaScript page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MyAppName.MyWebForm" %>

What can I do get the p-tag contents that is assigned and not hard-coded to be accessible in the Code-Behind?

What I have tried:

I have this JQuery code setting innerText of three p-tags in ASP.NET. It's working perfectly and populates three p-tags from localStorage when the form loads.

<script>
        $(document).ready(function () {
            document.getElementById("pPlaceId").innerText = localStorage.getItem("Map_PlaceId");
            document.getElementById("pPlaceName").innerText = localStorage.getItem("Map_PlaceName");
            document.getElementById("pPlaceAddress").innerText = localStorage.getItem("Map_PlaceVicinity");
        });
</script>


I have a Container div tag that contains a form tag that specifies runat="server", and I have within the form, these three p-tags located within div row and div col elements:

<p id="pPlaceId" runat="server"></p>
<p id="pPlaceName" runat="server"></p>
<p id="pPlaceAddress" runat="server"></p>


When the form loads, the p-tags are populated with data as expected. The page acts as a confirmation page. It displays the three pieces of info that were sent and provides two button, Submit and Cancel.

When I click the Submit button, it successfully executes the Click handler in the Code Behind, no problem there. But in the Click handler, retrieval of the innerText of the p-tags fails. Each one comes in as an empty string.

Code Behind

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //Read the connection string from Web.Config file
    string ConnectionString = ConfigurationManager.ConnectionStrings["HCCS"].ConnectionString;
    oLL = new LocLogin();
    oLL.PersonID = Convert.ToInt32(this.Session["UserID"].ToString());
    try
    {
        oLL.PlaceID = pPlaceId.InnerText;
        oLL.LocName = pPlaceName.InnerText;
        oLL.LocAddress = pPlaceAddress.InnerText;

if(oLL.PlaceID != null)
{
    <Code to serialize data>
}
    }
    catch ( Exception ex)
    {
        string exception1 = ex.InnerException.ToString();
    }
        <Other code>
    }
<Other code>

}
Posted
Updated 18-Jul-20 10:39am
v5

1 solution

Think about how HTML forms work... when you submit the form, whichever way you do it, the browser will only POST back form field contents (plus headers of course). So if it's not in a form field, you can't interrogate it on the server. (Sure, ASP.Net includes a few extra form fields of its own, as hidden fields, but they're still form fields.)
You have a couple of options:

1. In client-side submit code (i.e. Javascript), copy the inner HTML of your <p> tag into a hidden form field, so it gets sent back to the server where you can interrogate it
2. Use an <input type=text> field instead of a <p> tag; use CSS styling to make it look and behave like a <p> tag (you might need to use a <textarea> instead) and to make it readonly so the user can't directly modify it. Then the content will get sent back as part of the form submission and you can access it server side.

If you ever get stuck on this sort of stuff, then use the browser dev tools to inspect the code to see what's really happening. In this instance if you look at the network traffic you'll see that only the form fields get sent back (thank goodness!) and you'd have been able to work out the two suggestions above.
 
Share this answer
 
v2
Comments
Member 12824529 12-Jul-20 2:51am    
Thank you for your answer. Actually I do have form tags with runat="server" specified. I've tried so many things. I tried text boxes (which don't display the data), p-tags and label tags (that each do display the data), and hidden field tags added. All of them return empty strings in the Code Behind in spite of specifying runat="server". It's extremely frustrating. Any other ideas on what condition might not be right that could cause this kind of issue? Thanks.
DerekT-P 12-Jul-20 5:29am    
Also, I still think you don't fully understand Webforms; it doesn't matter adding "runat=server" to a p or label tag; these are NOT form fields so the browser will NOT send them back to the server!!
DerekT-P 12-Jul-20 5:27am    
Without seeing your full code, no. There are any number of issues that could cause problems (though personally I've never encountered a form field being empty server-side when it shouldn't be). Things you think are irrelevant and therefore you've not shown us may be the cause. I take it you've used your browser's network inspection tool to verify the data is actually being sent back in the form data??
Member 12824529 18-Jul-20 19:38pm    
Derek,
I updated my question. I had already moved away from using p tags, but they were still mentioned in the head question; but now, I copied ASP.NET form code for a login form and, not evendoing anything with code behind, I can't even assign text string values in a jQuery Ready function that will display in the textboxes on the form.
The new question is at (https://www.codeproject.com/Questions/5274150/Why-do-text-assignments-to-textboxes-in-jquery-fai).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900