Zach
Zach

Reputation: 448

Creating a hyperlinked image with onclick event in RowDataBound

I have the below Colmn in a Gridview.

<ItemTemplate>
    <a href="AddNote.aspx" onclick="return popitup('AddNote.aspx?Account=<%# Eval("AccountID") %>&ID=<%# Eval("Invoice ID") %>')"><img src="Images/NoteIcons/note_add.png" height="16" width="16" /></a>
</ItemTemplate>

I want it it to only show based on a value coming from the databind.

So I removed the itemtemplate code and in Gridview_RowDataBound I added the following code.

if (e.Row.RowType == DataControlRowType.DataRow)
    {
      if (e.Row.Cells[10].Text != "1")
            {
                //Code to display hyperlink and image
            }    
    }

Do I need to switch this to a image button? I'm not sure how I can get the link, onclick and image to show up all together. I tried with Hyperlink newHyperlink = new HyperLink(); but I don't see the property to set an onclick event.

Any help with dynamically building this column when the text =! 1?

Thank you.

Upvotes: 0

Views: 969

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13599

You can try doing something like this

ImageButton btn= new ImageButton();
btn.ImageUrl="Images/NoteIcons/note_add.png";
btn.Attributes.Add("onClientClick", "YourJavaScriptFunction();");
e.Row.Cells[10].Controls.Add(btn);

Upvotes: 1

Related Questions