Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help getting my events set up correctly. Here's what's going on.

I have a project where I don't know the number or location of GridViews until the user selects some options. I need to do this all on one asp page.

I have figured out that I can create and populate a GridView dynamically and add it to a placeholder's controls.
ASP.NET
<asp:PlaceHolder ID="phTimeEntryDay" runat="server"></asp:PlaceHolder>

VB
Protected WithEvents gvTimeEntry As GridView = New GridView()
phTimeEntryDay.Controls.Add(gvTimeEntry)

I have added the edit and delete commands using code:
VB
Dim cColumn As New CommandField

cColumn.ButtonType = ButtonType.Link
cColumn.CancelText = "Cancel"
cColumn.UpdateText = "Update"
cColumn.EditText = "Edit"
cColumn.DeleteText = "Delete"
cColumn.ShowEditButton = True
cColumn.ShowDeleteButton = True
cColumn.ShowCancelButton = True

gvTimeEntry.Columns.Add(cColumn)

This does set up the correct postback javascript events, but nothing happens when I click the links and the page is posted back.

In another project where I created the gridview at design-time, I can't find any code created to handle the events that I can copy to my project that does everything at run-time.
XML
<asp:GridView ID="GridViewTimeSheet" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    DataKeyNames="EmpPerNo,FromDate,ToDate,EntrySequence" DataSourceID="SqlDataSourcetimeEntry"
    ShowFooter="True" EmptyDataText="No Data Available.">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
...

The only complete example I've found uses DataGrid in C# instead of GridView and there are enough differences in DataGrid and GridView that I can't adapt it completely.

Examples in VB or C# are fine if you have some.

Any help would be greatly appreciated.

Thanks,
Bryan Lee
Posted
Comments
aidin Tajadod 10-May-12 12:33pm    
Hi,
I can not see anywhere in your code to define grids events. Try to attach the desiger events by using AddHandler.
ARBebopKid 10-May-12 13:02pm    
I'm lost at the disigner events. I can't figure out how the designer is handling events so I don't know what events to Add.

1 solution

I found this reference at msdn that gives me examples of the events I need to handle and how to handle them.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.90).aspx[^]

Thank you, Aidin, for jogging my brain.
 
Share this answer
 
Comments
Maciej Los 10-May-12 13:44pm    
ARBebopKid, You are interesting man ;)
You ask a question and answer to yourself...
ARBebopKid 10-May-12 13:46pm    
I find I often can't figure out the answers until I've already asked for help. :)
aidin Tajadod 10-May-12 17:54pm    
You are very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900