Asp.net: (C # client-side) how to access the html element created after the page loads?

Imagine,

Step 1: Download the ASPX pages.
Step 2: the button starts a script that creates an html element (div, span, etc.) with an id or class tag, including a runat server attribute.

and my problem is:

Final step: from my C # file, how to access this element and get its internal html so that I can save it in a line?

PS: I will use this line to save it in my mssql database.

+4
source share
3 answers

You cannot create a β€œreal” runat = server element / control without doing a full postback to the server.

A better approach would be to write some script that stores innerHTML in a hidden ASP.Net field right before the page is submitted. You can then access the value of this hidden field to capture data.

If you want to dynamically create multiple objects, you need to use the standard html hidden input fields, since you cannot create asp.net server controls through javascript.

<input type="hidden" name="fieldData_1" value="control 1 html content"> <input type="hidden" name="fieldData_2" value="control 2 html content"> 

Then you can access these hidden fields from the Request.Form object:

 Request.Form["fieldData_1"] 

Knowing this, you can now iterate over form data and process all your dynamic fields

 foreach (string fieldData in Request.Form) { if(fieldData.Contains("fieldData_"){ //process the data for each field } } 

You can also avoid using hidden fields and simply transfer your data to the server directly using the __doPostback ('', '') method. It can be implemented in different ways, so I just refer you to http://dopostback.net to read how this method works.

+6
source

Add the runat="server" attribute to the tag to make it an HTML control, and can be accessed from a .cs file by its identifier

0
source

I think you need to publish the internal HTML element back to the server. What would I do, in the client function, get the internal HTML of the newly created element, save it in a hidden field, and then call __doPostBack('<hidden element id>', '');

0
source

Source: https://habr.com/ru/post/1412315/


All Articles