user2747854
user2747854

Reputation: 1

how to get image url on click in repeater

i want to get image url on clicking the image from database in repeater my database contains(id,url)

my repeater code is:

<asp:Repeater runat="server" ID="repeater" >
    <ItemTemplate >
    <asp:ImageButton runat="server" Width="200px" Height="200px" ImageUrl='<%#Eval("url") %>' OnCommand="Image_Click" CommandName="ImageClick" CommandArgument='<%#Eval("url") %>' />
    </ItemTemplate>
    </asp:Repeater>

my .cs code is

 protected void Image_Click(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "ImageClick")
        {
            string a=e.CommandArgument.tostring();
            responce.write(a);
        }
    }

Upvotes: 0

Views: 1396

Answers (3)

Win
Win

Reputation: 62301

Both Vladimir and Guigui answers are prefer way of accessing URL of an image button.

If you also want ID value in addition to URL, you can store multiple values into CommandArgument separated by comma.

<asp:ImageButton runat="server" 
    ImageUrl='<%#Eval("url") %>' 
    OnCommand="Image_Command" 
    CommandName="ImageClick" 
    CommandArgument='<%# Eval("Id")%> + "," + Eval("url") %>' />

protected void Image_Command(object sender, CommandEventArgs e)
{
  if (e.CommandName == "ImageClick")
  {
    string[] commandArgs = e.CommandArgument.ToString()
       .Split(',');
    string id = commandArgs[0];
    string url = commandArgs[1];
  }
}

Upvotes: 1

Guigui
Guigui

Reputation: 1115

you can do in ImageClick

((ImageButton)sender).ImageUrl 

to get the url of the clicked button

Upvotes: 2

Vladimir Kocjancic
Vladimir Kocjancic

Reputation: 1842

Cast sender to ImageButton and read its ImageUrl property.

Also, is there a reason you are using command instead of handling ImageButtons click event?

Upvotes: 1

Related Questions