简体   繁体   English

asp.net :( c#client-side)如何访问页面加载后创建的html元素?

[英]asp.net: (c# client-side) how to access html element created after page loads?

Imagine this, 想象一下,

Step 1: ASPX Page Loads. 第1步:ASPX页面加载。
Step 2: Button fires a script that creates a html element (div, span, etc) with an id or class tag, including runat server attribute. 第2步:按钮触发一个脚本,该脚本创建一个带有id或class标记的html元素(div,span等),包括runat服务器属性。

and my problem is, 我的问题是,

Final Step: From my C# file, how to access that element and get it's inner html, so I can save it to a string? 最后一步: 从我的C#文件,如何访问该元素并获取它的内部html,所以我可以将它保存为字符串?

PS: i'll use that string to save it in my mssql database. PS:我将使用该字符串将其保存在我的mssql数据库中。

You cannot create a "real" runat=server element/control without doing a full postback to the server. 如果不对服务器执行完全回发,则无法创建“真实”的runat = server元素/控件。

The best approach may be to write some script that stores the innerHTML into an ASP.Net hidden field right before you submit the page. 最好的方法可能是在提交页面之前编写一些将innerHTML存储到ASP.Net隐藏字段的脚本。 You can then access the value of this hidden field to grab the data. 然后,您可以访问此隐藏字段的值以获取数据。

If you're wanting to dynamically create multiple objects, you'll need to use standard html hidden input fields instead, since you can't create asp.net server controls via javascript. 如果您想动态创建多个对象,则需要使用标准的html隐藏输入字段,因为您无法通过javascript创建asp.net服务器控件。

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

You'll then be able to access these hidden fields from the Request.Form object: 然后,您就可以从Request.Form对象访问这些隐藏字段:

Request.Form["fieldData_1"]

Knowing this, you can now iterate over the form data and process all of your dynamic fields 知道了这一点,您现在可以迭代表单数据并处理所有动态字段

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

It is also possible to avoid using hidden fields all together and just pass your data to the server directly using the __doPostback('', '') method. 也可以避免一起使用隐藏字段,只需使用__doPostback('','')方法直接将数据传递给服务器。 This could be implemented in many different ways, so I'll just refer you to http://dopostback.net to read up on how the method works. 这可以通过许多不同的方式实现,因此我将引用您http://dopostback.net来了解该方法的工作原理。

runat="server"属性添加到标记以使其成为HTML Control,并且可以通过其ID从.cs文件访问

I think you need to post the element inner HTML back to the server. 我认为你需要将元素内部HTML发布回服务器。 What I would do, in a client function, get the inner HTML of the newly created element, store it in a hidden field, then call __doPostBack('<hidden element id>', ''); 我会做什么,在客户端函数中,获取新创建元素的内部HTML,将其存储在隐藏字段中,然后调用__doPostBack('<hidden element id>', '');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用客户端验证来处理 c# 中的 asp.net mvc? - How to use client-side validation to work on your asp.net mvc in c#? ASP.NET MVC-在客户端JS中访问C#字符串 - ASP.NET MVC - Accessing C# string in client-side JS 在C#ASP.NET Core 2.2的客户端上实现简单的验证 - Implementing simple validation on the client-side in C# ASP.NET Core 2.2 如何从 asp.net 中的 java 脚本代码(客户端)访问隐藏字段值 c# - How to access hidden field value from java script code(Client side) in asp.net c# 从服务器端ASP.NET C#访问从客户端创建的缓存变量 - Accessing Cache Variable created from client side from the Server Side ASP.NET C# ASP.net:传递动态创建的(通过客户端javascript)值以及回发 - ASP.net: pass dynamically created (by client-side javascript) value along with postback 在服务器端ASP.Net上访问客户端阵列 - Accessing Client-Side arrays on Server Side ASP.Net 从 ASP.NET Webforms 中的页面级别调用 UserControl 内部的客户端函数 - Calling a client-side function that is inside of a UserControl from the Page level in ASP.NET Webforms 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? ASP.NET双列表框使用JQuery更新了客户端,以及如何在服务器端检索结果 - ASP.NET dual listboxes updated client-side with JQuery, and how to retrieve the results server-side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM