Click here to Skip to main content
15,916,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to put ImageButton inside Gridview with value from database
I tried this:

ASP.NET
<asp:ButtonField ButtonType="Image" DataTextField="BookID" ImageUrl="~\images\{0}.png" />


and tried to put eval(bookID)
like:


HTML
ImageUrl=<%#"~\images\"+eval("bookID")+".png"


and it didn't success..
Posted

1 solution

Try:
aspx
ImageUrl='<%# Eval("bookID", "~/images/{0}.png") %>'
 
Share this answer
 
Comments
Y.Ahmad 20-May-15 17:04pm    
Thank you..
Can I pass the BookID in the event ImageButton1_Click ?!



<pre lang="HTML"> <asp:GridView ID="GVBooks" runat="server" AutoGenerateColumns="False" DataKeyNames="BookID" DataSourceID="ObjectDataSource1" AllowPaging="True" AllowSorting="True" PageSize="6" OnRowCommand="BtnDetails" OnRowDataBound="GVBooks_RowDataBound">
<Columns>
<asp:ButtonField CausesValidation="True" CommandName="BtnDetails" DataTextField="BookName" HeaderText="Book Name" />

<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageUrl='<%# Eval("bookID", "~/images/{0}.png") %>' runat="server" Height="80px" OnClick="ImageButton1_Click" Width="60px" />
</ItemTemplate>
</asp:TemplateField>

</Columns>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:GridView> </pre>
Richard Deeming 21-May-15 7:54am    
The easiest option is probably to store the data in the CommandArgument on the control:
<asp:ImageButton ... CommandArgument='<%# Eval("bookID") %>' .../>

In the event handler, you can then cast the sender parameter to the IButtonControl interface, which will let you access the CommandArgument property.

Alternatively, you could handle the button's Command event, which gives you a CommandEventArgs which includes the CommandArgument property.
Y.Ahmad 29-May-15 16:25pm    
I don't know how to cast the sender

I put the HTML code
<asp:ImageButton ID="IBKutub" ImageUrl='<%# Eval("bookID", "~/images/{0}.png") %>' runat="server" Height="120px" Width="90px" CommandArgument='<%# Eval("bookID") %>' />

But what about the C#?!.. I tried.. and used this..

protected void GVBooks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (sender.Equals("IBKutub"))
{
Response.Redirect("BookDetails.aspx");
}
}

Didn't success..
Richard Deeming 29-May-15 16:31pm    
Why are you suddenly using the grid's RowDataBound event? The sender of that event will be the GridView.

In your previous comment, you were using the Click event of the ImageButton:
<asp:ImageButton ID="IBKutub" runat="server"
ImageUrl='<%# Eval("bookID", "~/images/{0}.png") %>'
CommandArgument='<%# Eval("bookID") %>'
OnClick="IBKutub_Click"
/>


protected void IBKutub_Click(object sender, EventArgs e)
{
var button = sender as IButtonControl;
string bookID = (button == null) ? null : button.CommandArgument;
// Do something with the ID here...
}
Y.Ahmad 29-May-15 17:49pm    
I tried all events of grid view and Image button, but I didn't know what should I do..

Really.. wow!
Whats this command?!
string bookID = (button == null) ? null : button.CommandArgument;
a Super Command!
I didn't understand what's happened! but it success.. Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900