Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

How to refer to the selected item of a dropdownlist from a datalist

  Asked By: Nichole    Date: Jul 02    Category: Asp.net    Views: 2153
  

I have a Datalist. One of the field defined in the ItemTemplate is
databind with value of a country name, "USA" in this case.
The problem I have is after hitting the Edit button (OnEditCommand) to
go to EditItemTemplate, how do I set the selectedItem to the same
value on a dropdownlist which containing a list of all countries?


<ItemTemplate>
<tr><td>Country:</td>
<td><%#DataBinder.Eval(Container.DataItem,"Country")%></td>
</tr>
</ItemTemplate>

<EditItemTemplate>
<tr><td>Country:</td>
<td><asp:DropDownList id="fCountryEdit"
DataSource='<%#objDS.Tables("tblCountries").DefaultView%>'
DataTextField="Name"
DataValueField="Name"
SelectedIndex=???//What should I do here to set selectedItem?
runat="server"/></td>
</tr>
</EditItemTemplate>

Share: 

 

9 Answers Found

 
Answer #1    Answered By: Ulfah Hashmi     Answered On: Jul 02
 
Answer #2    Answered By: Adaulfo Fischer     Answered On: Jul 02

But the example doesn't really show how to call the value to set  the
selected item  in dropdownlist  in edit  mode.

Take the same example, how do I call the value of "Exotic Liquids"
(ID =1) when it's in edit mode. In general, how do I call the value
of a textBox from the Itemtemplat after going into Edit mode?

 
Answer #3    Answered By: Nicholas Wells     Answered On: Jul 02

myList.Items.FindByText("River Mills").Selected = true;

similar for FindByValue

is that what you're after ?

 
Answer #4    Answered By: Lily Brown     Answered On: Jul 02

I got it.
Actually, the problem  was solved after changing the username in Machine.config
file under <processmodel>.

 
Answer #5    Answered By: Umaiza Hashmi     Answered On: Jul 02

I dont know whether this is good or not but I prefer
myList.SelectedIndex=myList.Items.IndexOf(myList.Items.FindByValue(value))
because this way some times even if the value is null(retreived from the
database, or the session expired) this will still work. But if you do the way
Steve mentioned you need to catch a null reference exception. So its up to you
how you want to use it.

 
Answer #6    Answered By: Barachias Levi     Answered On: Jul 02

I have all the code (or so I think) froma previous site I made,
but that site (although working on the web) is in a bit of a mess on my hdd. My
most recent "test" prj I am even having trouble getting the edititem working
(1st attempt before I've even cleaned my teeth, straight out of bed so to
speak).

I'll get cleaned up and have another look ... will reply when sorted.

To tell the truth, there shouldn't be a problem, edit  mode has more
functionality than item  mode anyway, selecting a preconcieved value from a combo
isn't going to be an issue (I'm sure).
You just have to make sure you pull your existing values froma db and set  the
combos accordingly.

Hang on, are you using ItemDataBound ??? or are you using the datasource = '<%
..... %>' method ???

Note :: don't use this as even if you use a helper function like datasource =
'<%= getSrc() %>' you cannot give it the "DataListItemEventArgs e" that you need
to get to the controls within. So if you are in the designer then GET OUT of
there !! It's restrictive.

Do this ::


<asp:datalist id=DataList1 runat="server" DataKeyField="Manufacturer"
OnItemCreated="Item_Created" OnItemDataBound="List_ItemDataBound"
OnEditCommand="List_Edit" OnSelectCommand="List_Select"
OnSelectedIndexChanged="List_Select">

Then one of your buttons puts you in edit mode

public void List_ItemDataBound(Object sender, DataListItemEventArgs e)
{
.... here set your data source .... and because you have "DataListItemEventArgs
e" coming in you can also use e.Item and get to your controls using

FindControl()

ListItemType itemType = e.Item.ItemType;
if(itemType == ListItemType.EditItem){ }

if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item){}

if(itemType == ListItemType.Footer){ }

}

So then you can set your combos to values that you will probably pull from
database.

Phil ... and anyone on this thread .... I have had to go "back in time" to find
and recap this (no probs), just soz I even suggested the datasource = '<% .....
%>' method in the first place.

NOTE :: This only works in EDIT mode, if you try to stick combos into Item mode
you will find you can fill them but cannot get to their eventhandlers - and so
cannot fire he "row selected  or datalist  row selected" event to do anything
with it.

The reason is that List_Select looks like this
public void List_Select(Object sender, EventArgs e) { } which DOESN'T give you
the "DataListItemEventArgs e", nor the e.Item you need to use FindControl()

There are "fudges" around, I (yesterday) made one .... but it's not up to much.
I can actually get the actual control using FindControl in "List_Select" (by
usng the control collection of the repeater ... thus avoiding the need for
e.Item) and then trying to assign and event handler to the individual control.
.... but still no way.

I haven't given up on that one yet (not quite) but basically asp.NET says "we
don't care what you try you an't using an event handler on a combo in a
repeater".

This will lead me to my next weak point which is Events and Delagates .... I'll
use this problem  as driving force to seewhat I can do with the attchment of that
handler to the combo.

 
Answer #7    Answered By: Naomi Lee     Answered On: Jul 02

you should be working for NASA, as their safety inspector.

 
Answer #8    Answered By: Bathilda Schmidt     Answered On: Jul 02

sorry got the login.aspx cpode where the credentiols of login are
defined, now further goinf through the source

 
Answer #9    Answered By: Joyce Edwards     Answered On: Jul 02


FormsAuthentication.RedirectFromLoginPage(UserEmail.Value,
PersistCookie.Checked)

This will take to which page? I mean after login where are we mentioning the
page to which it should go? I think it will go to default.aspx but if I want to
mention where should it lead, how can I do that?

 




Tagged: