[英]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.