简体   繁体   English

在回发中获取客户端HTML更改

[英]Get client side HTML changes on postback

I want to add rows to a table with javascript, but I also want to be able to find out what those rows are on postback. 我想使用javascript将行添加到表中,但是我也希望能够找出这些行在回发时的内容。 Is there a way to do that? 有没有办法做到这一点?

I also want to be able to populate the original rows in the table from the server (I'm thinking with a repeater). 我还希望能够从服务器填充表中的原始行(我在考虑使用中继器)。 Is it still possible to do that? 还有可能这样做吗?

That's not much of a description but I think that covers it... 这不是很多描述,但我认为涵盖了...

The code currently looks something like this 该代码目前看起来像这样

<table id="myTable">
<tr> <td> some static row </td> </tr>
<asp:repeater id="rptTest" runat="server">
<HeaderTemplate>
    <tr class="dgheader">
        <th> head1 </th>
        <th> head2 </th>
        <th></th>
    </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr class="<%# (Container.ItemIndex%2 == 0) ? "dgitem" : "dgalternatingitem" %>">
        <td><%# Eval("val1") %> </td>
        <td><%# Eval("val2") %> </td>
        <td><a class="dgdeletebutton" href="javascript:delete(this)"></a></td>
    </tr>
</ItemTemplate>
</asp:repeater>
</table>

At the moment all I'm wondering is how, server side, I can get a version of the table that has whatever changes I made client side. 目前,我想知道的是,在服务器端,我如何获得具有对客户端所做的任何更改的表的版本。

In order to get any information from a client in the manner you describe, you need to include a field in your form submit. 为了以您描述的方式从客户那里获取任何信息,您需要在表单提交中包含一个字段。

You will probably want hidden(s) field. 您可能需要隐藏字段。 Any time you add a row, either add a hidden field for each value you want to capture (such as val1 and val2) or have one hidden field, and when you add a row, append the information you want to the existing row. 每次添加行时,要么为要捕获的每个值(例如val1和val2)添加一个隐藏字段,要么有一个隐藏字段,然后在添加行时,将所需信息附加到现有行上。

I would warn against posting straight html, you probably only need the values not the full markup, and you most likely don't want to sanitize the html and parse it for the information you want. 我警告不要发布直接的html,您可能只需要值而不是完整的标记,并且您很可能不想清理html并解析所需的信息。

So to get you a head start you can add hidden inputs: 因此,为使您早日入门,您可以添加隐藏的输入:

<tr class="<%# (Container.ItemIndex%2 == 0) ? "dgitem" : "dgalternatingitem" %>">
    <input type="hidden" name="Row[1].val1" value="myvalue" />
    <td><%# Eval("val1") %> </td>
    <input type="hidden" name="Row[1].val2" value="myvalue" />
    <td><%# Eval("val2") %> </td>
    <td><a class="dgdeletebutton" href="javascript:delete(this)"></a></td>
</tr>

You can then get the submitted values on the backend: 然后,您可以在后端获取提交的值:

HttpContext.Current.Request.Form["Row[1].val1"]

This is from memory, the line above might not be correct. 这是来自内存,上面的行可能不正确。

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

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