javascript - 如何在javascript中的gridview中获取运行时创建的文本框的id

标签 javascript asp.net html

我有一个可编辑的 GridView 。当我单击与 gridview 中的一行对应的编辑按钮时,我提供了两个文本框来输入两个新的 values.2 文本框,因为我有 2 列。现在这个文本框是在运行时生成的。我想在 javascript 中访问这个文本框并对它们执行验证。这是 html 语法。

<table cellspacing="0" rules="all" border="1" id="TableGridView" style="border-collapse:collapse;table-layout:fixed">
            <tr>
                <th scope="col"><b>Action</b></th><th scope="col"><b>PROCESSOR_ID</b></th><th scope="col"><b>PROCESSOR_NAME</b></th>
            </tr><tr>
                <td><input type="image" name="TableGridView$ctl02$edit_button" id="TableGridView_ctl02_edit_button" title="Edit" src="images/edit.gif" style="border-width:0px;" /><input type="image" name="TableGridView$ctl02$delete_button" id="TableGridView_ctl02_delete_button" title="Delete" src="images/delete.gif" onclick="return confirm('Are you sure you want to delete the record?');" style="border-width:0px;" /><input type="image" name="TableGridView$ctl02$insert_button" id="TableGridView_ctl02_insert_button" title="Insert" src="images/insert.bmp" style="border-width:0px;" /></td><td><span id="TableGridView_ctl02_PROCESSOR_ID">23</span></td><td><span id="TableGridView_ctl02_PROCESSOR_NAME">dasdas</span></td>
            </tr><tr>
                <td><input type="image" name="TableGridView$ctl03$edit_button" id="TableGridView_ctl03_edit_button" title="Edit" src="images/edit.gif" style="border-width:0px;" /><input type="image" name="TableGridView$ctl03$delete_button" id="TableGridView_ctl03_delete_button" title="Delete" src="images/delete.gif" onclick="return confirm('Are you sure you want to delete the record?');" style="border-width:0px;" /><input type="image" name="TableGridView$ctl03$insert_button" id="TableGridView_ctl03_insert_button" title="Insert" src="images/insert.bmp" style="border-width:0px;" /></td><td><span id="TableGridView_ctl03_PROCESSOR_ID">123</span></td><td><span id="TableGridView_ctl03_PROCESSOR_NAME">asdasdas</span></td>
            </tr><tr>
                <td><input type="image" name="TableGridView$ctl04$update_button" id="TableGridView_ctl04_update_button" title="Add" src="images/update.gif" onclick="return (textfieldvalidation());" style="border-width:0px;" /><input type="image" name="TableGridView$ctl04$cancel_button" id="TableGridView_ctl04_cancel_button" title="Cancel" src="images/cancel.gif" style="border-width:0px;" /></td><td><input name="TableGridView$ctl04$PROCESSOR_ID" type="text" id="TableGridView_ctl04_PROCESSOR_ID" /></td><td><input name="TableGridView$ctl04$PROCESSOR_NAME" type="text" id="TableGridView_ctl04_PROCESSOR_NAME" /></td>
            </tr><tr>
                <td><input type="image" name="TableGridView$ctl05$edit_button" id="TableGridView_ctl05_edit_button" title="Edit" src="images/edit.gif" style="border-width:0px;" /><input type="image" name="TableGridView$ctl05$delete_button" id="TableGridView_ctl05_delete_button" title="Delete" src="images/delete.gif" onclick="return confirm('Are you sure you want to delete the record?');" style="border-width:0px;" /><input type="image" name="TableGridView$ctl05$insert_button" id="TableGridView_ctl05_insert_button" title="Insert" src="images/insert.bmp" style="border-width:0px;" /></td><td><span id="TableGridView_ctl05_PROCESSOR_ID">6456</span></td><td><span id="TableGridView_ctl05_PROCESSOR_NAME">dg</span></td>
            </tr>
        </table>

我“必须”使用 IE 6.0。

我已经为表格尝试了 document.getElementbyID 和 getElementsByTagName,但它们不起作用。

var curValue = document.getElementById("<%=TableGridView.ClientID%>").rows; curValue 在 IE 中为 1,在 firefox 中为 4。

最佳答案

您应该传递被点击元素的引用并避免使用 ID。

<input type="image" ... onclick="return textfieldvalidation(this)"...

函数

function textfieldvalidation(button){

  var tr = button.parentNode.parentNode,
      inputs = tr.getElementsByTagName('INPUT'), //all INPUT tags of the TR
      id = inputs[0].getAttribute('id'); //or inputs[0].id
}

关于javascript - 如何在javascript中的gridview中获取运行时创建的文本框的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976962/

相关文章:

asp.net - Linq 选择列表中项目存在的位置

asp.net - 如何在专用服务器中使用 IIS6 SMTP 从 ASP.NET 发送邮件?

jquery - Bootstrap 在本地运行但在实时运行时不完全运行(jquery)

html - 更新后占位符 css 在 chrome 中不起作用

javascript - joint.setTheme 不是 JointJS-Rappid 中的函数

javascript - 无法解决 Uncaught ReferenceError : jQuery is not defined

javascript - jQuery 在函数和动画开始/动画结束事件上的问题

c# - 为来自 web.config 文件的 http 请求配置身份验证

javascript - 我需要帮助来识别和解决淡入文本问题

javascript - Dojox.grid.datagrid 排序顺序