在另一个文本框中插入GridView文本框值
GridView textbox value insert in another textbox
我有一个包含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>
相关文章:
- 基于比较c#中gridview的文本框值来启用或禁用按钮
- 正在获取Gridview中文本框的客户端ID
- 使用 javascript 更改 Gridview 文本框的可见属性 true 在下拉列表的选定索引更改事件上为 tru
- 如何在javascript函数中获取文本框(位于Gridview内部)的值
- 使用 JavaScript 在 GridView 中查找和验证文本框和下拉列表
- 如何使用 JavaScript 在 GridView 中获取文本框值
- 使用 jquery 为 gridview 文本框赋值
- 如何使用Javascript从Gridview中的文本框中获取文本值
- JQuery 最接近的元素,来自 GridView 中的文本框
- 如何调用GridView中的文本框
- 用于在gridview中插入文本框值的javascript
- 如何使用javascript将文本框值插入到gridview中
- 如何从javascript函数设置gridview文本框的值
- gridview c#中文本框的Javascript验证
- 如何在gridview中自动完成文本框
- 适合GridView列中的长文本
- 在另一个文本框中插入GridView文本框值
- 使用Javascript在GridView内设置值文本框
- 使用JavaScript获取GridView的列名或标题文本
- 更改asp.net中gridview内文本框的背景颜色