读取 ASP.NET 代码隐藏中动态生成的 HTML 元素值

发布于 2024-10-09 14:18:49 字数 1355 浏览 5 评论 0原文

我有一个 asp.net 页面,其中有以下标记。基本上,此标记是通过从表中读取记录并循环它们来从代码隐藏生成的。对于表中的每条记录,都会有一个 div 块。

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

<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 的内部 HTML,因此我无法像在 IDE 中拖放的控件那样读取值。

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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼中杀气 2024-10-16 14:18:49

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

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

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

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.

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.

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

梦中楼上月下 2024-10-16 14:18:49

如果您想获取生成的控件的值,您必须做两件事。

  1. 为每个控件生成带有 runat='server' 标记的输入控件(否则它们将不会包含在 Request.Forms 集合中。)这可能是您缺少的步骤。

  2. 在回发时从 Request.Form 集合中获取您的值

    string txtValue1 = Request.Form["txtName1"];

确实应该这么简单。我使用 DIV 作为容器和简单的 javascript 将控制字符串注入到 innerHTML 中,对您的代码进行了测试。如果您遇到任何问题,您可能需要进行调试并查看动态控件 ID 是否由于将它们插入命名容器或其他内容而发生了更改。

If you want to grab your values for the generated controls you have to do 2 things.

  1. Generate the input controls with a runat='server' tag for each control (otherwise they will not be included in the Request.Forms collection.) This is probably the step your missing.

    <input type='text' id='txtName1' runat='server' value='something' />

  2. Grab your values from the Request.Form collection on postback

    string txtValue1 = Request.Form["txtName1"];

It really should be that easy. I tested this against your code using a DIV as the container and a simple javascript to inject the control string into the innerHTML. If your getting any issues you may have to debug and see if the dynamic control ID has changed due to inserting them into naming container or something.

痴意少年 2024-10-16 14:18:49

因此,最重要的问题是,当您在 Page_Init 之后动态添加控件时,POSTBACK 值无法插入回这些控件中。

CF:http://www.15seconds.com/issue/020102.htm 和 < a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms178472.aspx< /a>

这里的一些其他答案建议“哦,将 runat=server 添加到控件中”,但是当您在代码隐藏中而不是在 Page_Init 中创建它时,那么这会产生零差异。

如果这不是您创建控件的方式,或者这不是您使用它们的方式,请告诉我,我将修改此答案以了解更多详细信息。这实际上一切都归结为您如何尝试访问这些值。

So the brunt of the story is that when you dynamically add a control after Page_Init then POSTBACK values can not be inserted back into those controls.

CF: http://www.15seconds.com/issue/020102.htm and http://msdn.microsoft.com/en-us/library/ms178472.aspx

Some of the other answers here suggest "oh, add a runat=server to the control" but when you create it in the codebehind, and not in the Page_Init, then that makes ZERO difference.

Let me know if that's not how you're creating the controls or if that's not how you're using them and I'll revise this answer on more details. It really all boils down to how you're trying to access the values.

难理解 2024-10-16 14:18:49

通常,您会将动态创建的输入控件(在本例中为文本框)放置在面板控件(容器)之类的内容中。然后,在用户发布数据后,您将循环该容器 panel.Controls 集合并检索每个 TextBox 文本。

请注意,在使用动态创建的控件时需要注意一些注意事项,因为 ASP 具有无状态性质。

本页展示了如何实现这一点:

使用文本框在 ASP.Net GridView 控件中添加动态行

Generally, you'd place those input controls you're creating dynamically (in this case, a TextBox) inside something like a panel control (the container). Then after the user has posted their data, you'd loop that container panel.Controls collection and retrieve each TextBox text.

Be aware that some caveats apply when working with dynamically created controls because ASP is of stateless nature.

This page shows how to implement this:

Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes

我乃一代侩神 2024-10-16 14:18:49

我没有测试它,但我可以建议:

将带有 runat="server" 标记的动态控件添加到另一个带有 runat="server" 的控件(例如面板控件)中。然后你可以像这样访问它们:

Textbox t = (Textbox)panel1.controls.findControl("dynamicControlId");

I didn't test it but I can suggest that:

Add your dynamic controls with runat="server" tag inside another controls with runat="server"(such as panel control). Then you can access them like this:

Textbox t = (Textbox)panel1.controls.findControl("dynamicControlId");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文