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, the second button was actually a test. I should have taken it out before posting. I wanted a delete image for the user to be able to delete the row. I can use the button column for that, unless there is an image type column that would be easily implemented.
One thing about the button column that I cannot figure out is how to set the text of the button?
Thanks for your previous response. That does answer the original question.
mhurter said:One thing about the button column that I cannot figure out is how to set the text of the button?
I'm glad to have helped.
How do we add the image to the button if we select column of Type="Button"
here is the jscript to set the value of the button.
igtbl_getGridById(gridName).Rows.getRowById(rowId).getCellFromKey("ColName").setValue("your text");
Here is the C# do this on initializerow
e.Row.Cells[0].Value = "your text";
that would be very nice to know on this end. Please share here if you find out how to winned.