使用C#从表格单元格内的输入框中提取数据(Extracting Data out of a input box inside a table cell using C#)

编程入门 行业动态 更新时间:2024-10-28 14:31:41
使用C#从表格单元格内的输入框中提取数据(Extracting Data out of a input box inside a table cell using C#)

我有一些看起来像这样的表:

<table width="650" id="myTable" runat="server"> <tbody> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" />&nbsp;</td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" />&nbsp;</td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" />&nbsp;</td> </tr> </tbody> </table>

现在,我想从中删除数据并将其存储在XML文件中。 问题是行数未知。 用户可以创建3到n行之间的任何位置。 为了解决这个问题,我写了一个小循环,将数据逐行拉出来。

// Assume the relevant items are declared and assigned a proper value. while (i < myTable.Rows.Count) { tRow = myTable.Rows[i]; tNameNode.InnerText = tRow.Cells[0].InnerText; tUnitsNode.InnerText = tRow.Cells[1].InnerText; tValueNode.InnerText = tRow.Cells[2].InnerText; tNewEntryNode.AppendChild(tNameNode); tNewEntryNode.AppendChild(tUnitsNode); tNewEntryNode.AppendChild(tValueNode); tIntermediateNode.AppendChild(tNewEntryNode); i++; }

现在我在运行之后才意识到的问题是它不会取出输入框内的内容,只是实际的输入框代码,这不是我想要的。

如何获取输入框内的内容?

I have some table that looks like this:

<table width="650" id="myTable" runat="server"> <tbody> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" />&nbsp;</td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" />&nbsp;</td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" />&nbsp;</td> </tr> </tbody> </table>

Now, I want to rip data out of that and store it in an XML file. The problem is that the number of rows is unknown. A user can create anywhere between 3 and n rows. To solve this, I wrote a little loop that pulls data out of the table by row.

// Assume the relevant items are declared and assigned a proper value. while (i < myTable.Rows.Count) { tRow = myTable.Rows[i]; tNameNode.InnerText = tRow.Cells[0].InnerText; tUnitsNode.InnerText = tRow.Cells[1].InnerText; tValueNode.InnerText = tRow.Cells[2].InnerText; tNewEntryNode.AppendChild(tNameNode); tNewEntryNode.AppendChild(tUnitsNode); tNewEntryNode.AppendChild(tValueNode); tIntermediateNode.AppendChild(tNewEntryNode); i++; }

Now the problem with this that I realized only after I ran it is that it's not going to fetch what is inside the input box, just the actual input box code, which is not really what I want.

How can I get the content inside the Input boxes?

最满意答案

您可以开发一些脚本来获取生成的文本框的所有文本值,方法是为它们分配所有相同的ID,然后让脚本按ID获取值。 然后将其存储到您可以在C#代码中访问的变量中

您的文本框的HTML,包括一个隐藏的TextBox,可以使用脚本填充其余文本框的值:

<input type="text" class="userTxtBox" /> <input type="hidden" Id="hiddenTxtBox" />

此时对JQuery的个人偏好(但我很确定你可以用任何脚本完成类似的事情):

$('body').live('input','.userTxtBox', function () { $('#hiddenTxtBox').val($('.userTxtBox').val()); });

然后,在您的C#代码中,您可以从hiddenTxtBox中获取文本。

The solution I employed looked like this:

while (i < myTable.Rows.Count) { tRow = tblCropsAndFeed.Rows[i]; string tInputBox0 = tRow.Cells[0].Controls[0].ClientID; tContent = (HtmlInputText)tRow.FindControl(tInputBox0); tNameNode.InnerText = tContent.Value; string tInputBox1 = tRow.Cells[1].Controls[0].ClientID; tContent = (HtmlInputText)tRow.FindControl(tInputBox1); tUnitsNode.InnerText = tContent.Value; string tInputBox2 = tRow.Cells[2].Controls[0].ClientID; tContent = (HtmlInputText)tRow.FindControl(tInputBox2); tValueNode.InnerText = tContent.Value; tNewEntryNode.AppendChild(tNameNode); tNewEntryNode.AppendChild(tUnitsNode); tNewEntryNode.AppendChild(tValueNode); tCropsAndFeedNode.AppendChild(tNewEntryNode); i++; }

The huge problem I encountered was the C# was treating the input boxes as a DirectControl object, which made it pretty impossible to get anything out of it. After adding

runat="server"

To the input boxes, all was fine however.

更多推荐

问题,myTable,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-28 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309466.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   表格   数据   单元格内   Extracting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!