DropDownList in DataGrid ItemTemplate

E

Elroyskimms

I've been searching for an example of this and I've seen several
postings pointing to other web pages, but all of those web pages have
since been removed... or they post pages and pages of sample code and
my eyes can't take it any more... or you could just call me lazy; what
ever works for you, but please help! I know how to create a
DropDownList in the EditItemTemplate and handling the changes in that
DropDownList, but I need to find out how to use a DropDownList in the
ItemTemplate.

I have a datagrid with several bound columns, and one template column.
I want all items to have a DropDownList in that template column when
the datagrid is first displayed. I need the user to be able to choose
any of the DropDownLists, change its SelectedItem and I need to handle
that event. I don't want the user to have to click "Edit", change the
DropDownList selection, and then click "Update". The user will have
10-20 items on each page to work on so shaving off those extra
mouse-clicks will really make a difference.

Thank you!
 
E

Elroyskimms

I've spent 2 days trying to figure this one out and I've got it
working. For posterity and future users with a similar issue, here is
the solution.

First, create a DropDownList in the ItemTemplate for the DataGrid.
Assign its DataSource, DataTextField, and DataValueField properties to
match the function used to populate the DropDownList. Assign the
"OnSelectedIndexChanged" property to the name of the procedure you want
to run when the DropDownList has been changed.

Bind the datasource to the DataGrid as usual (PageLoad, PostBack,
whatever makes you feel good). Create a procedure to handle the
"ItemDataBound" event of the DataGrid. This event will be called for
each item as it is added to the DataGrid. This procedure should have
the usual 'sender' and 'e' parameters, of datatype object and
datagrideventargs (respectively). The 'e' variable represents the
current item being added to the DataGrid. You can access each of the
cells or use the FindControl method to search for the DropDownList by
its ID name. At this point, the DropDownList has been populated from
the datasource you assigned to it. If you need to change which item in
the DropDownList is selected or do anything else with the DropDownList
before it is displayed, here is where you do it. I store the value I
need to find in a Label control with its Visible property set to false
(so the user cannot see it). In the ItemDataBound event, I find that
Label, get its contents and then locate that value in the DropDownList.

And that's it. The event handler for the DropDownList will capture the
changes to the DropDownList and the ItemDataBound event will allow you
to modify each DropDownList in the datagrid.

Here is the DataGrid ItemTemplate setup:

<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:DropDownList
id=ddlStatus
runat="server"
DataSource="<%# qryGetDropDownListData(PublicVariable) %>"
DataTextField="Name"
DataValueField="StatusID"
OnSelectedIndexChanged="ddlStatus_IndexChanged"
AutoPostBack=True</asp:DropDownList>
<asp:Label
id=lblHiddenID
runat="server"
Visible="False"
<!-- The value must be in the TEXT property in order to access its
contents in the ItemDataBound event. -->
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

Here's the ItemDataBound code:

Private Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgLeads.ItemDataBound

If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType =
ListItemType.AlternatingItem) Then

Dim MyDDL As DropDownList
MyDDL = CType(e.Item.Cells(4).FindControl("ddlStatus"),
DropDownList)
MyDDL.SelectedIndex =
MyDDL.Items.IndexOf(MyDDL.Items.FindByValue(CType(e.Item.Cells(4).FindControl("lblHiddenID"),
Label).Text))
MyDDL.Items.Insert(0, "Status Unassigned...")

End If

End Sub


Hopefully this helps someone else out there, because it took me forever
to figure out.
 
N

nari reddy

Hai,
Whenever i using this code for DropDownList fire i got one
errormessage.i.e

"Object reference not set to an instance of an object."
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:


Line 285: Dim MyDDL As DropDownList
Line 286: MyDDL = CType(e.Item.Cells(1).FindControl("dd1"),
DropDownList)
Line 287: MyDDL.SelectedIndex =
MyDDL.Items.IndexOf(MyDDL.Items.FindByValue(CType(e.Item.Cells(1).FindCo
ntrol("lh"), Label).Text))
Line 288: MyDDL.Items.Insert(0, "--Parents list--")
Line 289:


Source File: c:\inetpub\wwwroot\Score\WebForm1.aspx.vb Line: 287


Please give me any solution
Thanks
 
E

Elroyskimms

Nari,

I'm sorry that I did not respond sooner. I haven't checked this post
recently.

I can't find anything wrong at first glance. Try breaking down the code
into smaller pieces to see which component is triggering the exception.
Use something like this:

Dim MyDDL As DropDownList
MyDDL = CType(e.Item.Cells(1).FindControl("dd1"), DropDownList)
Dim ItemCount as integer
ItemCount = MyDDL.Items.Count
Dim SelectedValue as String
SelectedValue = CType(e.Item.Cells(1).FindControl("lh"), Label).Text
Dim SelectedItem as ListItem
SelectedItem = MyDDL.Items.FindByValue(SelectedValue)
MyDDL.SelectedIndex = MyDDL.Items.IndexOf(SelectedItem)

If the exception is thrown on the 4th line, that means the DropDownList
"dd1" was not found in the second cell (Cells are numbered starting at
0 on the left, not 1). Replace the 2nd line with the following:
MyDDL = CType(e.Item.FindControl("dd1"), DropDownList)
This will search all cells in the current list item for the
DropDownList control "dd1".

If the exception is thrown on the 6th line, replace it with the
following:
CType(e.Item.FindControl("lh"), Label).Text
This will search all cells for the control named "lh". If that line
still does not work, then there is a problem locating the label "lh".

If it still is not working, please post the HTML that you are using to
create the DropDownList, Label, and DataGrid controls.

-E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top