|
-
Aug 30th, 2003, 04:41 PM
#1
Thread Starter
Junior Member
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.
-
Aug 31st, 2003, 11:33 AM
#2
Hyperactive Member
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.
-
Sep 1st, 2003, 10:57 AM
#3
Thread Starter
Junior Member
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.
-
Sep 1st, 2003, 11:08 AM
#4
Hyperactive Member
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:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class DataGridButton : Inherits System.Web.UI.Page
Protected WithEvents gridAuthors As DataGrid
Protected Overrides Sub OnInit(ByVal e As EventArgs)
If Not Page.IsPostBack Then
bindGrid()
End If
End Sub
Private Sub bindGrid()
gridAuthors.DataSource = getAuthors()
gridAuthors.DataBind()
End Sub
Private Function getAuthors() As DataSet
Dim ds As New DataSet
If Not Cache.Item("Authors") Is Nothing Then
Response.Write("Cache Hit!")
ds = CType(Cache.Item("Authors"), DataSet)
Else
Response.Write("Repopulating...")
Dim connString As String = "user id=sa;password=sa;database=pubs;server=DeathAngel;"
Dim cn As New SqlConnection(connString)
Dim cmdText As String = "Select * From Authors"
Dim cmd As New SqlCommand(cmdText, cn)
Dim da As New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds)
Cache.Item("Authors") = ds
End If
Return ds
End Function
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Write("Saving...")
End Sub
Private Sub gridAuthors_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gridAuthors.ItemCreated
If e.Item.ItemType = ListItemType.Footer Then
Dim btnSave As New Button
btnSave.Text = "Save"
AddHandler btnSave.Click, AddressOf btnSave_Click
e.Item.Cells(7).Controls.Add(btnSave)
End If
End Sub
End Class
-
Sep 1st, 2003, 07:13 PM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|