Home All Groups Group Topic Archive Search About
Author
6 Mar 2006 6:28 PM
Ben Schumacher
I have a GridView control within which I create all columns in code (VB).
The first few are BoundField controls and then a HyperLinkField control and
then finally a TemplateColumn control (so that I can have a checkbox for
each row).  To get a checkbox into the TemplateColumn I created a separate
class that implements the ITemplate interface and then instantiated in it a
CheckBox server control.  Everything is working fine until I do a postback.
Because I don't want to databind the GridView everytime, I do a "If Not
Page.IsPostback" to determing weather or not I need to create the columns,
create the datasource and then bind the data source.  Every column in the
GridView control looks perfect after postback except the TemplateColumn
within which the check box controls disappear.  What is the deal?  How can I
get this working?  Any help would be greatly appreciated.

Author
7 Mar 2006 3:09 PM
DWS
Ben,

I don't know exactly what your trying to accomplish but possibly you could
simply change your datasource to return an addtional default value boolean
column with your current results and render it as a check box.

select customer, sales, FALSE from sales

When needed I create on the fly controls for every postback.  Sometimes I
exclude on the fly controls from rendering for instance when the user clicks
a cancel button the cancel button has to be created to process its events but
should not render again.  Who knows if I'm doing it right?

Good Luck
DWS

PS Please vote for my designs "The Hand is Quicker Than the Eye" and
"Software"
http://blog.mix06.com/contest/gallery/default.aspx

Thank you
DWS

Show quoteHide quote
"Ben Schumacher" wrote:

> I have a GridView control within which I create all columns in code (VB).
> The first few are BoundField controls and then a HyperLinkField control and
> then finally a TemplateColumn control (so that I can have a checkbox for
> each row).  To get a checkbox into the TemplateColumn I created a separate
> class that implements the ITemplate interface and then instantiated in it a
> CheckBox server control.  Everything is working fine until I do a postback.
> Because I don't want to databind the GridView everytime, I do a "If Not
> Page.IsPostback" to determing weather or not I need to create the columns,
> create the datasource and then bind the data source.  Every column in the
> GridView control looks perfect after postback except the TemplateColumn
> within which the check box controls disappear.  What is the deal?  How can I
> get this working?  Any help would be greatly appreciated.
>
>
>
Author
7 Mar 2006 5:44 PM
CaffieneRush@gmail.com
I'm having the same problem and believe that the columns will have to
be programmatically created earlier in page_init rather than page_load
for the templated controls to appear.
Please update this post when you've found the solution.
Author
14 Mar 2006 4:40 PM
Phillip Williams
Ok Ben,

I added a demo to my website where I used 2 classes that implement the
ITemplate interface of the System.Web.UI namespace. I use the custom template
to programmatically add TemplateFields to the GridView based on the
attributes of DataColumns that are bound to those TemplateFields. Observe the
following features in this demo:

1.    I create a checkbox column (that allows selecting the row) for the
purpose of deleting multiple rows dynamically.
2.    If a column holds data of type string then the template will display a
TextBox upon edit and label during display. The size of the displayed field
will equal the number of characters multiplied by 6 pixels.
3.    If a column holds numeric data, then, upon Edit, a TextBox with both
validators for both the range of the data type and for required field if the
default value on the field cannot be null.
4.    If a column holds Boolean information then, upon edit, a radiobuttonlist
of Yes and No will be displayed
5.    If a column is read-only then no edit textbox will be displayed.
6.    The styles of each column would change based on the attributes; for
example primary keys will be in maroon color.
7.    Each GridView cell will display the column name, datatype and size when
hovered over it.

The GridView also implements sorting and allows multiple row selection for
deletion.

Show quoteHide quote
"Ben Schumacher" wrote:

> I have a GridView control within which I create all columns in code (VB).
> The first few are BoundField controls and then a HyperLinkField control and
> then finally a TemplateColumn control (so that I can have a checkbox for
> each row).  To get a checkbox into the TemplateColumn I created a separate
> class that implements the ITemplate interface and then instantiated in it a
> CheckBox server control.  Everything is working fine until I do a postback.
> Because I don't want to databind the GridView everytime, I do a "If Not
> Page.IsPostback" to determing weather or not I need to create the columns,
> create the datasource and then bind the data source.  Every column in the
> GridView control looks perfect after postback except the TemplateColumn
> within which the check box controls disappear.  What is the deal?  How can I
> get this working?  Any help would be greatly appreciated.
>
>
>
Author
28 Mar 2006 11:15 PM
CaffieneRush@gmail.com
You example code is very good.
Too bad the gridview itself was not created programmatically and so it
did not solve my problems.

Our grid (as well as all other controls) definitions are stored in a
database so we needed a grid that could bind to arbitrary data tables
at run time.
Oh and our boss wanted a grid that could do in-place data insertion -
that's the reason for using TemplatedFields.
We ended up having to subclass both GridView and ObjectDataSource.

My interim workaround to this problem was to do a databind within the
load event of our custom GridView.
Author
19 Apr 2006 6:49 PM
Michael Steele
Hello all, I have searched the web far and deep and came across this
thread. I am hoping that one of you can help me as we seem to be working
in the same area!

I have a GridView with dynamically generated templates columns. I have a
text box and label template columns being generated based on a
datatable. All works fine except when a post back occurs.

On Postback my template columns are not generated automatically. So far
I am "regenerating" them manually in the page init event.

My question(s) is

1. Is there a way for me access the values presented to the user on
postback? I don't want to rebind my datasource every time since I might
loose changes from the user.

2. Even when I recreate the columns for label they are not being loaded
with data from the viewstate?

3. Since I am creating a derived GridView object is there an easy to
many store and recreating the columns.

4. Why did MS make this so crazy?

5. How can I present a GridView with just the Header? If I don’t have
any data in my datasource my GridView doesn’t show up

Philip mentioned that he did a test project. Would you mind sharing some
of the knowledge with me?

*** Sent via Developersdex http://www.developersdex.com ***