如何使用javascript在gridview中获取文本框值

时间:2014-12-06 11:27:14

标签: javascript c# asp.net gridview

我想使用javascript在gridview中获取文本框值。但我无法找到价值。这是我的Asp.net页面。我怎么能得到" txtGridName"来自更新按钮onclientclick的值?

Asp.net代码:             

     <Columns>

     <asp:TemplateField HeaderText="S.No">
    <ItemTemplate>                                                                            <%#Container.DataItemIndex+1 %>
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Unit Std">

    <EditItemTemplate>
    <%--<asp:TextBox ID="txtLocEditName" Width="100%" runat="server" Text='<%#(Eval("loc_name").ToString()) %>' />--%>

    <asp:DropDownList ID="drpGridUnitStd" runat="server" Text='<%#(Eval("uom_standard").ToString()) %>'>
    <asp:ListItem Text="A" Value="A"></asp:ListItem>
    <asp:ListItem Text="I" Value="I"></asp:ListItem>
                                                                                </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("uom_standard").ToString())%>' />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Unit Name">

    <EditItemTemplate>
    <asp:TextBox ID="txtGridName" Width="100%" runat="server" Text='<%#(Eval("uom_ts_unit").ToString()) %>' />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="lblGridName" runat="server" Text='<%#(Eval("uom_ts_unit").ToString()) %>' />
    </ItemTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Dimension-1">
    <EditItemTemplate>
    <asp:TextBox ID="txtGridDim1" Width="100%" runat="server" Text='<%#(Eval("uom_dimension_1").ToString()) %>' />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="lblGridDim1" runat="server" Text='<%#(Eval("uom_dimension_1").ToString())%>' />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Dimension-2">
    <EditItemTemplate>
    <asp:TextBox ID="txtGridDim2" Width="100%" runat="server" Text='<%#(Eval("uom_dimension_2").ToString()) %>' />
    </EditItemTemplate>
    <ItemTemplate>
     <asp:Label ID="lblGridDim2" runat="server" Text='<%#(Eval("uom_dimension_2").ToString())%>' />
    </ItemTemplate>
    </asp:TemplateField>

      <asp:TemplateField HeaderText="Dimension-3">
    <EditItemTemplate>
       <asp:TextBox ID="txtGridDim3" Width="100%" runat="server" Text='<%#(Eval("uom_dimension_3").ToString()) %>' />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="lblGridDim3" runat="server" Text='<%#(Eval("uom_dimension_3").ToString())%>' />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Dimension-4">
    <EditItemTemplate>
    <asp:TextBox ID="txtGridDim4" Width="100%" runat="server" Text='<%#(Eval("uom_dimension_4").ToString()) %>' />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="lblGridDim4" runat="server" Text='<%#(Eval("uom_dimension_4").ToString())%>' />
    </ItemTemplate>
    </asp:TemplateField>

     <asp:TemplateField HeaderStyle-CssClass="hideGridColumn" ItemStyle-CssClass="hideGridColumn">
    <ItemTemplate>
    <asp:HiddenField ID="hdnUnit_Id" Value='<%#(Eval("unit_id").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Options">
<EditItemTemplate>
                                                                                <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/common/icons/Save-icon.png" ToolTip="Update" Height="15px" Width="15px" OnClientClick="return update();"/>
                                                                                <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/common/icons/Delete-icon.png" ToolTip="Cancel" Height="15px" Width="15px" />
                                                                            </EditItemTemplate>
                                                                            <ItemTemplate>
                                                                                <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/common/icons/edit.png" ToolTip="Edit" Height="15px" Width="15px" />
                                                                                <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/common/icons/delete.png" ToolTip="Delete" Height="15px" Width="15px" OnClientClick="return del();" />
                                                                            </ItemTemplate>

</asp:TemplateField>



</Columns>





</asp:GridView>

Javascript代码如下:

    function update() 
  {
        var grid1 = document.getElementById("<%= gvUnit.ClientID %>");
        for (i = 1; i < grid1.rows.length ; i++) 
        {
            var cellValue = grid1.rows[i].cells[1].value;
            alert(cellValue);
        }

    }

1 个答案:

答案 0 :(得分:0)

问题是客户端ID如下所示:

ct100_Contentplaceholder_GridID_RowID_ROWINDEX_TextboxID

这样的事情。并且它不能被唯一识别。由于网格仅适用于一次编辑,因此您可以更轻松地找到它。由于您没有使用JQuery,因此它有点复杂。也许如果您将CssClass="GridTextBox"添加到文本框控件中,然后使用getElementsByClassName

var tb = document.getElementsByClassName('GridTextBox');