I have a DataGrid that I'm adding CheckBox controls to at runtime (in
the code behind) and I'm not sure if I'm doing it correctly.
First of all, I noticed that the MyDataGrid.Columns.Add() method expects
a DataGridColumn so I instantiated an object of type TemplateColumn that
I had hoped I could add a CheckBox to. I soon discovered that the
ItemTemplate property of the TemplateColumn class returned an object
that had implemented the ITemplate interface and the CheckBox class did
not implement it out of the box.
From there, it meant that I would have to create a new class, which
inherited from CheckBox and implemented the ITemplate interface. I did
that and everything works (I have a CheckBox on my DataGrid), but it
seemed like the long way around and I figured there must be an easier
way. For example, how does ASP.NET add a CheckBox to a DataGrid if I
had just added it to my HTML? When I add a CheckBox in HTML, I never
have to implement the ITemplate interface --it appears that it happens
automatically. So what goes on behind the scenes that makes this possible?
As I said, I successfully added a CheckBox to my DataGrid; however, it
meant creating a new class, which I'd rather not use if it's possible.
Also, I'm having a strange issue come up that I think might be related
to my custom class.
What is the *proper* way to add a CheckBox to a DataGrid in code?
Thank you in advance,
--
Sean 3 3088
Options:
1. Switch to repeater. Allows you to play with the HTML table as you wish.
More flexible, but a bit harder to maintain.
2. Add dynamically using Add(). If you are simply adding the same way for
each row, this can be easy to maintain. If you persist the state or need
different states based on data, this is a pain.
3. Use the DataGrid events. You can catch when a row is being added and
dynamically add the cell at this time. The potential downside is creating a
very tightly coupled/dependent UI.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"Fao, Sean" wrote: I have a DataGrid that I'm adding CheckBox controls to at runtime (in the code behind) and I'm not sure if I'm doing it correctly.
First of all, I noticed that the MyDataGrid.Columns.Add() method expects a DataGridColumn so I instantiated an object of type TemplateColumn that I had hoped I could add a CheckBox to. I soon discovered that the ItemTemplate property of the TemplateColumn class returned an object that had implemented the ITemplate interface and the CheckBox class did not implement it out of the box.
From there, it meant that I would have to create a new class, which inherited from CheckBox and implemented the ITemplate interface. I did that and everything works (I have a CheckBox on my DataGrid), but it seemed like the long way around and I figured there must be an easier way. For example, how does ASP.NET add a CheckBox to a DataGrid if I had just added it to my HTML? When I add a CheckBox in HTML, I never have to implement the ITemplate interface --it appears that it happens automatically. So what goes on behind the scenes that makes this possible?
As I said, I successfully added a CheckBox to my DataGrid; however, it meant creating a new class, which I'd rather not use if it's possible. Also, I'm having a strange issue come up that I think might be related to my custom class.
What is the *proper* way to add a CheckBox to a DataGrid in code?
Thank you in advance,
-- Sean
Hi Gregory,
First of all, thank you very much for your response.
Cowboy (Gregory A. Beamer) - MVP wrote: Options:
1. Switch to repeater. Allows you to play with the HTML table as you wish. More flexible, but a bit harder to maintain.
I actually created two template pages so that I could later decide which
version I prefer. I need the ability to "Select" a record, so a
Repeater was out of the question. Fortunately, a DataList *did* provide
the functionality I need (minus paging and the ability to sort). I
successfully implemented paging support in a custom DataList control by
creating a SQL Stored Procedure with paging support. I have not yet
implemented sorting in my custom DataList control and probably will not
do so (yet). The other template (the one with the DataGrid), however,
does [obviously] have the ability to sort.
At any rate, adding CheckBox's in a DataList was quite a bit easier than
implementing it in a DataGrid. However, it's important to note that the
DataGrid control that I'm using is actually another custom control,
which inherits from the DataGrid class and implements a few nice
features. I'd like to keep these additional features because it would
make this page consistent with other pages that also use the custom
DataGrid control.
2. Add dynamically using Add(). If you are simply adding the same way for each row, this can be easy to maintain. If you persist the state or need different states based on data, this is a pain.
Unless you're referring to something else, this is what I'm already
doing (as well as the way that I prefer). The problem is that it
required that I create a custom CheckBox class, which I thought was the
long way around.
3. Use the DataGrid events. You can catch when a row is being added and dynamically add the cell at this time. The potential downside is creating a very tightly coupled/dependent UI.
I'd prefer to avoid this approach because of the potential downside you
listed above.
Thank you again for your response,
--
Sean
Fao, Sean wrote: What is the *proper* way to add a CheckBox to a DataGrid in code?
Anybody? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: The_Rave |
last post by:
Hi everyone,
I'm trying to add my own template columns to the property builder of
..NET.
E.g. a checkbox column, or an image column.
But I can't find the sources of the wizard, or a way to add...
|
by: Jim Heavey |
last post by:
Trying to figure out the technique which should be used
to add rows to a datagrid. I am thinking that I would
want an "Add" button on the footer, but I am not quite
sure how to do that. Is that...
|
by: DJ Dev |
last post by:
Hi,
I am stuck at a problem for 3 days now. I create runtime datagrids
depending on the user selections and they may vary from 2-10 depending
on user selection at runtime. The datagrids are...
|
by: Sjaakie Helderhorst |
last post by:
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do...
|
by: Bob Hollness |
last post by:
Hi group. I am a newbie to ASP.NET as you will see from some of the
questions I may ask!
I have a datagrid which I have populated from a database. It works great!
I have added a column, via...
|
by: Dexter |
last post by:
Hello all,
I have a datagrid with a template column with checkbox, and at this template
column,
at header section, i have a checkbox with autopostback property as true,
and when i check this...
|
by: sianan |
last post by:
I tried to use the following example, to add a checkbox column to a
DataGrid in an ASP.NET application:
http://www.codeproject.com/aspnet/datagridcheckbox.asp
For some reason, I simply CAN'T get...
|
by: Rekha |
last post by:
The data is filled in datagrid. I want to know how to add a checkbox column
in datagrid?
In the runtime, checkbox is checked then instead of checkbox value the
(caseID )column value is retained....
|
by: Abhishek |
last post by:
I am creating a Datagrid at runtime and I have to create all the columns of
grid at runtime... I am facing problem in the item template column. Here I
want to place more than one control... For...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |