0

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

Developer
  • 8,390
  • 41
  • 129
  • 238

2 Answers2

2

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?

Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • If i remove that function my custom validator does not fire – Developer Nov 14 '12 at 11:30
  • You're calling it with `txt1.Attributes.Add("onBlur", "return javascript:multiplication('" + txt1.ClientID + "');");`, what happens if you remove that? – Lloyd Nov 14 '12 at 11:30
  • If i place those functions directly inside script tag with out placing them in a `JS` file it works for me. But when placing them in a `JS` file that didn't work – Developer Nov 14 '12 at 11:31
  • @User Of cource they not work, because the `<%=Gridview1.ClientID%>` is not render inside the js. Try to understand what Lloyd say to you here. – Aristos Nov 14 '12 at 11:32
  • `Lloyd` I removed and test and as you said it is not worth for me, but I tried this scenario in a different way to show an alert instead of custom validation, there I used that – Developer Nov 14 '12 at 11:38
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.

Kevin
  • 552
  • 2
  • 4