Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Created ButtonColumn Not firing event

Status
Not open for further replies.
Oct 15, 2003
145
US
I am creating a datagrid dynamically,I have 3 columns -
1 - data bound column from a dataset that is generated based on my parenty key
2 - Input text box
3 - button that will be used to save the data entered in the text box


Problem:
My button does nothing. It's like it doesn't fire any event.

Code:
I am trying to create a child datagrid ( a datagrid that is displayed in the row beneath a "parent" row that dispalys many rows from another table) - so I call this function within the parent datagrids ItemDataBound function.

' code to create my child dataset: oBillDS
' code to create the datagrid
NewDg = New DataGrid
NewDg.AutoGenerateColumns = False
Dim cDgCol As DataColumn

For Each cDgCol In oBillDS.Tables(0).Columns
If cDgCol.ColumnName = "Pnum" Then
NewDg.Columns.Add(CreateBoundControl(cDgCol))
End If

Next
NewDg.Width = Unit.Percentage(100)

Dim tc1 As New TemplateColumn
tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, "Amount Paid")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "From DB Amt Paid")
tc1.EditItemTemplate = New DataGridTemplate(ListItemType.EditItem, "AmtPaid")
tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, "NoFooter")
NewDg.Columns.Add(tc1)

Dim txtBoxColumn As New ButtonColumn

txtBoxColumn.CommandName = "MyCommmand"
txtBoxColumn.Text = "Update"
txtBoxColumn.ButtonType = ButtonColumnType.PushButton
' my attempt to add a handler for my datagrid
AddHandler NewDg.ItemCommand, AddressOf NewDG_EditCommand
NewDg.Columns.Add(txtBoxColumn)
NewDg.EnableViewState = True

NewDg.EditItemIndex = 0

NewDg.DataSource = oBillDS
NewDg.DataBind()

Dim sw As New System.IO.StringWriter
Dim htw As New System.Web.UI.HtmlTextWriter(sw)
NewDg.RenderControl(htw)

Dim DivStart As String = "<DIV id=uniquename" + e.Item.ItemIndex.ToString("' style='DISPLAY: none>'")
Dim DivBody As String = sw.ToString()
Dim DivEnd As String = "</DIV>"
Dim FullDIV As String = DivStart + DivBody + DivEnd

Dim LastCellPosition As Integer = e.Item.Cells.Count - 1
Dim NewCellPosition As Integer = e.Item.Cells.Count - 2

e.Item.Cells(0).ID = "CellInfo" + e.Item.ItemIndex.ToString()

If e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(LastCellPosition).Controls.Add(New LiteralControl("</td><tr><td bgcolor='f5f5f5'></td><td colspan='" + NewCellPosition.ToString + "'>" + FullDIV))
Else
e.Item.Cells(LastCellPosition).Controls.Add(New LiteralControl("</td><tr><td bgcolor='d3d3d3'></td><td colspan='" + NewCellPosition.ToString + "'>" + FullDIV))

End If
e.Item.Cells(0).Attributes("onmouseover") = "this.style.cursor='pointer'"
e.Item.Cells(0).Attributes("onmouseout") = "this.style.cursor='pointer'"


' Sub that I thought would fire - but doesn't

Public Sub NewDG_EditCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Response.Redirect(" End Sub

Does anyone have any suggestions as to why my event is never fired. I thought the buttonColumn would fire the ItemCommand event of the datagrid --- if anyone has any thoughts - I'd greatly appreciate them.
 
Well that is just weird - cuz it won't work for me! however using your code as an example -- I've been successful in getting my datagrid to look like I wanted - now I just have to code the functionality of the events fired and make the grid look right - it's a bit funky

Your example really helped me - thank you!
 
I think I know what the problem is... Did you wire up the events in the generated code's InitializeComponent method? The events won't fire if you don't wire them up.

Code:
private void InitializeComponent()
{
	this.dgParent.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgParent_ItemCommand);
	this.dgParent.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgParent_ItemDataBound);
	this.Load += new System.EventHandler(this.Page_Load);

}




 
KDavie - actually I had added that part - but only partially -- I didn't have the itemdatabound...but I had the itemcommand & load


so now it does work -- and that is what i had wanted! Well, close - I would LOVE to not have the user have to press update/cancel after each row - and just be able to open the screen and have a text box and then a button at the bottom and save the data. But I don't think that's how the edit works - so I'm going to get this working then eventually look into changing it. I have an idea that may work - we'll see :)

You have been a huge help - thank you - I was able to use your example and tie it into what I had - and now it is pretty close to what I want.

I was giving up on this idea. Thank you to all who gave me help on this!
 
Great, glad to hear you have it working. You can acheive your desired look and feel by implementing your own form of editing, rather than using the built in edit features.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top