【问题标题】:Set Value Textbox Inside GridView Using Javascript使用 Javascript 在 GridView 内设置值文本框
【发布时间】:2013-06-17 20:10:11
【问题描述】:

我希望将“val”中的存储值分配给 gridview 中的文本框。我能够循环并找到行但不是控件。我收到一条未定义的错误消息。我缺少的是我找不到控件。附上示例代码:

<!--Javascript Code-->


<script type="text/javascript">
function SetPIN(val)
{
 if (Searching == 'eID')
{

  var num = val;
  var grdvw = document.getElementById('gvEvents');


for (var rowId = 1; rowId < grdvw.rows.length -1; rowId++){
var txtbx = grdvw.getElementById('txtEvtTo').value;

txtbx = num;


 }end for

 }
 }

</script>
  <asp:GridView ID:gvEvents runat="server" AutoGenerateColumns="False" >
  <Columns>
 <asp:TemplateField>
    <ItemTemplate>
 <asp:TextBox ID="txtEvtTo" runat="server" Width="250px"></asp:TextBox>
</ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>

我已经尝试了不同的选项,但我无法将值传递给文本框

任何建议将不胜感激。

博士

【问题讨论】:

    标签: javascript asp.net gridview textbox


    【解决方案1】:

    不要在 IDE 中查看 html 标记,而是在浏览器的“查看页面源代码”中显示呈现的 html。 asp 控件 ID 在呈现时会被破坏,因此除非您将控件的 ClientIDMode 设置为 Static,否则您对 getElementById 的调用将无法用于查找 gridview 或单元格控件。

    以下是我的一个网格视图中的几个示例数据行:

    <tr class="gvRowStyleContactList " valign="middle">
        <td>
            <span id="ctl00_ctl00_cphMain_IEHContent_gvEscalationContactListView_ctl02_lblEmailAddress">x@v.com</span>
        </td>
    </tr>
    <tr class="gvRowStyleContactList " valign="middle">
        <td>
            <span id="ctl00_ctl00_cphMain_IEHContent_gvEscalationContactListView_ctl03_lblEmailAddress">xx@vv.com</span>
        </td>
    </tr>
    

    【讨论】:

    • 我重新编写了这样的代码:var txtbx = grdvw.rows[rowId].cells[0].children[0].id;它让我找到了我需要的列和单元格,但现在我需要遍历单元格以获得特定的控制。发布的代码是实际代码的简化版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    相关资源
    最近更新 更多