Results 1 to 5 of 5

Thread: creating a dynamic button inside a datagrid

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    creating a dynamic button inside a datagrid

    I would like to dynamically create a button in the "footer" section of a datagrid. Once this button is created, I would like to wire up an existing event (by another button) to this new dynamically created button.

    I have the following code in my Item_DAtabound event of the datagrid.

    <code>
    If e.Item.ItemType = ListItemType.Footer Then
    Dim btnSave2 As System.Web.UI.WebControls.Button = New System.Web.UI.WebControls.Button
    e.Item.Cells(10).Controls.Add(btnSave2)
    btnSave2.Width.Pixel(105)
    btnSave2.Height.Pixel(32)
    btnSave2.Text = "Save"
    AddHandler btnSave2.Click, AddressOf btnSave_Click
    End If
    </code>

    The button gets displayed, but after it's clicked, it disappears. In addition, it doesn't go into the btnSave_Click event (below):

    <code>
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    .
    .
    .
    End sub
    </code>

    What do I need to do in order to get this to work???
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Move your code to the ItemCreated event handler, the ItemDataBound event will only be fired if you're databinding which on postback you probably won't be.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17
    The save button does not get created inise the item_created event....
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Yes it does, here's aspx the code:
    Code:
    <%@ Page Language="vb" 
    	AutoEventWireup="false" 
    	Codebehind="DataGridButton.aspx.vb" 
    	Inherits="localhost.DataGridButton"%>
    <html>
    	<body>
    		<form runat="server">
    			<asp:DataGrid ID="gridAuthors" 
    				AutoGenerateColumns="True" 
    				ShowFooter="True" 
    				Runat="server"/>
    		</form>
    	</body>
    </html>
    ...and here's the code behind:
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.SqlClient
    4.  
    5. Public Class DataGridButton : Inherits System.Web.UI.Page
    6.     Protected WithEvents gridAuthors As DataGrid
    7.     Protected Overrides Sub OnInit(ByVal e As EventArgs)
    8.         If Not Page.IsPostBack Then
    9.             bindGrid()
    10.         End If
    11.     End Sub
    12.     Private Sub bindGrid()
    13.         gridAuthors.DataSource = getAuthors()
    14.         gridAuthors.DataBind()
    15.     End Sub
    16.     Private Function getAuthors() As DataSet
    17.         Dim ds As New DataSet
    18.         If Not Cache.Item("Authors") Is Nothing Then
    19.             Response.Write("Cache Hit!")
    20.             ds = CType(Cache.Item("Authors"), DataSet)
    21.         Else
    22.             Response.Write("Repopulating...")
    23.             Dim connString As String = "user id=sa;password=sa;database=pubs;server=DeathAngel;"
    24.             Dim cn As New SqlConnection(connString)
    25.             Dim cmdText As String = "Select * From Authors"
    26.             Dim cmd As New SqlCommand(cmdText, cn)
    27.             Dim da As New SqlDataAdapter(cmd)
    28.             ds = New DataSet
    29.             da.Fill(ds)
    30.             Cache.Item("Authors") = ds
    31.         End If
    32.         Return ds
    33.     End Function
    34.     Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    35.         Response.Write("Saving...")
    36.     End Sub
    37.     Private Sub gridAuthors_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gridAuthors.ItemCreated
    38.         If e.Item.ItemType = ListItemType.Footer Then
    39.             Dim btnSave As New Button
    40.             btnSave.Text = "Save"
    41.             AddHandler btnSave.Click, AddressOf btnSave_Click
    42.             e.Item.Cells(7).Controls.Add(btnSave)
    43.         End If
    44.     End Sub
    45.  
    46. End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    dynamically create a button

    You're right......

    I forgot to set the grid to create the Footer template at design time. Since it didn't work before, I unchecked the item to create the Footer......
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

Posting Permissions

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



Click Here to Expand Forum to Full Width