Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EditItemTemplate and DropDown issue 1

Status
Not open for further replies.

jkl

Programmer
May 16, 2001
83
US
Here is my edititemtemplate

<EditItemTemplate>
<font class=&quot;Std&quot; color=&quot;#006600&quot;>
<asp:DropDownList id=&quot;edtState&quot; width=&quot;30px&quot; runat=&quot;server&quot; />
</font>
</EditItemTemplate>

I want to be able to dynamicaaly fill this dropdown with a list of US States; when somebody clicks the &quot;Edit&quot; link in an EditCommandColumn in the same DataGrid object.

I have already created a function for
edtState.Items.Add(New ListItem(&quot;Alabama&quot;,&quot;AL&quot;)); but this doesn't work.

How can I do this?

 
try:

edtState.Items.Add(ListItem(&quot;Alabama&quot;,&quot;AL&quot;));

hth
 
Yeah I tried that route too. No go.

I'm using this so I don't double up on the entries:

IF edtState.Items.Count < 1 then
... add items ...
END IF


I get an error message of &quot;Object reference not set to an instance of an object&quot; for the &quot;IF&quot; line of code.
 
Make sure that the code you are using to fill in the list is in the &quot;ItemCreated&quot; Event of the datagrid.

before you need to do this:
[c#]
ListItemType lit = e.Item.ItemType;

if (lit == ListItemType.Item || lit == ListItemType.AlternatingItem)
{
DropDownList edtState = (DropDownList) e.Item.FindControl(&quot;edtState&quot;);
if edtState.Items.Count < 1
{
ListItem lstAlabama = new ListItem(&quot;Alabama&quot;,&quot;AL&quot;);
edtState.Items.Add(lstAlabama);
// add all the other states
}
}

hope this helps
 
Okay. Here's my onItemCreated function

I'm still getting the &quot;Object reference not set to an instance of an object&quot; error for the line(s) indicated with **

==================
Sub ItemCreated_Res(ByVal Sender As Object, ByVal e As DataGridItemEventArgs)


Select Case e.Item.ItemType
Case ListItemType.Item
Dim myDeleteButton As TableCell
myDeleteButton = e.Item.Cells(0)
myDeleteButton.Attributes.Add(&quot;onClick&quot;, &quot;return confirm('Are you Sure you want to delete this entry?');&quot;)
If IsPostBack Then
'popStates_Drop(resStates)
Dim resStates As DropDownList = e.Item.FindControl(&quot;edtResState&quot;)
** resStates.Items.Add(New ListItem(&quot;Alabama&quot;, &quot;AL&quot;))
** resStates.Items.Add(New ListItem(&quot;Alaska&quot;, &quot;AK&quot;))
End If

Case ListItemType.AlternatingItem
Dim myDeleteButton As TableCell
myDeleteButton = e.Item.Cells(0)
myDeleteButton.Attributes.Add(&quot;onclick&quot;, &quot;return confirm('Are you Sure you want to delete this entry?');&quot;)
If IsPostBack Then
Dim resStates As DropDownList = e.Item.FindControl(&quot;edtResState&quot;)
** resStates.Items.Add(New ListItem(&quot;Alabama&quot;, &quot;AL&quot;))
** resStates.Items.Add(New ListItem(&quot;Alaska&quot;, &quot;AK&quot;))
End If

End Select
End Sub
==================
 
upps sorry my fault: here add this case:


Case ListItemType.EditItem

Dim DropDownList edtState = (DropDownList) e.Item.FindControl(&quot;edtState&quot;);
if edtState.Items.Count < 1 then
Dim ListItem lstAlabama = new ListItem(&quot;Alabama&quot;,&quot;AL&quot;)
edtState.Items.Add(lstAlabama)
end if
 
Terrific. That case statement did the trick.

Now another thing. How do I get the state, as currently listed in the recordset, to be set as the &quot;selected&quot; item in the dropdown?
 
ah hah I got this one.

dropdown.SelectedIndex = dropdown.Items.IndexOf(dropdown.Items.FindByText(dataset.table(0).Field))

this should work perfectly as long as your dataset contains only one record. If not then simply change the
dataset.table(0).Field to what ever is appropriate for you.

Hope this does it for ya [peace]
 
okay, i understand the process of hwo to do this. but...

the dropdown object is instantiated in the item_created() sub, and my dataset is in another sub getData(), which binds the data to the datagrid.

those two objects do no co-exist in any portions of the code, so how do i get one to interact with the other?
 
Well there are a few ways you could do this. Probably the most simple being to make your datset global to the form. Another method that I think should work is to put the value that you want into a hidden textbox and read it back from there.

Your choice pretty much your options are limited only by your imagination. [peace]
 
Sheesh, I need to learn to use punctuation a little better I think. [peace]
 
&quot;punctuation 101&quot; class is right down the hall to the left Zarcom. When you see the teacher, tell him I wont make this evening =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top