GridView文本框值插入另一个文本框中

时间:2014-09-17 16:27:53

标签: c# javascript asp.net gridview

我有一个包含两个TextBox的GridView,我想只使用JavaScript将TextBox1的值插入TextBox2。 我正在使用ASP.Net C#

这是我的ASPX代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
    function cal(t1, t2) {
        document.getElementById(t2).value = document.getElementById(t1).value;
    }
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
    <Columns>
        <asp:TemplateField HeaderText="Header1">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" onkeypress="cal('MainContent_TextBox1','MainContent_TextBox2')"
                onkeyup="cal('MainContent_TextBox1','MainContent_TextBox2')" onselect="cal('MainContent_TextBox1','MainContent_TextBox2')"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header2">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" />
    <EditRowStyle BackColor="#2461BF" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Content>

我正在使用该代码,但这不起作用。

2 个答案:

答案 0 :(得分:2)

试试这个

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox textBox1 = (TextBox)e.Row.FindControl("TextBox1");
            TextBox textBox2 = (TextBox)e.Row.FindControl("TextBox2");
            textBox1.Attributes.Add("onkeyup", "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");
            textBox1.Attributes.Add("onkeypress", "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");

        }
    }

将RowDataBound事件添加到GridView

OnRowDataBound="GridView1_RowDataBound"

并使用此Javascript

<script type="text/javascript">
        function cal(t1, t2) {
            document.getElementById(t2).value = document.getElementById(t1).value;
        }
</script>

答案 1 :(得分:1)

在GridView的OnRowDataBound事件中,您可以执行以下操作 -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox textBox1 = (TextBox)e.Row.FindControl("TextBox1");
        TextBox textBox2 = (TextBox)e.Row.FindControl("TextBox2");
        textBox1.Attributes.Add("onkeyup", "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");
        textBox1.Attributes.Add("onkeypress", "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");
    }
}

并从gridview的itemtemplate中的文本框中删除该属性。

<asp:TemplateField HeaderText="Header1">
     <ItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     </ItemTemplate>
</asp:TemplateField>