Wednesday, May 29, 2013

Find Label inside Templete column of GridView Javascript

Hi, An Easy way to find Label inside Gridview through Javascript.
Label control actually renfered like Span the Html  Control. you can see by Using Firebug

In GridView

   <asp:TemplateField HeaderText="Employee Name" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="110px">
                   
                        <ItemTemplate>
                            <asp:Label ID="lblDisplayEmp" runat="server" Text='<%# Eval("DisplayEmpName") %>'></asp:Label>       
                            <asp:Label ID="hidEmpname" style="display:none;"     runat="server" Text='<%# Eval("EmpName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


  var grd = document.getElementById("<%= GridView1.ClientID %>");
  for (i = 1; i < grd.rows.length; i++) {
var empName = grd.rows[i].cells[17].getElementsByTagName("span");
                alert(empName[1].innerHTML);
}

Note :  empName[1].innerHTML- it will give you first label
             empName[0].innerHTML  it will give you Second label

Thursday, May 9, 2013

get distinct values from string


An easy way to remove duplicate from string and get distinct values


 var input = '1,2,3,1,4,6,7,2,3';
                var splitted = input.split(',');
                var collector = {};
                for (i = 0; i < splitted.length; i++) {
                    key = splitted[i].replace(/^\s*/, "").replace(/\s*$/, "");
                    collector[key] = true;
                }
                var out = [];
                for (var key in collector) {
                    out.push(key);
                }
                var output = out.join(',');

OutPut will be 1,2,3,4,6,7