在另一个文本框中插入GridView文本框值
本文关键字:文本 GridView 插入 另一个 | 更新日期: 2023-09-27 18:22:17
我有一个包含Two 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>
我正在使用该代码,但这不起作用。
试试这个
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>
在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中的textbox中删除该属性。
<asp:TemplateField HeaderText="Header1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>