PC Review


Reply
Thread Tools Rate Thread

Adding checkbox to datagrid headers

 
 
=?Utf-8?B?VmljdG9yIEhlaWprZQ==?=
Guest
Posts: n/a
 
      18th Nov 2005
Can anyone help me with this.
I have a datagrid on a page which is used to show different views from
SQL-Server.
With autogenerate columns, this works fine for me. But now I want a checkbox
before every caption in the header. With this checkbox I'm goint to determine
if the user wants that field in his/her final print or export.

I'm able to add the checkbox on designtime by using Itemtemplates, but I can
not get this to work on runtime.

Thanks in advance
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2FsdmluIEtE?=
Guest
Posts: n/a
 
      21st Nov 2005
Hi Victor,
Shouldn't you be adding the checkbox control in the
<HeaderTemplate></HeaderTemplate> instead of <ItemTemplate>? then in the
ItemDataBound event of the datagrid, write:
public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
CheckBox myCheckbox = (CheckBox)e.Item.FindControl("CheckBox1");
if (myCheckbox != null)
{
// Inject clientside click event to capture the checkbox's value
and put it into a hidden field txtColumnSelected
myCheckbox.Attributes.Add("onclick",
"document.all.txtColumnSelected.value = " + myCheckbox.checked);
}
}
}

Hope that's what you're looking for.
Calvin.

"Victor Heijke" wrote:

> Can anyone help me with this.
> I have a datagrid on a page which is used to show different views from
> SQL-Server.
> With autogenerate columns, this works fine for me. But now I want a checkbox
> before every caption in the header. With this checkbox I'm goint to determine
> if the user wants that field in his/her final print or export.
>
> I'm able to add the checkbox on designtime by using Itemtemplates, but I can
> not get this to work on runtime.
>
> Thanks in advance

 
Reply With Quote
 
=?Utf-8?B?VmljdG9yIEhlaWprZQ==?=
Guest
Posts: n/a
 
      21st Nov 2005
Thanks for your reaction, you are right.
I'm not even able to add the items to the header on runtime.
If I use something like this in ItemDataBound

Dim chkBox As CheckBox
Try

If e.Item.ItemType = ListItemType.Header Then
chkBox = New CheckBox
e.Item.Controls.Add(chkBox)
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try

than I get a 'TableRow' cannot have children of type 'CheckBox'. error

Any ideas?

Victor

"Calvin KD" wrote:

> Hi Victor,
> Shouldn't you be adding the checkbox control in the
> <HeaderTemplate></HeaderTemplate> instead of <ItemTemplate>? then in the
> ItemDataBound event of the datagrid, write:
> public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
> {
> if (e.Item.ItemType == ListItemType.Header)
> {
> CheckBox myCheckbox = (CheckBox)e.Item.FindControl("CheckBox1");
> if (myCheckbox != null)
> {
> // Inject clientside click event to capture the checkbox's value
> and put it into a hidden field txtColumnSelected
> myCheckbox.Attributes.Add("onclick",
> "document.all.txtColumnSelected.value = " + myCheckbox.checked);
> }
> }
> }
>
> Hope that's what you're looking for.
> Calvin.
>
> "Victor Heijke" wrote:
>
> > Can anyone help me with this.
> > I have a datagrid on a page which is used to show different views from
> > SQL-Server.
> > With autogenerate columns, this works fine for me. But now I want a checkbox
> > before every caption in the header. With this checkbox I'm goint to determine
> > if the user wants that field in his/her final print or export.
> >
> > I'm able to add the checkbox on designtime by using Itemtemplates, but I can
> > not get this to work on runtime.
> >
> > Thanks in advance

 
Reply With Quote
 
=?Utf-8?B?Q2FsdmluIEtE?=
Guest
Posts: n/a
 
      23rd Nov 2005
Hi Victor,
First of all, I'd like to establish that you're using .Net Framework 1.1
aren't you? because my solution only works with 1.1, not 2.0.
OK, I always use an HTML Table rather than the Webcontrol table. Here's what
i do:
<aspataGrid ...>
<HeaderTemplate>
<Table id="..." ... runat="server">
<tr>
<td width="10%"><asp:CheckBox id="CheckBox1" ...> Column1
Header</td>
<td width="10%"><asp:CheckBox id="CheckBox2" ...> Column2
Header</td>
...
</tr>
</Table>
</HeaderTemplate>
</aspataGrid>

The check boxes are created at design time rather than at runtime. Once
that's done, use the solution that i previously proposed to try and obtain
the values from those checkboxes.

Let me know if still have problems. More than happy to help you out.
Cheers,
Calvin
"Victor Heijke" wrote:

> Thanks for your reaction, you are right.
> I'm not even able to add the items to the header on runtime.
> If I use something like this in ItemDataBound
>
> Dim chkBox As CheckBox
> Try
>
> If e.Item.ItemType = ListItemType.Header Then
> chkBox = New CheckBox
> e.Item.Controls.Add(chkBox)
> End If
> Catch ex As Exception
> Response.Write(ex.Message)
> End Try
>
> than I get a 'TableRow' cannot have children of type 'CheckBox'. error
>
> Any ideas?
>
> Victor
>
> "Calvin KD" wrote:
>
> > Hi Victor,
> > Shouldn't you be adding the checkbox control in the
> > <HeaderTemplate></HeaderTemplate> instead of <ItemTemplate>? then in the
> > ItemDataBound event of the datagrid, write:
> > public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
> > {
> > if (e.Item.ItemType == ListItemType.Header)
> > {
> > CheckBox myCheckbox = (CheckBox)e.Item.FindControl("CheckBox1");
> > if (myCheckbox != null)
> > {
> > // Inject clientside click event to capture the checkbox's value
> > and put it into a hidden field txtColumnSelected
> > myCheckbox.Attributes.Add("onclick",
> > "document.all.txtColumnSelected.value = " + myCheckbox.checked);
> > }
> > }
> > }
> >
> > Hope that's what you're looking for.
> > Calvin.
> >
> > "Victor Heijke" wrote:
> >
> > > Can anyone help me with this.
> > > I have a datagrid on a page which is used to show different views from
> > > SQL-Server.
> > > With autogenerate columns, this works fine for me. But now I want a checkbox
> > > before every caption in the header. With this checkbox I'm goint to determine
> > > if the user wants that field in his/her final print or export.
> > >
> > > I'm able to add the checkbox on designtime by using Itemtemplates, but I can
> > > not get this to work on runtime.
> > >
> > > Thanks in advance

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a CheckBox to a DataGrid at Runtime Fao, Sean Microsoft ASP .NET 3 27th Jan 2006 07:41 PM
adding a checkbox to a datagrid =?Utf-8?B?b3NjYXI=?= Microsoft Dot NET Framework Forms 0 9th Mar 2005 08:39 PM
adding radiobutton or checkbox in a datagrid daisy Microsoft Dot NET Framework 1 25th Jul 2003 12:51 AM
adding radiobutton or checkbox in a datagrid daisy Microsoft Dot NET 0 24th Jul 2003 12:14 PM
adding radiobutton or checkbox in a datagrid Daisy Microsoft Dot NET Framework Forms 0 24th Jul 2003 12:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:29 AM.