Postback for DataGrid Columns Added in Code Behind not firing.

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Postback for DataGrid Columns Added in Code Behind not firing.

    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 Guest

  2. Similar Questions and Discussions

    1. Help with Dynamic Columns in Datagrid and Postback
      Hi, I'm having problems retaining my dynamic columns during postbacks. I've read that if you create dynamic columns at runtime, you have to add...
    2. Autogenerated Datagrid columns rebinding problem on postback
      Just to give a brief background what I'm trying to do, is I have a DataGrid with 2 static columns, and the rest are autogenerated (which have...
    3. Sort not firing PostBack event using Dynamic Columns
      I am having a problem with the DataGrid control and sorting, and have found it's realted to if you use dynamic columns or not. If I set the columns...
    4. Keeping DataGrid Columns through Postback
      I am doing the exact same thing you are requesting. Here is what you do: 1. You must wire-up a method to the eventhandler of the grid called...
    5. Datagrid Dynamic Columns Postback and SelectedIndexChanged problems
      All, I have a datagrid that has a few design time columns (two buttons - one for select and one for delete) and the programatically I add...
  3. #2

    Default Re: Postback for DataGrid Columns Added in Code Behind not firing.

    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" <landerud.pete@students.uwlax.edu> wrote in message
    news:189dfa06.0408301049.4e699080@posting.google.c om...
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139