Google Answers Logo
View Question
 
Q: Asp.net - Countries DropDownList in Template ( No Answer,   2 Comments )
Question  
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_Cou­ntries.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("DropDow­nListObject"); 
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.
Answer  
There is no answer at this time.

Comments  
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!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy