Dynamic CheckBox CheckedChanged event not firing

D

dilip.movva

Hi,
In the ItemDataBound of my datagrid I am creating a checkbox
dynamically. I have the eventhandler defined for CheckedChanged of the
check box. The event is not firing. The grid is bound in the
Page_Prerender method. My code is something like this

Private Sub grdMissingData_ItemDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
grdMissingData.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
If ((e.Item.Cells(5).Text <> "Yes") And
(rdDataTypes.SelectedIndex = 0)) Then
Dim cbx As New CheckBox

cbx.EnableViewState = True
cbx.AutoPostBack = True
AddHandler cbx.CheckedChanged, AddressOf
Me.CheckBoxChangedEvent

cbx.ID = e.Item.ItemIndex

Dim productIDLabel As Label =
CType(e.Item.Cells(0).FindControl("prodid"), Label)
cbx.Checked =
IsOnscreenItemSelected(productIDLabel.Text.Trim)

'cbx.ID = e.Item.ItemIndex
e.Item.Cells(5).HorizontalAlign =
HorizontalAlign.Center
e.Item.Cells(5).Controls.Add(cbx)


End If

End If
End Sub


Private Sub CheckBoxChangedEvent(ByVal sender As Object, ByVal e As
System.EventArgs)

' Changed Event Method
End Sub



Is there some thing i am missing. Can someone help me.

Thanks
Dilip
 
G

Guest

If the datagrid is bound at the Page_Prerender stage then the veiwstate of
that eventhandler would be lost upon postback. You have to do the datagrid
binding in the page initialization or page loading stages. For a better
understanding
of viewstate during the page life cycle refer to this article on the MSDN
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

If you are waiting for some data until the page_prerender stage to decide on
databinding the datagrid then persist these data in the ViewState or Session
in order to do the databinding during the page_load upon postback and thereby
preserve the viewstate of the events. I have here a sample that demonstrates
this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx
 
T

tdavisjr

You would have to capture this event in the ItemCommand event within
the datagrid. Any button clicked in the datagrid raises this event.

Then you would have to get a reference to the checkbox button by
something like this
CheckBox mycheckbox = (ChackBox) e.Items.Cells(0).Controls(0)

Cells(0) is the very first cell
Contols(0) is the very first control in that cell.

You need to change the ordinals to where the checkbox appears in your
datagrid.

Now that you have a refenence to the checkbox , then you can do
whatever you like.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top