CheckBoxList Selection when used in EditItemTemplate for DataGrid
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngescalate' list.


Chris Kinsman
I am stuck on this one.

I have a CheckBoxList which is used as part of an EditItemTemplate in a
DataGrid.

I set it's DataSource via an inline DataBinding expression like this:

<asp:TemplateColumn
HeaderText="<b>Roles</b>">
<EditItemTemplate>
<asp:checkboxlist
id="chkRoleList" runat="server" DataTextField="GroupName"
DataValueField="GroupId" Font-Size="10" DataSource='<%#
GetRoleList((string)DataBinder.Eval(Container.DataItem, "Email")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>

The problem I am running into is how do I show which roles are selected?
I tried using OnPreRender but it doesn't appear to be called when in a
DataGrid.
OnLoad() is prior to the DataBind.

In the OnEditCommand for the grid the CheckBoxList hasn't been created
yet. It almost feels like this thing either needs another event or a
property that can be used to data bind the selections.

Any ideas?

Chris
Reply to this message...
 
    
Alex Lowe
Chris,

You could do something like this (instead of setting the DataSource,
etc. in the declaration):

1) Modify the declaration to look something like this...

<asp:checkboxlist id="chkRoleList"
            runat="server"
             Font-Size="10" />

2) Wire your DataGrid to the OnItemDataBound event similar to this....

Public Sub DataGrid_ItemDataBound(sender As Object, e As
DataGridItemEventArgs)
Dim itemType As ListItemType = e.Item.ItemType
If (itemType = ListItemType.EditItem) Then
Dim myCheckBoxList As CheckBoxList =
CType(e.Item.FindControl("chkRoleList"), CheckBoxList)
Dim myRow As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim myEmail As String = myRow("Email").ToString()
    
myCheckBoxList.DataTextField = "GroupName"
myCheckBoxList.DataValueField = "GroupId"
myCheckBoxList.DataSource = GetRoleList(myEmail)
myCheckBoxList.DataBind()

    'I don't see how/where you get the selected items from your
message
'but you would cycle through them and use a line similar to the
    'one below to select the appropriate items
myCheckBoxList.Items.FindByValue("yourcheckboxitemvalue").Selected
= true
End If
End Sub

It's not perfect and a property/collection or some other built-in
mechanism would be ideal - but I don't believe one exists.

Hth,

Alex - AspFriends.com Moderation Team
Microsoft MVP - ASP.NET

***********************************************************
Have a question about using client side coding in your ASP.NET pages?
Send your question to [aspngclient]
(http://www.aspfriends.com/aspfriends/aspngclient.asp)
***********************************************************

-----Original Message-----
From: Chris Kinsman [mailto:Click here to reveal e-mail address]
Sent: Thursday, February 21, 2002 10:36 AM
To: aspngescalate
Subject: [aspngescalate] CheckBoxList Selection when used in
EditItemTemplate for DataGrid

I am stuck on this one.

I have a CheckBoxList which is used as part of an EditItemTemplate in a
DataGrid.

I set it's DataSource via an inline DataBinding expression like this:

<asp:TemplateColumn
HeaderText="<b>Roles</b>">
<EditItemTemplate>
<asp:checkboxlist
id="chkRoleList" runat="server" DataTextField="GroupName"
DataValueField="GroupId" Font-Size="10" DataSource='<%#
GetRoleList((string)DataBinder.Eval(Container.DataItem, "Email")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>

The problem I am running into is how do I show which roles are selected?
I tried using OnPreRender but it doesn't appear to be called when in a
DataGrid.
OnLoad() is prior to the DataBind.

In the OnEditCommand for the grid the CheckBoxList hasn't been created
yet. It almost feels like this thing either needs another event or a
property that can be used to data bind the selections.

Any ideas?

Chris
| [aspngescalate] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngescalate.asp = JOIN/QUIT

Reply to this message...
 
 
System.ComponentModel.Container
System.Data.DataRowView
System.Web.UI.DataBinder
System.Web.UI.DataBinding
System.Web.UI.WebControls.CheckBoxList
System.Web.UI.WebControls.DataGrid
System.Web.UI.WebControls.DataGridItemEventArgs
System.Web.UI.WebControls.ListItemType
System.Web.UI.WebControls.TemplateColumn
System.Windows.Forms.DataGrid




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com