在ASP.NET中的代码隐藏中读取动态生成的HTML元素值 [英] Reading dynamically generated HTML element value in codebehind in ASP.NET

查看:59
本文介绍了在ASP.NET中的代码隐藏中读取动态生成的HTML元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net页面,上面有以下标记。基本上,此标记是从代码隐藏生成的,方法是从表中读取记录并遍历它们。对于表中的每个记录,将有一个div块。

I have an asp.net page where I have the below markup. Basically this markup is generated from codebehind by reading records from a table and looping through them. For each record in table, there will be a div block.

基本上,此表单用于读取/显示用户设置。设置条目存储在表格中。

Basically this form is to read/show settings for a user. The settings entries are stored in a table.

<div id='divContainer' runat='server'>

 <div id='div1' runat='server'>
    <table>
      <tr>
        <th>Name</th>
        <td><input type='text' id='txtName1' value='something' /></td>
      </tr>
       </table>
 </div>
 <div id='div2' runat='server'>
    <table>
      <tr>
        <th>Domain name</th>
        <td><input type='text' id='txtName2' value='something' /></td>
      </tr>
     </table>
 </div>
 <div id='div3' runat='server'>
    <table>
      <tr>
        <th>URL</th>
        <td><input type='text' id='txtName3' value='something' /></td>
      </tr>
      </table>
 </div>
 <div id='div4' runat='server'>
    <table>
      <tr>
        <th>Some other value is enabled ?</th>
        <td><input type='checkbox' id='chk4'  /></td>
      </tr>
      </table>
 </div>

</div>

每个输入元素的ID都是唯一的。现在在代码隐藏中,我想读取每个输入元素的值以保存用户所做的更改。我如何在这里阅读内容?由于标记是在代码背后以字符串形式生成的,并附加了外部div的INNER HTML,所以我无法像在IDE中拖放控件那样读取值。

The id's of each input element will be unique. Now in codebehind I want to read the values of each input element to save the changes user made. How can I read the elements here? Since the mark up is generated in codebehind as a string and appended the the INNER HTML of the external div, I can't read values like we do for a control which we drag and drop in the IDE.

推荐答案

如果这些是通过标准HTTP POST发送回页面的,则可以读取 Request.Form NameValueCollection

If these are being sent back to the page in a standard HTTP POST then you can read the values in the Request.Form NameValueCollection.

从本质上讲,所有变成表单元素的服务器控件都会像您一样转换为标准HTML表单元素在那里,尽管.NET生成了更多标记来帮助识别它们。然后,它会在回发时自动将它们的值映射回服务器控件,但这些值本身仍处于标准的HTTP POST中,您可以手动访问。

Essentially, all of the server controls that become form elements get translated into standard HTML form elements just as you have there, albeit with more markup generated by .NET to help it identify them. Then it automatically maps their values back to the server controls for you on the postback, but the values themselves are still just in a standard HTTP POST, which you can access manually.

(这也是从一个ASP .NET应用程序向另一个应用程序发布表单时常用的方法。)

(This is also a common method used when posting a form from one ASP .NET application to another.)

这篇关于在ASP.NET中的代码隐藏中读取动态生成的HTML元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆