![]() |
|
|
| Subject:
Asp.net - Countries DropDownList in Template
Category: Computers > Programming Asked by: vicx-ga List Price: $10.00 |
Posted:
05 Sep 2005 08:04 PDT
Expires: 05 Oct 2005 08:04 PDT Question ID: 564460 |
Hello,
I am using ASP.NET and adding columns to the datagrid
programmatically. And I have created a countries drop down list in an
ascx file. I have added the
countries drop down list to the datagrid column as follows,
TemplateColumn tc = new TemplateColumn();
ITemplate iTemplate = LoadTemplate("DropDownList_Countries.ascx");
tc.EditItemTemplate = iTemplate;
DataGrid1.Columns.Add(tc);
The drop down list has been loaded successfully on the datagrid. And
now I want to set it to a value (according to the database binding).
Say, set to the value of Norway.
I have tried this but not successful:
DropDownList ddlCountry = (DropDownList)
DataGrid1.FindControl("DropDownListObject");
ddlCountry.SelectedValue = "Norway";
The error is "System.NullReferenceException: Object reference not set
to an instance of an object." (Probably the control cannot be found)
How can I access the dropdownlist object inside the iTemplate and set
its value?
I was looking into the instantiateIn method, but not sure how to fit
this into my case. |
|
| There is no answer at this time. |
|
| Subject:
Re: Asp.net - Countries DropDownList in Template
From: awilinsk-ga on 22 Sep 2005 04:59 PDT |
You have to select the value through the ItemDataBound Event. Make a
non-visible column in the datagrid and bind the column to the value
that you want to be selected in the dropdownlist. Set the
onitemdatabound attribute in the datagrid control to an event. The
event code would look like this.
Public Sub ItemBound_Prog_Grid(sender As Object, e As DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item Or ListItemType.AlternatingItem
CType(e.Item.Cells(2).FindControl("lstCountries"),DropDownList).SelectedValue=e.Item.Cells(5).Text
Case ListItemType.Footer
End Select
End Sub
The select case statement makes sure that the item is of type Item or
AlternatingItem. You find the control in the cell it is located and
cast it to the type of DropDownList. Then you take the selected value
and assign it the the non-visible column that holds the value you need
to be selected. |
| Subject:
Re: Asp.net - Countries DropDownList in Template
From: vicx-ga on 22 Sep 2005 06:39 PDT |
Thank you very much for commenting my question! I was puzzling on how to achieve that. I will try your recommendation. Thanks again! |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |