locked
Javascript works only first textbox inside gridview RRS feed

  • Question

  • User1052662409 posted

    Hello All,

    I have a gridview. This gridview have a textbox in template field.

      <asp:TemplateField HeaderStyle-CssClass="thead-dark" HeaderText="Request No.">
                                                        <ItemTemplate>
    
     
                                                             <asp:TextBox runat="server" ID="txtfinAmt" CssClass="textboxsize req" onkeypress="ValidateMemComment(event);" onchange="CheckAvailability()"> </asp:TextBox>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>

    Below is my java script function which I am calling on "onchange" event of textbox.

     <script type="text/javascript">  
            function CheckAvailability() {
                var req_no =  $('[id$=txtfinAmt]').val();
                alert(req_no);
                $.ajax({
                    type: "POST",
                    url: "Topic.asmx/CheckReqNumber", // this for calling the web method function in cs code.  
                    data: '{req_no: "' + req_no    + '" }',// user name or email value  
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response);
                    }
                });   
            }
            function OnSuccess(response) {
                switch (response.d) {
                    case "true":
                        alert("Req. No already exists");
                        $('[id$=txtfinAmt]').val("");
                        $('[id$=txtfinAmt]').focus();
                        break;
                    case "false":
                        //msg.style.display = "block";
                        //msg.style.color = "green";
                        //msg.innerHTML = "User Name Or Email Available";
                        break;
                }
            }   
    
        </script> 

    The problem is that, this CheckAvailability() funxtion works only for first textbox of gridview. when I try to run this function by putting some value in another textbox then it shows the textbox value is empty

    Please suggest.

    Tuesday, April 20, 2021 5:53 AM

Answers