我有一些看起来像这样的表:
<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" /> </td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /> </td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /> </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" /> </td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /> </td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /> </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"
发布评论