0

I have a GridView that contain Two TextBox and i want to insert value of TextBox1 into TextBox2 using only JavaScript. I am using ASP.Net C#

Here is my ASPX Code :

<%@ 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>

I am using that code but this is not working.

0

2 Answers 2

2

Try this

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 + "')");

        }
    }

add RowDataBound Event To GridView

OnRowDataBound="GridView1_RowDataBound"

and use this Javascript

<script type="text/javascript">
        function cal(t1, t2) {
            document.getElementById(t2).value = document.getElementById(t1).value;
        }
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Is that possible on JavaScript only? Why we use GridView1_RowDataBound.
yes this is only in javascript.RowDataBound Just add event to the controls at runtime.
@Ganesh_Devlekar: I have already posted this answer. What is unique in your answer ??
1

In the OnRowDataBound event of your GridView, you can do something like this -

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 + "')");
    }
}

and remove the attribute from textbox in itemtemplate of gridview.

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

4 Comments

how can i don this on JavaScript?
@user3834541: Using above code, your cal function will call for all textbox in gridview. See my modified answer... Modify your code as per the answer
It gives an Error "The name 'onkeyup' does not exist in the current context" in "textBox1.Attributes.Add(onkeyup, "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");" line.
@user3834541: Please add attribute in double quote("") like this - textBox1.Attributes.Add("onkeyup", "cal('" + textBox1.ClientID + "','" + textBox2.ClientID + "')");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.