【问题标题】:How to get the id of textbox created at runtime in a gridview in javascript如何在javascript的gridview中获取运行时创建的文本框的id
【发布时间】:2011-04-27 23:28:34
【问题描述】:

我有一个可编辑的网格视图。当我单击与 gridview 中的一行相对应的编辑按钮时,我提供了两个文本框来输入两个新值。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("").rows; curValue 在 IE 中为 1,在 Firefox 中为 4。

【问题讨论】:

  • 如何添加编辑框?使用 javascript 还是在代码隐藏中?
  • 您可能需要考虑重新调整代码布局,使其适合屏幕。目前我发现很难提供帮助。
  • codebehind..通过模板@Kangkan做

标签: javascript asp.net html


【解决方案1】:

你应该传递被点击元素的引用,避免使用 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
}

【讨论】:

    【解决方案2】:

    这里我给出了示例,你将如何在此处使用 java 脚本代码

    enter code here
    

    // JScript 文件

    //此方法用于在验证电子邮件字段时添加行 函数 addRows(tableID) { 如果(验证(this.obj)) { var emailTableRow = document.getElementById("ctl00_ContentPlaceHolder1_EmailTableRow").value;

            var table = document.getElementById(tableID);
    
            var rowCount = table.rows.length;
    
    
            var lastRow;
            if (emailTableRow == '') {
                emailTableRow = "'1'";
                lastRow = 1;
            }
            else 
            {
    
                lastRow = emailTableRow.substr(emailTableRow.lastIndexOf(",") + 2, emailTableRow.lastIndexOf("'") - (emailTableRow.lastIndexOf(",") + 2));
    
    
            }
    
            var newRow = parseInt(lastRow) + 1;
    
            emailTableRow = emailTableRow + ",'" + newRow.toString() + "'";
    
            document.getElementById("ctl00_ContentPlaceHolder1_EmailTableRow").value = emailTableRow;
    
    
            var row = table.insertRow(rowCount);
    
    
            var cell0 = row.insertCell(0);
    
        cell0.style.marginLeft="0px";
    cell0.style.width="200px";
    cell0.style.height="12px";
    //cell0.style.line-height="12px";
    cell0.style.paddingLeft="0px";
    cell0.style.background="#70b9ee";
    cell0.style.fontFamily="Trebuchet MS";
    cell0.style.fontSize="11px";
    cell0.style.fontWeight="normal";
    cell0.style.color="#fff";
    cell0.style.textAlign="center";
    cell0.style.border="0px solid #000";
    
            var element0 = document.createElement("label");
            element0.innerHTML = newRow;
            element0.setAttribute("size", "3");
    
            element0.setAttribute("name", "lblRownumber" + newRow.toString());
            element0.setAttribute("id", "lblRownumber" + newRow.toString());
            cell0.appendChild(element0);
    
    
    
            var cell1 = row.insertCell(1);
            cell1.style.marginLeft="0px";
    cell1.style.width="200px";
    cell1.style.height="12px";
    //cell0.style.line-height="12px";
    cell1.style.paddingLeft="0px";
    cell1.style.background="#70b9ee";
    cell1.style.fontFamily="Trebuchet MS";
    cell1.style.fontSize="11px";
    cell1.style.fontWeight="normal";
    cell1.style.color="#fff";
    cell1.style.textAlign="center";
    cell1.style.border="0px solid #000";
            var element1 = document.createElement("input");
            element1.type = "text";
    
            element1.setAttribute("name", "txtEMAIL_ADDRESS" + newRow.toString());
            element1.setAttribute("id", "txtEMAIL_ADDRESS"+ newRow.toString());
    
            cell1.appendChild(element1);
    
    
            var cell2 = row.insertCell(2);
    
            cell2.style.marginLeft="0px";
    cell2.style.width="200px";
    cell2.style.height="12px";
    //cell0.style.line-height="12px";
    cell2.style.paddingLeft="0px";
    cell2.style.background="#70b9ee";
    cell2.style.fontFamily="Trebuchet MS";
    cell2.style.fontSize="11px";
    cell2.style.fontWeight="normal";
    cell2.style.color="#fff";
    cell2.style.textAlign="center";
    cell2.style.border="0px solid #000";
            var element2 = document.createElement("input");
            element2.setAttribute("name", "txtName"+newRow.toString());
            element2.setAttribute("id", "txtName"+newRow.toString());
            element2.type = "text";
            cell2.appendChild(element2);
    
    
    
            var cell3 = row.insertCell(3);
    
            cell3.style.marginLeft="0px";
    cell3.style.width="200px";
    cell3.style.height="12px";
    //cell0.style.line-height="12px";
    cell3.style.paddingLeft="0px";
    cell3.style.background="#70b9ee";
    cell3.style.fontFamily="Trebuchet MS";
    cell3.style.fontSize="11px";
    cell3.style.fontWeight="normal";
    cell3.style.color="#fff";
    cell3.style.textAlign="center";
    cell3.style.border="0px solid #000";
            var element3 = document.createElement("input");
            element3.setAttribute("name", "txtPROMOTIONAL_COMPAIGN"+newRow.toString());
            element3.setAttribute("id", "txtPROMOTIONAL_COMPAIGN"+newRow.toString());
            element3.type = "text";
            cell3.appendChild(element3);
    
    
            var row = table.insertRow(rowCount+1);
    
    
            var cell0 = row.insertCell(0);
            cell0.colSpan = 4;
            cell0.style.textAlign = "left";
            cell0.style.paddingLeft = "0px"
            cell0.style.fontWeight="normal";
            cell0.style.fontSize="11px";
    

    cell0.style.fontFamily="Trebuchet MS"; cell0.style.marginLeft="0px"; //cell0.style.margin="0px"; // cell0.style.width="200px"; // cell0.style.height="20px"; // cell0.style.line-height="12px"; cell0.style.paddingLeft="5px"; cell0.style.background="#70b9ee"; // cell0.style.font-family="Trebuchet MS"; // cell0.style.font-size="11px"; // cell0.style.font-weight="normal"; cell0.style.color="#fff"; // cell0.style.text-align="left"; //cell0.style.border="0px 实心#000";

            var element0 = document.createElement("label");
            element0.innerHTML ="Please feel free to change the Text";
            element0.setAttribute("size", "100");
    
            element0.setAttribute("name", "lblMessage" + newRow.toString());
            element0.setAttribute("id", "lblMessage" + newRow.toString());
            element0.setAttribute("class","srn-td-space3");
            cell0.appendChild(element0);
    
    
    
    
            var row = table.insertRow(rowCount+2);
    
    
    
                var cell0 = row.insertCell(0);
                cell0.colSpan = 4;
                cell0.style.textAlign = "left";
            cell0.style.paddingLeft = "5px"
            cell0.style.fontWeight="normal";
            cell0.style.fontSize="11px";
    

    cell0.style.fontFamily="Trebuchet MS"; cell0.style.marginLeft="1px"; // cell0.style.width="200px"; // cell0.style.height="20px"; // cell0.style.line-height="12px"; // cell0.style.paddingLeft="0px"; cell0.style.background="#70b9ee"; // cell0.style.font-family="Trebuchet MS"; // cell0.style.font-size="11px"; // cell0.style.font-weight="normal"; cell0.style.color="#fff"; // cell0.style.text-align="left"; //cell0.style.border="0px 实心#000";

                var element0 = document.createElement("textarea");
    
                element0.setAttribute("cols", "50");
    
                element0.setAttribute("rows", "4");
                element0.setAttribute("name", "txtAreaEmailBody" + newRow.toString());
                element0.setAttribute("id", "txtAreaEmailBody" + newRow.toString());
                cell0.appendChild(element0);
    
    
    
    
    
    
    
       }
    
            return false;
    

    }

    //此方法用于验证电子邮件..... 函数验证(formobj){

    var emailPattern = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    
    var getWorkFields;
    
    getWorkFields = getFieldRequired('ctl00_ContentPlaceHolder1_SendmailsTable', 'ctl00_ContentPlaceHolder1_EmailTableRow', 'txtEMAIL_ADDRESS');
    
    var fRequired = getWorkFields;
    
    var fieldRequired = fRequired.split(",");
    
    var validRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i;
    var email = document.getElementById(fieldRequired).value;
    
    
    if (email == "") {
        alert("email cannot be empty");
         return false;
    }
    else {
    
        if (email.search(validRegExp) == -1) {
    
            alert(" A valid E-mail is required");
    
            return false;
        }
        else {
            return true;
        }
    
    }
    

    } //该方法用于动态生成Ids function getFieldRequired(tableID, hiddenFieldID, filedNamePrefix) {

        try {
            var returnValue = '';
            var accRow = document.getElementById(hiddenFieldID).value;
    
            if (accRow == null || accRow == '')
                accRow = "'1'";
    
            var arrayString;
            arrayString = accRow.toString().split(",");
    
    
            aFieldPrefix = filedNamePrefix.toString().split(",");
    
            var I = parseInt(0);
            var J = parseInt(0);
            for (I = 0; I < arrayString.length; I++) {
                for (J = 0; J < aFieldPrefix.length; J++) {
                    if (I == 0 && J == 0)
                    {
                        returnValue = aFieldPrefix[J] + arrayString[I].replace("'", "").replace("'", "");
    
                     }
                    else
                    {
                        returnValue = aFieldPrefix[J] + arrayString[I].replace("'", "").replace("'", "");
    
                    }
    
                }
            }
    
            return returnValue;
    
        }
        catch (e) {
            alert(e);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      为此使用 jquery,因为它具有跨浏览器支持

      $.each("#TableGridView tr td input",function(){
          alert($(this).attr("id"));
      });
      

      【讨论】:

      • 使用javascript的任何方式
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多