Arianule
Arianule

Reputation: 9063

Adding functionality to an imageButton in a gridview

I have an ImageButton control as part of a GridView control that is displayed as an ItemTemplate and in the same GridView. I have a regular Button control to which I added some code like this

if (e.CommandName == "addToSession")
{
    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

    string ISBN = selectedRow.Cells[0].Text;
    string bookTitle = selectedRow.Cells[1].Text;
    string image = selectedRow.Cells[2].Text;

    //storing title, author, pictureUrl into session variables to 'carry them over' to RateBook.aspx
    Service s = new Service();
    Session["ISBN"] = ISBN;
    Session["bookTitle"] = bookTitle;
    Session["ImageUrl"] = s.returnImageUrl(bookTitle);

    if (Session["userName"] == null)
    {
        Response.Redirect("registerPage.aspx");
    }
    else
    {
        Response.Redirect("RateBook.aspx");
    }
}
else if (e.CommandName == "ratedBooks")
{
    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

    string bookTitle = selectedRow.Cells[1].Text;

    Service s = new Service();

    Session["ImageUrl"] = s.returnImageUrl(bookTitle);

    Response.Redirect("BookRated.aspx");
}

when I run this code I get a format exception and again I am not sure why. I have altered the image button a bit and nested the image in a link button which seems to be more correct.

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ratedBooks">
            <asp:Image ID="ImageButton1" ImageUrl='<%#Eval("pictureUrl") %>' runat="server" />   

        </asp:LinkButton>    
    </ItemTemplate>
</asp:TemplateField>

Please advise.

Regards,

Arian

Upvotes: 0

Views: 3254

Answers (3)

cmujica
cmujica

Reputation: 1334

You need CommandArgument

<asp:TemplateField ShowHeader="False">
<ItemTemplate>
    <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"                                                                             ImageUrl="~/Modelos/Img/deleted.gif" />
</ItemTemplate>
</asp:TemplateField>

code behind C#

public void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            string t;
            if (e.CommandName == "Delete")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow selectedRow = grid1.Rows[index];
                t = selectedRow.Cells[2].Text;
            }
        }

On ASPX

<asp:GridView ID="grid1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" ...

Upvotes: 0

kbrimington
kbrimington

Reputation: 25692

I believe you can accomplish your needs with an ImageButton, as it supports all the major Button functionality, including CommandName (see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.commandname.aspx).

Try this out:

    <asp:ImageButton ID="LinkButton1" runat="server"
                     CommandName="ratedBooks"
                     ImageUrl='<%#Eval("pictureUrl") %>' />

Also, note that your format exception could be coming from the lines that read:

Convert.ToInt32(e.CommandArgument);

The reason being that there appears from this code snippet to be no value assigned to the CommandArgument of the button. Convert.ToInt32 requires a valid integer value to be passed in, which means that the ImageButton needs to have a number bound to its CommandArgument property.

If you based your solution on this MSDN reference (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx), note that the <asp:ButtonField> column type gets special treatment; the CommandArgument is set to the row index. When you use a template field, ASP.NET requires you to specify or data bind your own command argument.

Update

This question contains details on binding the grid view row index to a custom button:

ASP.NET GridView RowIndex As CommandArgument

Upvotes: 1

marcoaoteixeira
marcoaoteixeira

Reputation: 505

A possible solution is to add the ItemDataBound event handler and search for the image button change its image url.

MyGrid.RowDataBound += new RepeaterItemEventHandler(MyGrid_RowDataBound);

void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex > -1)
        {
            ImageButton image = e.Row.FindControl("MY_IMAGE_CONTROL") as ImageButton;

            image.ImageUrl = "PATH_TO_IMAGE";
        }
    }

Hope it helps.

Upvotes: 1

Related Questions