Postback for DataGrid Columns Added in Code Behind not firing.

P

Pete Mahoney

I am trying to build a web custom control that displays a datagrid, so
first off I have no designer where I can drag and drop a datagrid and
then set properties from the user interface. I have to do everything
from the code behind. Most of the code follows below.

The problem is after I create my datagrid from the code behind and add
ButtonColumn's to the datagrid the items in those ButtonColumn's
should do a post back after being pressed. No such postback is ever
called from these ButtonColumn's and thus no server side code for
Edit, Update or Delete buttons can ever be called.

How can I get my datagrid ButtonColumn or EditCommandColumn to
properly do post back when they are created from the code behind?

Public Sub BuildDG()
dgDisplayFiles.ID = "dgDisplayFiles"
dgDisplayFiles.AutoGenerateColumns = False

dgDisplayFiles.DataKeyField = "FullName"
dgDisplayFiles.HeaderStyle.CssClass =
ViewState("HeaderStyle-CssClass")
dgDisplayFiles.ItemStyle.CssClass = ViewState("ItemStyle-CssClass")

Dim deleteColumn As New System.Web.UI.WebControls.ButtonColumn
deleteColumn.Text = "<img src='/images/common/delete.gif' border=0>"
deleteColumn.CommandName = "Delete"
deleteColumn.ButtonType = WebControls.ButtonColumnType.LinkButton

Dim renameColumn As New System.Web.UI.WebControls.EditCommandColumn
renameColumn.EditText = "<img src='/images/common/edit.gif' border=0>"
renameColumn.ButtonType = WebControls.ButtonColumnType.LinkButton

Dim previewColumn As New System.Web.UI.WebControls.HyperLinkColumn
previewColumn.Text = "<img src='/images/common/preview.gif' border=0>"
previewColumn.Target = "_blank"

Dim nameColumn As New System.Web.UI.WebControls.BoundColumn
nameColumn.DataField = "Name"
nameColumn.HeaderText = "Name"

Dim lastUpdatedColumn As New System.Web.UI.WebControls.BoundColumn
lastUpdatedColumn.DataField = "LastWriteTime"
lastUpdatedColumn.HeaderText = "Last Modified"

dgDisplayFiles.Columns.Add(deleteColumn)
dgDisplayFiles.Columns.Add(renameColumn)
dgDisplayFiles.Columns.Add(previewColumn)
dgDisplayFiles.Columns.Add(nameColumn)
dgDisplayFiles.Columns.Add(lastUpdatedColumn)
End Sub

Public Sub BindFiles()
Dim dir As New
DirectoryInfo(ViewState("strDirectoryLocation"))
If dir.Exists Then
dgDisplayFiles.DataSource =
dir.GetFiles(ViewState("strTypeOfFile"))
dgDisplayFiles.DataBind()
If dgDisplayFiles.Items.Count = 0 Then
dgDisplayFiles.Visible = False
Else
dgDisplayFiles.Visible = True
End If
End If
End Sub

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
dgDisplayFiles = New System.Web.UI.WebControls.DataGrid

BuildDG()

BindFiles()

dgDisplayFiles.RenderControl(output)
End Sub
 
R

Raghavendra T V

Hi Pete,

I guess the delegate ( events) in the initialize() are missing.
Check wether all delegates for datagrid are present in the initialize ().

VS.NET sometimes swallows this events ( i think its a bug )

Hope this helps you.

Thanks
Raghavendra
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top