Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid LinkButton 1

Status
Not open for further replies.

zatch

MIS
Oct 11, 2001
75
US
I have a datagrid - datagrid1.
At runtime, I am adding a ButtonColumn (Link Button) like so:

Dim bc As New ButtonColumn

bc.HeaderText = Format(dr.Item("PAYMENT_DATE"), "Short Date")
bc.DataTextField = Format(dr.Item("PAYMENT_DATE"), "Short Date")
bc.Text = Format(dr.Item("PAYMENT_DATE"), "Short Date")
bc.ButtonType = ButtonColumnType.LinkButton
bc.CommandName = "Test"
Me.DataGrid1.Columns.Add(bc)

This works fine. It creates the proper linkbutton with the correct data.

Now, I am under the impression that when I click the buttons in this column, it is to raise the Datagrid1.ItemCommand event at which point I can find out what to do next by grabbing the .commandname text. This is not happening. When I click the button, the newly created column disappears and the datagrid reverts back to its original state (minus the buttoncolumn.) It's a postback with no event. The Datagrid1.ItemCommand event is not raised.

Does anyone have any experience in adding ButtonColumns to datagrids at runtime? If so, any advice on why this is not working correctly?

Any advice is greatly appreciated.

Zatch
 
As it is a dynamic control, it needs to be created each time the page is created (else how does the page know it should exist?).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for your reply. Any ideas as to why the button is not raising the event?

Zatch
 
If you've followed my advise and created it each time, then no I'm not sure as it seems to work fine for me:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim dt As New DataTable
        Dim dr As DataRow

        dt.Columns.Add("Column1")
        For i As Integer = 1 To 10
            dr = dt.NewRow
            dr(0) = i
            dt.Rows.Add(dr)
        Next

        Dim bc As New ButtonColumn
        bc.HeaderText = "Button Column"
        bc.Text = "Button"
        bc.ButtonType = ButtonColumnType.LinkButton
        bc.CommandName = "Test"
        DataGrid1.Columns.Add(bc)

        DataGrid1.DataSource = dt
        DataGrid1.DataBind()

    End Sub

    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
        Response.Write("I Fired!")
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
You are right. I thought I had two issues here, when really they were related. Thank you.

Zatch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top