且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置RUNAT ="服务器"从JavaScript

更新时间:2022-10-15 11:21:03

这没有任何意义,因为更改DOM都没有的推回的到服务器时的页面发布背部。并称,意思是绝对没有到浏览器的属性(如 RUNAT )将不会有任何效果。即使你的表设置为 =服务器,不是由服务器检测到表中的客户端的变化。

选项#1(回传每次添加一个新行时间)也许是最简单的方法,基本上什么ASP.NET设计做到,因为1.0版本(前网络应用开始流行)

有关更动感的体验,你需要做的是存储您的客户端的本地更改,然后通过一个标准的表单元素后他们备份到服务器上,比如一个隐藏的输入字段。您的可以的添加任意数量隐藏字段到<形式> 元素,并使用请求对象。或者,你可以或许通过JSON或XML序列化的新行数据。如果你只是想的保存的新行数据库中,也许实施的保存的按钮发送一个新行通过AJAX服务器,并立即将其保存。

Is there a way to set the runat attribute from client-side JavaScript? 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.

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.

  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. This would be the ideal solution for me, but I'm not sure if ASP.NET works this way. 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.

  3. Give the table or maybe tbody the runat attribute, then refer to its children somehow. 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.

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:

$("#<%=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. Adding attributes that mean absolutely nothing to a browser (such as runat) won't have any effect. Even if your table is set to runat="server", client-side changes to that table aren't detected by the 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)

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. Or, you could perhaps serialize the new row data via JSON or 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.