简体   繁体   中英

Calling javascript function from JS file in asp.net

I have referenced my javascript in my page as follows

<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

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

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

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

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

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM