Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4579256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:36:59+00:00 2026-05-21T20:36:59+00:00

I have an ImageButton control as part of a GridView control that is displayed

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:37:00+00:00Added an answer on May 21, 2026 at 8:37 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET web site which contains some ImageButton controls that cause postbacks
I have a gridview with some imagebuttons, each of which kicks off a report
I am making a custom ImageButton Control which has the option to show button
I have created a custom control for ImageButton as <Style TargetType={x:Type Button}> <Setter Property=Template>
I have a gridview control with delete asp:ImageButton for each row of the grid.
I have an asp:ImageButton that is supposed to popup a save dialog when clicked.
I have a server side ImageButton, and a server side Button, and then a
I have the following button <asp:ImageButton ID=button1 runat=server ImageUrl=~/image.jpg CommandName = id CommandArgument =
I have a Gridview with ImageButtons added to a column via a templatefield. I've
I have the below code in a .ascx file as part of an asp.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.