2014-12-06 79 views
0

我想使用javascript在gridview中獲取文本框的值。但我無法找到價值。這是我的Asp.net頁面。如何從更新按鈕onclientclick獲取「txtGridName」值?如何使用javascript在gridview中獲取文本框的值

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); 
     } 

    } 

回答

0

的問題是客戶端ID是類似以下內容:

ct100_Contentplaceholder_GridID_RowID_ROWINDEX_TextboxID 

類似的東西。它不能被唯一標識。由於網格僅適用於一個編輯,所以您可以更輕鬆地找到它。由於您沒有使用JQuery,所以它稍微複雜一些。也許如果您將CssClass="GridTextBox"添加到您的文本框控件,然後使用getElementsByClassName

var tb = document.getElementsByClassName('GridTextBox'); 
相關問題