简体   繁体   English

从JavaScript设置runat =“ server”

[英]Set runat=“server” from JavaScript

Is there a way to set the runat attribute from client-side JavaScript? 有没有办法从客户端JavaScript设置runat属性? I need to add rows to a table after the page is loaded, and I need to make their cells' data available to the server. 页面加载后,我需要在表中添加行,并且需要使服务器的单元格数据可用。 I am OK with making the first (couple of) row(s) runat="server" , but I do not know how many rows the user will want, so it is not just a matter of adding a bunch of hidden rows and showing them when the user clicks on "Add New Row" instead. 我可以让第一(两对)行运行runat="server" ,但是我不知道用户想要多少行,所以这不仅仅是添加一堆隐藏行并显示的问题当用户单击“添加新行”时,显示它们。

I can think of a couple ways to do this: 我可以想到几种方法来做到这一点:

  1. Postback to the page every time the user clicks the "Add New Rows" button", and handle the runat attribute on the server instead of in JavaScript/jQuery. I do not want to do this, but as it seems like the only surefire way, I might have to. 每次用户单击“添加新行”按钮时回发到页面,并在服务器上而不是在JavaScript / jQuery中处理runat属性。我不想这样做,但似乎是唯一的保证方式,我可能必须这样做。

  2. Add runat="server" to the cells with jQuery, then reference them the next time I do a postback, but do nothing with them currently. 使用jQuery将runat="server"添加到单元格中,然后在下次执行回发时引用它们,但是当前对它们不执行任何操作。 This would be the ideal solution for me, but I'm not sure if ASP.NET works this way. 这对我来说是理想的解决方案,但是我不确定ASP.NET是否以这种方式工作。 Considering WebForms was created before JavaScript developers got down pat the idea that we could add elements to a page after the page is loaded , I'm not holding my breath. 考虑到WebForms是在JavaScript开发人员下手之前创建的,因此我们可以在页面加载后向页面中添加元素,这一点我没有屏息。

  3. Give the table or maybe tbody the runat attribute, then refer to its children somehow. tabletbodyrunat属性,然后以某种方式引用其子级。 This would also be better than #1, but I'm also not sure how to do this, or even if it can indeed be done. 这也将比#1更好,但是我也不确定如何做到这一点,或者即使确实可以做到。

Here is a simplified version of what I'd like to do: 这是我想做的事情的简化版本:

<table runat="server" id="this_table">
    <tbody runat="server" id="this_table_body">
        <tr>
            <td class="this_table_cell">Some Content</td>
            <td><button type="button" class="this_table_button">Please click this button</button>
        </tr>
    </tbody>
</table>

And a simplified version of the jQuery: 以及jQuery的简化版本:

$("#<%=this_table_body.ClientID%>").on("click", ".this_table_button", function() {
    $("<%=this_table_body.ClientID%>").append("<tr>
            <td class="this_table_cell">Some Content</td>
            <td><button type="button" class="this_table_button">Please click this button</button>
        </tr>
    );
});

This doesn't make any sense because changes to the DOM aren't pushed back up to the server when the page is posted back. 这没有任何意义,因为在页面回发时,不会将对DOM的更改回推回服务器。 Adding attributes that mean absolutely nothing to a browser (such as runat ) won't have any effect. 向浏览器添加绝对没有意义的属性(例如runat )不会有任何效果。 Even if your table is set to runat="server" , client-side changes to that table aren't detected by the server. 即使您将表设置为runat="server"runat="server"也不会检测到对该表的客户端更改。

Option #1 (postback every time a new row is added) is perhaps the easiest method, and basically what ASP.NET was designed to do since version 1.0 (before web-apps became popular) 选项#1(每次添加新行时回发)也许是最简单的方法,并且基本上是ASP.NET从1.0版开始设计的功能(在Web应用变得流行之前)

For a more dynamic experience, what you'll need to do is store your client-side changes locally, then post them back up to the server via a standard form element, such as a hidden input field. 为了获得更动态的体验,您需要做的是在本地存储客户端更改,然后通过标准表单元素(例如隐藏的输入字段)将其发布回服务器。 You can add any arbitrary number of hidden fields to a <form> element, and read them on the server using the Request object. 可以将任意数量的隐藏字段添加到<form>元素,并使用Request对象在服务器上读取它们。 Or, you could perhaps serialize the new row data via JSON or XML. 或者,您也许可以通过JSON或XML序列化新行数据。 If you're simply trying to save new rows in a database, perhaps implement a Save button that sends a single new row to the server via AJAX and immediately saves it. 如果您只是想在数据库中保存新行,则可以实现“ 保存”按钮,该按钮通过AJAX将单个新行发送到服务器并立即保存。

runat="server"

...is a server directive parameter which means nothing to the browser. ...是服务器指令参数,对浏览器没有任何意义。 The server wires up any methods based on this directive before rendering the page to HTML, and sending it to the Client browser. 在将页面呈现为HTML并将其发送到客户端浏览器之前,服务器会根据此指令连接所有方法。 By the time JS can make the change to the element, and set runat="server" the server will not be able to receive the directive. 到JS可以更改元素并设置runat="server" ,服务器将无法接收指令。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM