"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Tuesday, January 29, 2013

Changing image within Gridview programatically

Simple RowdataBound event isit? an alternate if it reduce memory load and time. wht abt that :)
Yup by jquery :)

 gridviewgoes like below , and over that which cell to be replaced will be selected and and added a class activeImg,i get all the com element with that class name and get the text and replace it with my criteria

<asp:GridView ID="gvClientDetails" runat="server" AutoGenerateColumns="False">
……..
…………
<asp:TemplateField HeaderText="IsActive">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label8" CssClass="activeImg" runat="server" Text='<%#Eval("IsActive") %>'></asp:Label>
                                                </ItemTemplate>
……………
………..
</asp:GridView>


Jquery goes here



function AddImg() {
            $('.activeImg').each(function() {
                var $this = $(this);

                if ($this.text() == 'True') {
                    $this.empty();
                    $this.append('<input type="image" title="Active" src="../Images/tick.png" style="border-width:0px;"/>');
                }
                else {
                    $this.empty();
                    $this.append('<input type="image" title="Active" src="../Images/grdDeleteIcon.gif" style="border-width:0px;"/>');
                }
            });
        }

Here i had make it in a function advantage is that if their is any event over that (pagination , edit etc) .it should be called again.Better way is after binding the grid call this javascript function from server side (How to acheive)

Demo

If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: