adding functionality to a image button asp.net C#

Posted by Arianule under ASP.NET on 5/3/2011 | Points: 10 | Views : 9523 | Status : [Member] | Replies : 3
Hi there

I have a gridview which I binded with an access database and within in this gridview I have and ImageButtonField and a regular button as well. The image button field displays and image and the url of this image is retrieved from the database.
When I click on the regular button then I am directed to a different page where I can few detail of the row entry of the gridview.
I want to achieve the same thing with the image button but I am really struggling with this.
Each of the imgages of each concecutive image button is of course different and the data in each row is of cource unique .
Any advice as to how to add functionality the imagebuttonField so as to be redirected from the gridview page(without errors) and display the row information on a new page?.
I have done it relatively easily with the regualar button field and here is the code for the button field.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "addToSession")
{
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;

//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");
}
}
}


here is the html for the image button.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl='<%#Eval("pictureUrl") %>'
CommandName="ratedBooks" />
</ItemTemplate>
</asp:TemplateField>

Thanks and kind regards
Arian




Responses

Posted by: Ndebata on: 5/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

There are several ways to implement this functionality.
1. Add OnClick Event for ImageButton
 <asp:TemplateField>

<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl='<%#Eval("pictureUrl") %>'
OnClick="img_click" />
</ItemTemplate>
</asp:TemplateField>

Then in code behind
public void img_click(object sender, EventArgs e)

{
GridViewRow selectedRow = ((Control)sender).Parent.NamingContainer as GridViewRow;
//use this row as you are using in case of normal button.
}


2. You can specify CommandName and CommandArgument for Imagebutton also and you can check the command name in rowcommand event and do any operation.

Thanks,
Debata

Arianule, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arianule on: 5/3/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks for the advice
Most helpfull
Arian

Arianule, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prafullshrivastava on: 5/3/2011 [Member] Starter | Points: 25

Up
0
Down
hi if you want to use image button in grid view then try this:-

Add a image button in your grid view-

<ItemTemplate>
<asp:ImageButton ID="ibtnNewPage" runat="server" ImageUrl="~/Images/edit.png" CommandName="newpage" />
</ItemTemplate>

and handel event of rowcommand and write this code-

protected void gvStudent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "newpage")
{
Response.Redirect("frmYourPage.aspx");
}
}



Pshrivastava

Arianule, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response