在asp.net中从JS文件调用javascript函数

时间:2022-04-09 15:59:27

I have referenced my javascript in my page as follows

我在我的页面中引用了我的javascript,如下所示

<script src="JScript1.js" type="text/javascript"></script>

These are my function inside that script file

这些是我在该脚本文件中的函数

function multiplication(txtQuantity) {
var weight = document.getElementById(txtQuantity).value;
}
 function f(sender, args) {
args.IsValid = false;
var gridview = document.getElementById("<%=Gridview1.ClientID%>");
var txt = gridview.getElementsByTagName("textarea");
for (i = 0; i < txt.length; i++) {

    if (txt[i].id.indexOf("TextBox1") != -1) {

        if (txt[i].value == '') {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }

    }
}
}

function f1(sender, args) {
args.IsValid = false;
var gridview = document.getElementById("<%=Gridview1.ClientID%>");
var txt = gridview.getElementsByTagName("textarea");
for (i = 0; i < txt.length; i++) {

    if (txt[i].id.indexOf("TextBox2") != -1) {

        if (txt[i].value == '') {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }

    }
}
 }

I would like to call these function from my code behind and also I would like to assign the function to custom validator

我想从我的代码后面调用这些函数,我也想将该函数分配给自定义验证器

I tried some thing as follows but not working

我尝试了一些如下事情,但没有工作

<asp:CustomValidator ID="custValCountry" runat="server" ValidationGroup="Country"
                        ValidateEmptyText="true" ControlToValidate="TextBox1" ClientValidationFunction="javascript:f"
                        ErrorMessage="Other is required"></asp:CustomValidator>

Also my under my RowDataBound event I write as follows this is also not working

我在我的RowDataBound事件下编写如下,这也是行不通的

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txt = (TextBox)e.Row.FindControl("TextBox1");
            Page.ClientScript.RegisterClientScriptBlock(txt.GetType(), "onBlur", "multiplication('" + txt.ClientID + "')");
            //Page.ClientScript.RegisterClientScriptBlock(, "Script", "alert('Records Successfuly Saved!');", true);
           // txt.Attributes.Add("onBlur", "return javascript:multiplication('" + txt.ClientID + "');");
            //TextBox txt1 = (TextBox)e.Row.FindControl("TextBox2");
            txt1.Attributes.Add("onBlur", "return javascript:multiplication('" + txt1.ClientID + "');");
        }
    }

Can some one help me

有人能帮我吗

2 个解决方案

#1


2  

Static JavaScript files do not get fed through ASP.NET normally, so this line will not work:

静态JavaScript文件通常不会通过ASP.NET提供,因此这行不起作用:

var gridview = document.getElementById("<%=Gridview1.ClientID%>");

Use a fixed ID for the grid and specify it directly:

对网格使用固定ID并直接指定它:

var gridview = document.getElementById('my-grid');

<asp:GridView ID="my-grid" ClientIDMode="Static" runat="server" ...>

Or come up with some other way of finding the ID.

或者想出一些其他方法来查找ID。

Also note that this function is next to worthless:

另请注意,此功能几乎毫无价值:

function multiplication(txtQuantity) {
var weight = document.getElementById(txtQuantity).value;
}

You get the weight then do nothing with it?

你得到了重量,然后无所事事吗?

#2


0  

You need to realize that your javascript functions are running in the client's browser, not on your server where your code behind is running. If you need to call the functions from your code behind, you will need to create equivalent functions in your code behind.

您需要意识到您的javascript函数正在客户端的浏览器中运行,而不是在运行代码的服务器上运行。如果需要从后面的代码中调用函数,则需要在代码后面创建等效函数。

#1


2  

Static JavaScript files do not get fed through ASP.NET normally, so this line will not work:

静态JavaScript文件通常不会通过ASP.NET提供,因此这行不起作用:

var gridview = document.getElementById("<%=Gridview1.ClientID%>");

Use a fixed ID for the grid and specify it directly:

对网格使用固定ID并直接指定它:

var gridview = document.getElementById('my-grid');

<asp:GridView ID="my-grid" ClientIDMode="Static" runat="server" ...>

Or come up with some other way of finding the ID.

或者想出一些其他方法来查找ID。

Also note that this function is next to worthless:

另请注意,此功能几乎毫无价值:

function multiplication(txtQuantity) {
var weight = document.getElementById(txtQuantity).value;
}

You get the weight then do nothing with it?

你得到了重量,然后无所事事吗?

#2


0  

You need to realize that your javascript functions are running in the client's browser, not on your server where your code behind is running. If you need to call the functions from your code behind, you will need to create equivalent functions in your code behind.

您需要意识到您的javascript函数正在客户端的浏览器中运行,而不是在运行代码的服务器上运行。如果需要从后面的代码中调用函数,则需要在代码后面创建等效函数。