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)
{
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>
}