Thursday 6 June 2013

Search in gridview entered in textbox using javascript

Javascript


<script type="text/javascript">  
   String.prototype.startsWith = function(str){    return (this.indexOf(str) === 0);}  
        function Highlight()     
        {     
            var srchval = document.getElementById('<%= TextBox1.ClientID %>').value;     
            var cntrlname = document.getElementById('<%= GridView1.ClientID %>');                 
            var rows = cntrlname.getElementsByTagName("tr"); 
            var rowTop = 0;  
            var FirstRow = rows[1];
            for(var loop = 0 ; loop < rows.length ; loop++)     
            {
                rows[loop].style.background='#ffffff';
            }
            if(srchval != '')
            {
            for(var loop = 0 ; loop < rows.length ; loop++)     
            {     
                var ok = 0;    
                var cells = rows[loop].getElementsByTagName("td");     
                for(i = 0 ; i < cells.length ; i++)     
                {                
                    if(cells[i].innerHTML.toLowerCase().startsWith(srchval.toLowerCase()))     
                    {     
                        ok = 1;                             
                    }     
                }     
               if(ok == 1)        
               {
                    CurrentRow = rows[loop];
                    SendUp(CurrentRow, FirstRow);
                    CurrentRow.style.background='#eeff00';                
               }       
                else                     
                    rows[loop].style.background='#ffffff';             
            }     
            }
            return false;     
        }   
        function SendUp(CurrentRow,FirstRow)
        {
            FirstRow.parentNode.insertBefore(CurrentRow,FirstRow);
        }
        function GetMedName(MedName)
        {
            document.getElementById('<%= TextBox1.ClientID %>').value = MedName;
        }
     </script>


 <asp:TextBox runat="server" ID="TextBox1" onkeypress="Highlight() " ></asp:TextBox> 

Code Behind
Hope that your medication name is on first cell



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onclick", "GetMedName('" + e.Row.Cells[0].Text + "');");
        }
    }

No comments:

Post a Comment