0

这是一个代码。当我单击编辑按钮时,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>
4

1 回答 1

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。

于 2013-08-25T01:53:28.103 回答