Dynamic ImageButton inside GridView ASP .NET

J

J S

Good Morning Everyone,

I have a problem, i've spend too much time trying to figure out how can
i get the "click event" from a ImageButton inside a GridView. I have a
dynamic generated GridView, after i create the columns and all, i have
to create an ImageButton for each one of the elements on the GridView,
i'm trying to make something like a dinamic two dimensional array for
the asp page.

The problem is, i could create all the columns and buttons with the
commandName and everything else for each one, i thought the
GridView_RowCommand could catch the event from the Button, but it
doesn't so i don´t know where can i catch it...

I've tried to make the columns of type TemplateField, but when i try to
Add the ImageButton, i can't because of the components types.

I'm out of ideas, if someone had the same problem and knows how to solve
it, please tell me :)

Thanks !!
 
A

Alessandro Zifiglio

hi Joan, for me it works well. Below i am pasting a simple test page i used
as per your description. I am using a usercontrol as the ItemTemplate for
the dynamic GridView Control. For more regarding this, you can reference
this document on msdn :
http://msdn2.microsoft.com/en-us/library/6d5z5yty.aspx

I also add images dynamically in the GridViews RowDataBound method. It
works well, so let me know what you are doing differently.

Regards,
Alessandro Zifiglio
Http://www.AsyncUI.net

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Data" %>

<script runat="server">
GridView gv;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gv.DataSource = CreateDataSource();
gv.DataBind();
}
}

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("StringValue1", typeof(string)));
dt.Columns.Add(new DataColumn("StringValue2", typeof(string)));

for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = "This button is on row no : " + i;
dr[1] = "Row : " + i.ToString();
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton ib = new ImageButton();
ib.ID = "ImageButton1";
ib.ImageUrl = ResolveClientUrl("~/url-to-your-image");
ib.CommandName = "ImageButton";
e.Row.Cells[0].Controls.Add(ib);
}
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
System.Data.DataRowView drv;
drv = (System.Data.DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv != null)
{
ImageButton ib =
(ImageButton)e.Row.FindControl("ImageButton1");
String stringValue2 = drv[1].ToString();
ib.CommandArgument = stringValue2;
}
}
}

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ImageButton")
{
ImageButton ib = (ImageButton)e.CommandSource;
ImageButton_Click(ib);
}
}

private void ImageButton_Click(ImageButton ib)
{
Response.Write(ib.CommandArgument);
}


protected void Page_Init(object sender, EventArgs e)
{
/* Best create your gridview
* in this phase, so it
* participates properly
* in your pages life-cycle
* starting from the first
* phase.
*/
gv = new GridView();
gv.ID = "GridView1";
PlaceHolder1.Controls.Add(gv);
gv.AutoGenerateColumns = false;
TemplateField tf = new TemplateField();
tf.HeaderText = "Template Column1";
// create the data rows
tf.ItemTemplate = Page.LoadTemplate("UserControlItemTemplate.ascx");
gv.Columns.Add(tf);
gv.RowCreated += gv_RowCreated;
gv.RowDataBound += gv_RowDataBound;
gv.RowCommand += gv_RowCommand;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeHolder ID="PlaceHolder1"
runat="server"></asp:placeHolder>
</div>
</form>
</body>
</html>
 
A

Alessandro Zifiglio

Joan, This is the code i have in the UserControl, which i am using as the
ItemTemplate, i forgot to paste it in my previous post sample code. I
include it here for completion, though its really minimum code =P

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net

<%@ Control Language="C#" ClassName="UserControlItemTemplate" %>

<script runat="server">

</script>

<asp:Label ID="Label1" runat="server"
Text='<%# Eval("StringValue1") %>'></asp:Label>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top