如何使用javascript从gridview的edititem模板字段中获取值

时间:2013-08-24 14:59:18

标签: javascript asp.net

这是一个代码。当我单击编辑按钮时,gridview进入编辑模式,可以看到更新和取消按钮,项目模板的所有标签都更改为edititem模板的文本框。我想在单击“更新”按钮时使用javascript从选定的索引文本框中获取值。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewJavaScript.aspx.cs"     Inherits="GridViewJavaScript.GridViewJavaScript" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
    function gridviewvalidation() {
        debugger;
        var gridview = document.getElementById("GridView1");
        //var values = gridview.rows[].cells[1].innerText;
        alert(gridview.rows[selectedindex].cells[1].innerText);
    }
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="roleid" EmptyDataText="There are no data records to display." OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="roleid" SortExpression="roleid">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("roleid") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("roleid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="username" SortExpression="username">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox123" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="password" SortExpression="password">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("password") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("password") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="role" SortExpression="role">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("role") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("role") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Action">
                    <EditItemTemplate>
                        <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" OnClientClick="return gridviewvalidation()">Update</asp:LinkButton>
                        &nbsp;
                    <asp:LinkButton ID="btncancel" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="btnedit" runat="server" CommandName="Edit" Text="Edit" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>

    </div>
</form>

也试过但没有工作:

 <script type="text/javascript">
    function gridviewvalidation() {
        debugger;
        var gridview = document.getElementById("TextBox1");
        //var values = gridview.rows[].cells[1].innerText;
        alert(gridview.value);
    }
</script>

以下是错误:未定义

<script type="text/javascript">
    function gridviewvalidation() {
        debugger;
        var gridview = document.getElementById("GridView1");
        //var values = gridview.rows[].cells[1].innerText;
        // alert(gridview.rows[selectedindex].cells[1].innerText);
        var txtvalue = gridview.getElementsByTagName("TextBox1");
        alert(txtvalue.value);
    }
</script>

1 个答案:

答案 0 :(得分:0)

<script type="text/javascript">
    function gridviewvalidation(lnk) {
        debugger;
        var row = lnk.parentNode.parentNode;
        var rowIndex = row.rowIndex - 1;
        var gridview = document.getElementById("GridView1_TextBox1_"+rowIndex);
        alert(gridview.value);
    }
</script>

注意:“GridView1_TextBox1_0”是我从页面源获取的ID。