简体   繁体   English

访问DOM以从HTML表服务器端获取值

[英]Accessing the DOM to obtain values from a HTML table Server-Side

I created a aesthetic control that creates two lists in seperate div tags organized with a table for each. 我创建了一个美观控件,该控件在单独的div标签中创建了两个列表,每个div标签都带有一个表格。 The control always the user to click on the img tag associated with each item to move it between the two lists. 控件始终是用户单击与每个项目关联的img标签,以在两个列表之间移动它。 I am doing all the movement between the lists via javascript and want to have a good method for obtaining the values and storing them in two lists upon Postback. 我正在通过javascript在列表之间进行所有移动,并希望有一个好的方法来获取值并将它们存储在Postback上的两个列表中。 I thought of doing something like storing the indexes/classes in hidden values and them reading them to find the items but that seems messy. 我想做一些类似将索引/类存储在隐藏值中,然后读取它们以查找项目的事情,但这似乎很麻烦。 Also, thought there might be a clever way of traversing the DOM via FindControl() but not sure. 另外,认为可能存在通过FindControl()遍历DOM的聪明方法,但不确定。 Any input would be great. 任何输入都会很棒。

One other thing is that this is not a composite control. 另一件事是这不是复合控件。 All the tags are being rendered in the RenderContents() method. 所有标签都在RenderContents()方法中呈现。 So I don't know if there is a way to even tack on a runat="server" attribute to an elem like I normally would if I wanted code behind access to an html element. 因此,我不知道是否有办法像我通常想要在访问html元素之后编写代码一样,将runat =“ server”属性附加到元素上。

Here is the HTML that is generated. 这是生成的HTML。 I don't think there is need to post the server side code since it does nothing more than generate the html at this point. 我认为没有必要发布服务器端代码,因为它只不过在此时生成了html。

<div class="DualListPanel" id="test1">
   <div key="test1_Unassigned">
      <table class="DLPTable">
         <tr>
            <td>Item 1</td>
            <td><img src="..." onclick="Move(this)" /></td>
         </tr>
         <tr>
            <td>Item 2</td>
            <td><img src="..." onclick="Move(this)" /></td>
         </tr>
      </table>
   </div>
   <div key="test1_Assigned">
      <table class="DLPTable">
      </table
   </div>
</div>

You can't use FindControl() to parse client-script. 您不能使用FindControl()解析客户端脚本。 If you want to track changes made on the client so you can adjust data on the server, you have to use a hidden field, store the changes. 如果要跟踪在客户端上所做的更改,以便可以调整服务器上的数据,则必须使用隐藏字段,存储更改。 A hidden field is the only way in this case to get the client to server interaction working. 在这种情况下,隐藏字段是使客户端与服务器交互正常工作的唯一方法。

The other answer is the right approach. 另一个答案是正确的方法。 There's nothing inherently messy about storing data in a hidden field. 在隐藏字段中存储数据本质上没有什么麻烦。 It's the most basic way the client and the server communicate. 这是客户端和服务器通信的最基本方式。

But I wanted to post another alternative, which is to use AJAX and a WebMethod. 但是我想发布另一种替代方法,即使用AJAX和WebMethod。 With jQuery this is very easy. 使用jQuery,这非常容易。 Your submit handler can do a POST to the WebMethod with whatever information is needed about the new list order (even including all the HTML, if you really wanted to do it that way, though I think tracking changes in state or ordinal values as needed would be a lot simpler). 您的提交处理程序可以使用有关新列表顺序的任何信息(包括所有HTML,如果您确实想这样做)将POST信息发送到WebMethod,尽管我认为可以根据需要跟踪状态或顺序值的变化要简单得多)。 The method can then update the database. 然后,该方法可以更新数据库。 If you wanted to get real fancy, you could hit the server in real time (eg every time they move something on the client) with an asynchronous method. 如果您想获得真正的幻想,则可以使用异步方法实时访问服务器(例如,每次它们在客户端上移动某些东西时)。

Without knowing the context, it's probably overkill to do that, but it's another way that has its time and place. 在不知道上下文的情况下,这样做可能是过高的,但这是有时间和地点的另一种方式。

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

相关问题 完全通过服务器端代码在主体中编写HTML响应吗? - Writing an HTML response in the body, entirely from server-side code? 是否可以在服务器端获取Facebook API访问令牌? - Is it possible to obtain Facebook API access token server-side? 表格/嵌套(?)JSON集合,然后在服务器端访问它 - Tabular/nested(?) JSON collection and then accessing it on the server-side 从服务器端启动模态对话框 - Launch modaldialog from server-side 从RadComboBox删除服务器端事件 - Removing Server-Side Event from RadComboBox 我可以从html页面调用C#或C ++以便在本地主机上进行服务器端脚本编写/编程吗? - Can I call C#, or C++, from html pages for server-side scripting/programming on localhost? 从 ASP.NET MVC 中的服务器端块生成 HTML - Generating HTML from server-side block in ASP.NET MVC 创建表客户端,然后下载excel文件或在服务器端生成excel文件,保存到服务器,然后使用JQuery从那里下载? - Create table client side, then download excel file OR generate excel file server-side, save to server, and use JQuery to download from there? 无法将值从服务器端设置为客户端的文本框 - unable to set value from server-side to a Textbox in client side UpdatePanel-从服务器端到客户端div显示错误 - UpdatePanel - Show errors from server-side into client side div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM