【问题标题】:accessing a text box inside a grid with javascript使用 javascript 访问网格内的文本框
【发布时间】:2017-09-04 16:31:54
【问题描述】:

我在 gridView 中有一个标签和多个文本框和一个按钮,单击该按钮时我需要访问这些文本框,这就是我的代码的样子..

<asp:GridView id="grdEventProfile" runat="server" >
    <Columns>

    <asp:BoundField DataField="EventID" SortExpression="EventID" HeaderText="Event ID"></asp:BoundField>

    <asp:TemplateField>
    <EditItemTemplate>
        <asp:TextBox id="txtEventDate" runat="server" Text='<%# Bind("EventRecognitionDate") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label id="Label4" runat="server" "Text='<%# Bind("EventRecognitionDate") %>'></asp:Label> 
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
    <EditItemTemplate>
        <asp:TextBox id="txtTaskDueDate" Text='<%# Bind("TaskDueDate") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label id="Label2" runat="server" Text='<%# Bind("TaskDueDate") %>'></asp:Label> 
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
    <ItemTemplate>
        <asp:Button id="btnEdit" runat="server" Text="EDT" OnClientClick = "AllowEdit()" ></asp:Button> 
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
</asp:GridView>

在我的 JavaScript 函数 AllowEdit() 中,我希望能够做这样的事情

if ( EventID == 01 && txtEventDate.value == 0 )
    txtTaskDueDate.Enabled = False;

我无法通过 document.getElementByID 访问我的文本框或标签,因为它们位于网格内。

我也试过这种方法:

var grid = document.getElementById('<%=grdEventProfile.ClientID%>');
var elements = grid.getElementsByTagName("input");

但事实证明它只适用于 (onChange) 属性,而不适用于 onClientClick。

提前致谢。

【问题讨论】:

  • 为什么不在服务器端试试呢?
  • 这是一个小组项目,我不应该在服务器端进行任何更改,我希望我能够在客户端进行更改!所以你是说 JavaScript 不可行?

标签: javascript asp.net gridview


【解决方案1】:

GridView 有行,因此您需要先指定正确的行号才能访问 TextBox。

var value = document.getElementById('<%= grdEventProfile.Rows[i].FindControl("TextBox1").ClientID %>').value;

或者将它们全部循环并得到正确的。

$('#<%= grdEventProfile.ClientID %> input[type="text"]').each(function () {
    alert($(this).attr("ID"));
});

【讨论】:

  • 如何知道正在编辑的行的行号?因为我只想为与单击的特定按钮关联的文本框执行我的代码..
  • 在 GridView 中你有 Container.DataItemIndex,它会给你行号。
  • 不幸的是使用该方法, (var value = document.getElementById('').value;) 给了我错误:表达式不产生值
猜你喜欢
  • 2013-04-14
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多