开发者

adding functionality to an imageButton in a gridview

Hallo there 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.

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "addToSession")
    {
        //get the row index stored in the CommandArgument property
        int index = Convert.ToInt32(e.CommandArgument);

        //get the gridview row where the command is raised
        GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

        //values stored in the text property of the cells
        string ISBN = selectedRow.Cells[0].Text;
        string bookTitle = selectedRow.Cells[1].Text;
        string image = selectedRow.Cells[2].Text;

        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 t开发者_如何转开发his 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>

Advice perhaps as to what to do

regards


ImageButton.CommandName is a valid property and it should work the same as Button.CommandName.

The problem, then, is most likely in your GridView1_RowCommand procedure. Can you post the entire procedure code including the part intended to handle the ImageButton click?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜