Hello, I have an ultrawebgrid. The first column is a template column with an imagebutton as the only control. once I click the add new button, the row appears, but the control is not there. Any help would be appreciated. Below is the templated column in the webgrid.
<igtbl:TemplatedColumn AllowGroupBy="No" FilterIcon="False" ServerOnly="false" > <CellTemplate> <asp:ImageButton ID="btnDeleteResource" runat="server" ImageUrl="~/images/delete.png" CommandArgument="Delete" OnClientClick="return confirm('Delete Selected Row?');" CommandName="Delete" /> <asp:Button runat="server" Text="test" /> </CellTemplate> <Header> <RowLayoutColumnInfo OriginX="1"></RowLayoutColumnInfo> </Header> <Footer> <RowLayoutColumnInfo OriginX="1"></RowLayoutColumnInfo> </Footer> </igtbl:TemplatedColumn>
Controls in a templated column must be instantiated on the server before they can be displayed on the client. This typically requires you to perform a postback on the page before the controls for the new cell in the templated column will appear.
If it's possible for you to do so, it's better to use the built-in functionality of the grid, instead of column templates. It is typically less work to implement. More importantly, it provides better performance, since instantiating ASP.NET templates is a computationally-expensive process. Of course, templated columns are more flexible, so there are some approaches for which you'll have to use a tempalted column (or consider an alternate approach).
For a situation like this, where you have a button that is meant to perform an action on the grid row, I recommend to use a WebGrid column with a Type of Button, instead of a templated column containing an image button. You can use the server-side and client-side CellClickButton events of the grid to process what to do when such a button is clicked, both which give you references to the cell that contains the button that was clicked.
I notice that you have two buttons in your column template. For the Button-type column, you can have exactly one button in the column. If displaying multiple buttons is important, you may want to have one column for each button. If you absolutely need to have them all in the same cell, then a templated column is your only viable option.
Vince, I have a templated column that shows a usercontrol inside it. When the user types inside the control, hidden cells get loaded with the data. Everything works fine until I delete a row (fires DeleteRow) but the event is cancelled on the server because of a business rule. I set e.Cancel = true and the deleted row reappeares, but the data is missing in my custom control (but it is present in the auxiliary hidden cells). I found out it's because the InitializeRow event does not get fired (that's where I load the data into my control when getting it from the database). How do I solve this problem? should I use a different event to load the data into the control?