Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a 2 textbox inside a gridview. i call j javascipt function on textbox onclick event. i need to get the both textbox value in my javascipt function when i select any textbox.

here is my code:-

<asp:GridView ID="GridView2" runat="server" C AutoGenerateColumns="False" Width="100%">
               <Columns>
                   <asp:TemplateField HeaderText="text1" >
                       <ItemTemplate>
                           <asp:TextBox ID="txt1" runat="server" onclick="javascript: BindCheckboxlist()"    Text='<%# Eval("txt1") %>'></asp:TextBox>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="text2" >
                       <ItemTemplate>
                           <asp:TextBox ID="txt2" runat="server" Text='<%# Bind("txt2") %>'></asp:TextBox>
                       </ItemTemplate>
                       <ControlStyle Width="150px" />
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>


can any one sugguest me the solution.

What I have tried:

    <script type="text/javascript">
        function BindCheckboxlist() {
            var txtName, txtName2;

            var row = txt1.parentNode.parentNode;

            //Fetch all controls in GridView Row.
            var controls = row.getElementsByTagName("*");

            //Loop through the fetched controls.
            for (var i = 0; i < controls.length; i++) {

                //Find the TextBox control.
                if (controls[i].id.indexOf("txt1") != -1) {
                    txt1= controls[i];
                    alert(txt1);
                }
                if (controls[i].id.indexOf("txt2") != -1) {
                    txt2= controls[i];
                    alert(txt2);
                }
            }
    }
</script>
Posted
Updated 12-Jun-20 5:23am

1 solution

Pass the ids of the elements into your javascript function

HTML
<asp:GridView ID="GridView2" runat="server" C AutoGenerateColumns="False" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="text1" >
            <ItemTemplate>
                <asp:TextBox ID="txt1" runat="server" onclick='<%#string.Format("javascript:BindCheckboxlist(\"{0}\", \"{1}\")", ((GridViewRow) Container).FindControl("txt1").ClientID, ((GridViewRow) Container).FindControl("txt2").ClientID) %>' Text='<%# Bind("txt1") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="text2" >
            <ItemTemplate>
                <asp:TextBox ID="txt2" runat="server" Text='<%# Bind("txt2") %>'></asp:TextBox>
            </ItemTemplate>
            <ControlStyle Width="150px" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<script>
    function BindCheckboxlist(txt1ID, txt2ID) {
        var txt1 = document.getElementById(txt1ID);
        var txt2 = document.getElementById(txt2ID);

            var row = txt1.parentNode.parentNode;

            //Fetch all controls in GridView Row.
            var controls = row.getElementsByTagName("*");

            //Loop through the fetched controls.
            for (var i = 0; i < controls.length; i++) {

                //Find the TextBox control.
                if (controls[i].id.indexOf("txt1") != -1) {
                    txt1= controls[i];
                    alert(txt1.value);
                }
                if (controls[i].id.indexOf("txt2") != -1) {
                    txt2= controls[i];
                    alert(txt2.value);
                }
            }
    }
</script>
 
Share this answer
 
Comments
TCS54321 13-Jun-20 7:53am    
thank you. its working perfectly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900