views:

219

answers:

1

Hi All,

<script language="javascript" type="text/javascript">
    function ValidateNew() {

        var gridview = document.getElementById('ctl00_cp_GridViewKRIlib');

        if (gridview != null) {

            var Inputs = gridview.getElementsByTagName("input");

            for (i = 0; i < Inputs.length; i++) {

                if (Inputs[i].type == 'text') {

                    if (Inputs[i].value == "") {
                        alert('Enter the value!');
                        Inputs[i].focus();
                        return false;
                    }
                }
            }
        }
    }

i am calling this funtion in

<asp:TemplateField HeaderText="Edit">
<ItemTemplate>

<asp:LinkButton ID="linkbuttonNew" runat="server" Text="New" CommandName="New" CommandArgument='<%#Container.DataItemIndex%>'
OnClientClick="javascript:return ValidateNew();">
</asp:LinkButton>

INPUTS Are Showing zero Why?

A: 

This looks brittle:

var gridview = document.getElementById('ctl00_cp_GridViewKRIlib');

You're relying on knowing ASP.NET's generated ID for an HTML element, which at best reduces your code's portability and at worst simply won't work (for instance, if you change something else in the page). In the absence of any actual HTML to look at I'd suggest that line is the most likely source of error.

What you could do instead is pass the ID of the HTML element ASP.NET is generating into your JavaScript function, so the first two lines become

function ValidateNew(elementId) {

    var gridview = document.getElementById(elementId);

My ASP.NET is rusty so I'm not sure how best to get the ID and pass it into the JavaScript. ClientID is the property you need from the generated control, which you get in the OnClientClick attribute (a quick search found me this page, which may help). And you shouldn't have the "javascript:" prefix in your OnClientClick attribute value. The value of a JavaScript event handler attribute should be just JavaScript.

Tim Down