我有一个ImageButton控件作为GridView控件的一部分,该控件显示为ItemTemplate并位于同一个GridView中。我要我加了一些这样的代码在gridview中为imageButton添加功能
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");
}
当我运行这段代码我得到一个格式异常和我又是不知道为什么一个普通Button控件。我已经改变了一下图像按钮,并将图像嵌入链接按钮,似乎更正确。
<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>
请指教。
问候,
阿里安
@marco:在GridView的TemplateField的ItemTemplate的情况下,你绝对可以有'的eval()'语句。 – kbrimington 2011-05-02 17:38:03
嗨...我删除我的第一个评论... tks的建议! – marcoaoteixeira 2011-05-02 17:46:07