【问题标题】:GridView textbox value insert in another textboxGridView 文本框值插入另一个文本框中
【发布时间】:2014-09-17 16:27:53
【问题描述】:

我有一个包含两个 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>

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

【问题讨论】:

    标签: c# javascript asp.net gridview


    【解决方案1】:

    试试这个

    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>
    

    【讨论】:

    • 这只能在 JavaScript 上实现吗?为什么我们使用 GridView1_RowDataBound。
    • 是的,这只是在 javascript.RowDataBound 只是在运行时向控件添加事件。
    • @Ganesh_Devlekar:我已经发布了这个答案。你的答案有什么独特之处??
    【解决方案2】:

    在您的 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>
    

    【讨论】:

    • 如何在 JavaScript 上做到这一点?
    • @user3834541:使用上面的代码,您的 cal 函数将调用 gridview 中的所有文本框。查看我修改后的答案...根据答案修改您的代码
    • 在 "textBox1.Attributes.Add(onkeyup, "cal('" + textBox1.ClientID + "',' " + textBox2.ClientID + "')");"行。
    • @user3834541:请像这样在双引号(“”)中添加属性 - textBox1.Attributes.Add("onkeyup", "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多