Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
My JS code is not working if i give that in seperate file and working fine in the same file. Here is my code.


ASPX:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TwoInstances.aspx.cs" Inherits="TwoInstances" %>

<%@ Register src="TwoInstances.ascx" tagname="TwoInstances" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
   <div style="float:left;width:20%;"><uc1:TwoInstances ID="TwoInstances1" runat="server" /></div>
   <div style="float:left;width:20%;"><uc1:TwoInstances ID="TwoInstances2" runat="server" /></div>
        </div>
         </form>
    <script type="text/javascript">
        function fnCheck() {
            var a = document.getElementById('<%=((TextBox)TwoInstances1.FindControl("txt_text")).ClientID%>').value;
            if (a == null || a == "")
                alert("Please enter some text");
            return false;
        }
        function fnCheck1() {
            var b = document.getElementById('<%=((TextBox)TwoInstances2.FindControl("txt_text")).ClientID%>').value;
    if (b == null || b == "")
        alert("Please enter some text");
    return false;
}
    </script>
   <%-- <script src="Scripts/TextJS.js" type="text/javascript"></script>--%>
    </body>
</html>




ASPX.CS:

XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TwoInstances : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button btnSubmit1 = (Button)TwoInstances1.FindControl("btn_submit");
        btnSubmit1.Attributes.Add("onClick", "fnCheck()");
        btnSubmit1.Click += new EventHandler(btnSubmit1_Click);
        Button btnSubmit2 = (Button)TwoInstances2.FindControl("btn_submit");
        btnSubmit2.Attributes.Add("onClick", "fnCheck1()");
        btnSubmit2.Click += new EventHandler(btnSubmit2_Click);
    }
  
   void btnSubmit1_Click(object sender, EventArgs e)
    {
        ((TextBox)TwoInstances2.FindControl("txt_text")).Text += ((TextBox)TwoInstances1.FindControl("txt_text")).Text;

    }
   
   void btnSubmit2_Click(object sender, EventArgs e)
    {
        ((TextBox)TwoInstances1.FindControl("txt_text")).Text += ((TextBox)TwoInstances2.FindControl("txt_text")).Text;
    }
  }








If i give the same javascript code in seperate file it is not working. Can anyone plz
give me the solution for this.
Posted
Comments
Sergey Alexandrovich Kryukov 4-Nov-15 2:36am    
You did not show how you used separate file.
—SA

1 solution

You can't use server side code into a separate JS file
JavaScript
'<%=((TextBox)TwoInstances1.FindControl("txt_text")).ClientID%>'


Solution:
create function with parameter into a JS file
JavaScript
function fnCheck(a) {....}

and call the function from aspx page like

JavaScript
<script>
fnCheck('<%=((TextBox)TwoInstances1.FindControl("txt_text")).ClientID%>');
</script>
 
Share this answer
 
Comments
Member 11069263 4-Nov-15 3:08am    
I tried like that also. It is giving validation even if i enter the text also.

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