|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamic ImageButton inside GridView ASP .NETI 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 !! *** Sent via Developersdex http://www.developersdex.com *** 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> Show quoteHide quote "J S" <joan_sabo***@yahoo.com> ha scritto nel messaggio news:uycf8e%23tGHA.2260@TK2MSFTNGP02.phx.gbl... > > 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 !! > > *** Sent via Developersdex http://www.developersdex.com *** 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> Show quoteHide quote "J S" <joan_sabo***@yahoo.com> ha scritto nel messaggio news:uycf8e%23tGHA.2260@TK2MSFTNGP02.phx.gbl... > > 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 !! > > *** Sent via Developersdex http://www.developersdex.com ***
dynamic image? store and retrieve properties values? resizable web control?
Hiding columns in GridView control Create A 3-Day Window Between 2 Calendars DropDownList -- setting by text? label control Redirecting to another page after a specified time User Control Renders on Separate Line Integration with office Recommended way to add child controls in CreateChildControls Problem with capturing enter key in TextBox within UserControl |
|||||||||||||||||||||||