设置RUNAT ="服务器"从JavaScript [英] Set runat="server" from JavaScript

查看:128
本文介绍了设置RUNAT ="服务器"从JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来设置从客户端JavaScript的 RUNAT 属性?我需要的页面加载后行添加到一个表,我需要让自己的细胞的数据提供给服务器。我与使第(情侣)行(S) =服务器 OK,但我不知道用户将有多少行想要的,所以它是不只是加入一些隐藏的行,并显示他们当用户点击的问题添加新行代替。

我能想到的几种方法可以做到这一点:


  1. 回发到每一个用户点击添加新行按钮,并处理服务器,而不是在JavaScript中的 RUNAT 属性时的页面/ jQuery的。我并不想这样做,但它似乎是唯一正确的方式,我可能不得不这样做。


  2. =服务器添加到使用jQuery的细胞,那么下一次我做了回发引用它们,却无可奈何与他们目前。这对我来说是理想的解决方案,但我不知道,如果ASP.NET以这种方式工作。考虑到JavaScript开发人员之前创建的WebForms有拍下来,我们可以添加元素到页面的想法的加载页面后的,我不是抱着我的呼吸。


  3. 输入或者 TBODY RUNAT 属性,然后参照其子不知何故。这也将优于#1,但我也不能确定如何做到这一点,或者即使它确实是可以完成的。


下面是想我做的一个简化版本:

 <表=服务器ID =this_table>
    < TBODY =服务器ID =this_table_body>
        &所述; TR>
            < TD类=this_table_cell>有些内容与LT; / TD>
            < TD><按钮式=按钮级=this_table_button>请点击该按钮< /按钮>
        < / TR>
    < / TBODY>
< /表>

和jQuery的的简化版本:

  $(#<%= this_table_body.ClientID%GT;)。在(点击,.this_table_button功能(){
    $(<%= this_table_body.ClientID%GT;)。追加(< TR>
            < TD类=this_table_cell>有些内容与LT; / TD>
            < TD><按钮式=按钮级=this_table_button>请点击该按钮< /按钮>
        < / TR>
    );
});


解决方案

这没有任何意义,因为更改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.

这篇关于设置RUNAT =&QUOT;服务器&QUOT;从JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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