Postback for DataGrid Columns Added in Code Behind not firing.

Discussion in 'ASP .Net Datagrid Control' started by Pete Mahoney, Aug 30, 2004.

  1. Pete Mahoney

    Pete Mahoney Guest

    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
     
    Pete Mahoney, Aug 30, 2004
    #1
    1. Advertising

  2. 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
    "Pete Mahoney" <> wrote in message
    news:...
    > 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
     
    Raghavendra T V, Aug 31, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Tucker
    Replies:
    2
    Views:
    2,572
    Ken Tucker
    Jul 6, 2003
  2. Ken Tucker

    DataGrid SortCommand not firing using code-behind??

    Ken Tucker, Jul 5, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    206
    Ken Tucker
    Jul 6, 2003
  3. tsarna

    Sort not firing PostBack event using Dynamic Columns

    tsarna, Jun 23, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    191
    tsarna
    Jun 28, 2004
  4. Replies:
    1
    Views:
    351
    Teemu Keiski
    Apr 17, 2006
  5. Replies:
    1
    Views:
    213
Loading...

Share This Page