| | |
Dynamic Buttons in Datagrid Not Firing
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi
I've got a datagrid that I'm populating and depending on the data returned depends on what button I add. That all works fine until I click on the button and it posts the page and loses everything. Basically I can't get the button to fire.
Please help!
Lbob
This is my code
I've got a datagrid that I'm populating and depending on the data returned depends on what button I add. That all works fine until I click on the button and it posts the page and loses everything. Basically I can't get the button to fire.
Please help!
Lbob
This is my code
ASP.NET Syntax (Toggle Plain Text)
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then bindDG() End If End Sub Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound If e.Item.ItemType = ListItemType.Item Then Dim btn As Button btn = New Button btn.Text = "Click Me" btn.ID = e.Item.Cells(i).Text AddHandler btn.Click, AddressOf btn_Click e.Item.Cells(i).Controls.Add(btn) end if End Sub Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Select Case CType(sender, Button).Text Case "Click Me" trace.write("Hello") End Select
because you are dynamically creating buttons they are not retaining state on postback and therefore wont fire the event assigned to them. You need to re-instantiate each dynamic control individually. E.g. when creating each control assign it an ID and save this to the viewstate. Then on page load loop through viewstate to find you controls and rebuild them. Some good examples are here
or you could use the "ItemCommand" event of the datagrid. This will fire if you set a commandname to your buttons when you create them and you wont need to retain state as above (Which can be messy if not implemented properly).
or you could use the "ItemCommand" event of the datagrid. This will fire if you set a commandname to your buttons when you create them and you wont need to retain state as above (Which can be messy if not implemented properly).
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: How to Create BookMarks in ASP.Net
- Next Thread: image button problem
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax alltypeofvideos appliances asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions compatible content contenttype control countryselector courier dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol fileuploader fill findcontrol flash flv formview gridview gudi iis javascript list listbox login menu microsoft mouse mssql nameisnotdeclared news novell numerical opera order panelmasterpagebuttoncontrols problem radio ratings redirect registration relationaldatabases reportemail schoolproject search security serializesmo.table sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl tracking treeview validatedate validation vb.net videos vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming webservice wizard xsl





